Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

What's the schema and what's the error?

From a glance.. Why do you have post_id and comment_id in those relationships?
You have a post_id field in the posts table and a comment_id field in the comments table?

Last updated 2 years ago.
0
public function comments()
{
 return $this->hasMany('Comment','comment_id');
}

Has many Comment, not Comments. With relation you refer to model, not table.

Also, why would you use post_id as foreign key in User model for posts function? It's like you want to insert id from User model into post_id in Post model. Your foreign key, in Post model should be user_id, so you can assign User to Post as an author.

Last updated 2 years ago.
0

thanks for the replies..

Yes... I used post_id field in the posts table and a comment_id field in the comments table..

This is the error i'm getting... SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from posts where id = ? limit 1) (Bindings: array ( 0 => 15, ))

Last updated 2 years ago.
0

Well, I think you will have problem with User<>Post relation later with wrong foreign key (unless you really want to assign User's id to post_id), just giving you a heads-up.

Do you have self-incrementing id column in your Posts table? Find() searches by id.

Last updated 2 years ago.
0

Hi Belar.

These are my tables......

users:id name

posts:post_id(pk),post,user_id

comments:comment_id(pk),Comment,post_id

Last updated 2 years ago.
0
Solution

Well, here is the problem. Find() function is looking for ID column in your posts table, but it's not there.

Now you could reconfig that, so it will be searching by post_id (Redefine primary key in model), but I would advice to just add self-incrementing id column to your posts table or at least rename the post_id one.

On that topic, take a look at schema building in Laravel:
http://laravel.com/docs/schema
If you get your DB right, it will be huge improvement for your future development.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.