Last active
January 3, 2016 14:59
-
-
Save clarkf/8479441 to your computer and use it in GitHub Desktop.
Laravel 4.1.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SQLSTATE[HY000]: General error: 1 ambiguous column name: id | |
| (SQL: select * from "categories" inner join "category_posts" on "categories"."id" = "category_posts"."category_id" where "category_posts"."post_id" = 1 and "id" = 1 limit 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Post extends \Illuminate\Database\Eloquent\Model | |
| { | |
| public function categories() | |
| { | |
| return $this->belongsToMany('Category', 'category_posts'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Category extends \Illuminate\Database\Eloquent\Model | |
| { | |
| public function posts() | |
| { | |
| return $this->belongsToMany('Post', 'category_posts'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class MyGreatTest extends PHPUnit_Framework_TestCase | |
| { | |
| public function testAssociation() | |
| { | |
| $post = Post::create(); | |
| $category = Category::create(); | |
| $post->categories()->attach($category); | |
| $this->assertCount(1, $post->categories()->where('id', $category->id)); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment