Laravel 4 Eloquent 'hasMany': how to set the default lookup value rather than primay key -
i want retrieve comments under post_id(post1) in page:
//localhost/posts/post1/ table comments
id:int[primary], comment:varchar, post_id:varchar, comment_id:varchar 1 comment post_1 comment_1 table posts
id:int[primary], post_title:varchar, post_id:varchar 1 post title post_1 model comment.php
public function post() { return $this->belongsto('post'); } model post.php
public function comments() { return $this->hasmany('comment'); } controller postscontroller.php
public function show($id) { $comments = post::where('post_id','=',$id)->first() ->comments()->where('post_id','=',$id)->get(); } when visit //localhost/posts/post1/ , no related comment displayed. sql runs below:
select * `posts` `post_id` = 'post1' limit 1 select * `comments` `comments`.`post_id` = '1' , `post_id` = 'post1' how can remove `post_id = '1'` retrieve corresponding comments?
in posts table id column should named id instead of post_id laravel expects. possible set primary key in eloquent model other id better use id , follow standard. please refer docs here http://four.laravel.com/docs/eloquent. once naming id column id can individual post post::find($id)
Comments
Post a Comment