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

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.

Last updated 2 years ago.
0

I think I had one built once. I'm working on trying to replicate it

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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 [ ]

  1. if you want to filter the posts put your wheres in POST_AREA_WHERES
  2. if you want to filter the users put your wheres in user_AREA_WHERES
  3. if you want to filter the lookingfors put your wheres in lookingfors_AREA_WHERES etc....

If it doesnt work, please explain who belongs to who: user lookingfors playstyles postcomments

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Soooo...did this work?

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.