Skip to content

Instantly share code, notes, and snippets.

@clarkf
Last active January 3, 2016 14:59
Show Gist options
  • Select an option

  • Save clarkf/8479441 to your computer and use it in GitHub Desktop.

Select an option

Save clarkf/8479441 to your computer and use it in GitHub Desktop.
Laravel 4.1.2
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)
<?php
class Post extends \Illuminate\Database\Eloquent\Model
{
public function categories()
{
return $this->belongsToMany('Category', 'category_posts');
}
}
<?php
class Category extends \Illuminate\Database\Eloquent\Model
{
public function posts()
{
return $this->belongsToMany('Post', 'category_posts');
}
}
<?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