Do you know how to build a query that does what you want? If so, we can help you translate it to the Laravel query-builder. Build the SQL first.
I think I had one built once. I'm working on trying to replicate it
Alright, SQL is not my strength...
$posts = DB::table('posts')
->leftJoin('users', 'posts.user_id', '=', 'users.id')
->join('ranks', 'users.rank_id', '=', 'ranks.id')
->join('regions', 'users.region_id', '=', 'regions.id')
//->join('postcomments', 'posts.id', '=', 'postcomments.post_id')
->select('users.username as username',
'ranks.name as rank',
'regions.id as regionid',
'regions.name as region'
//'postcomments.comment as postcomment',
//'postcomments.author_id as commentauthor'
)
->where('regions.id', '=', 6)
->get();
foreach ($posts as $post) {
var_dump($post->username);
var_dump($post->rank);
var_dump($post->region);
//var_dump($post->postcomment);
echo "<hr>";
}
I got it working for a bit, but when postcomments are being joined ONLY posts with comments are showing up. I'm sure this is obvious to most of you, but I'm lost and not sure how to go further.
I just need help getting this 'where' working, I think if someone guides me with one (regions) I can work out how to do the others.
Hey friend, I got your post on my thread, and now I didn't understand exactly what you want but maybe it will help.
$posts = Post::with('user'=>function($query){
$query->where('region_id', '=', 6);
[user_AREA_WHERES]
}))
->with(array('lookingfors'=>function($query){
//whatever you need
[lookingfors_AREA_WHERES]
}))
->with(array('playstyles'=>function($query){
//whatever you need
[playstyles_AREA_WHERES]
}))
->with(array('postcomments'=>function($query){
//whatever you need
[postcomments_AREA_WHERES]
}))
[POST_AREA_WHERES]
->take($this->numOfPostsInAreaPageLoad)
->paginate(10)
->get();
I'm sure it is not what you wanted, but tell me what you need. please view the areas surrounded with brackets [ ]
If it doesnt work, please explain who belongs to who: user lookingfors playstyles postcomments
Thank you for this. I'm at work now so I will try it when I get home.
In case it wasn't clear. I'm trying to filter posts by information in the users table as well as posts table. So this looks pretty good and I will let you know what happens when I get a chance to test it tonight.
Cheers
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community