Support the ongoing development of Laravel.io →
Database Eloquent Forms

I've asked this question on IRC and stackoverflow, but I may have described it wrong or too complicated, so I'm going to try and be as brief as possible in what I'm trying to do.

I have models:

User

        public function region()
	{
		return $this->belongsTo('Region');
	}

	public function skill()
	{
		return $this->belongsTo('Skill');
	}

	public function rank()
	{
		return $this->belongsTo('Rank');
	}
	
	public function status()
	{
		return $this->belongsTo('Status');
	}

	public function voips()
	{
		return $this->belongsToMany('Voip');
	}

	public function posts()
	{
		return $this->hasMany('Post');
	}

Posts

        public function user()
	{
		return $this->belongsTo('User');
	}

	public function steamuser()
	{
		return $this->belongsTo('Steamuser');
	}

	public function playstyles()
	{
		return $this->belongsToMany('Playstyle');
	}

	public function lookingfors()
	{
		return $this->belongsToMany('Lookingfor');
	}
	public function postcomments()
	{
		return $this->hasMany('PostComment');
	}

I won't put in the others referencing back to these, because they should be fine as it's been working.

Current posts/index

$posts = Post::with('user','lookingfors','playstyles', 'postcomments')->orderBy('id', 'DESC')->paginate(10);
return View::make('posts/index', compact('posts'));

What I want to do:

I want to build a filter where the user can adjust the results being called by:

  • region
  • rank
  • lookingfors
  • playstyle
  • skill

That's it, as simple as that. Or so I thought when I started trying to implement this over a month ago. I'm at my wits end.

What I've tried...

I have been all over the place looking for a solution.

From laravel docs:

$posts = Post::with(array('user' => function($query)
{
	$query->where('region_id', 'like', '6');
}), 'lookingfors', 'playstyles', 'postcomments')->orderBy('id', 'DESC')->paginate(10);

Not sure if this worked or not, because it outputted the posts without usernames.

There's a couple others but this post is becoming too long already.

I'm not sure this is allowed but I'd like to post a link to my website so you can maybe get a better idea of what I'm trying to do: Website

Thank you for taking the time to have a look at my problem!

  • Mike
Last updated 3 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 3 years ago.
0

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

Last updated 3 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 3 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 3 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 3 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.

© 2025 Laravel.io - All rights reserved.