php - Where with Eloquent Model -


in laravel, have 2 table:

post table:

╔════╦══════════════╦══════╗ ║ id ║  content     ║userid║ ╠════╬══════════════╬══════╣ ║  1 ║ lorem 1      ║  1   ║ ║  2 ║ lorem 2      ║  3   ║ ║  3 ║ lorem 3      ║  1   ║ ║  4 ║ lorem 4      ║  2   ║ ╚════╩══════════════╩══════╝ 

and user table:

╔════╦════════════════╗ ║ id ║  email         ║ ╠════╬════════════════║ ║  1 ║user1@email.com ║ ║  2 ║user2@email.com ║ ║  3 ║user3@email.com ║ ║  4 ║user4@email.com ║ ╚════╩════════════════╝ 

i want select posts posted user id using laravel. post, user model:

#post model class post extends \eloquent {     public function user()     {         return $this->belongsto('user', 'userid');     } }  #user model class user extends \eloquent {     public function post()     {         return $this->hasmany('post');     } } 

now, select using laravel eloquent:

$post = post::with(array(     'user'  =>  function($query)     {         $query->where('email', input::get('email'));     } ))->paginate(10); 

this code select post, not user, , paginate wrong page. can me ? !

you're looking wherehas method:

$posts = post::wherehas('user', function($query) {     $query->where('email', input::get('email'));  })->paginate(10); 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -