Reputation: 473
I have posts model and tags model I need to make a search query so the user inputs the tags and then the function takes this tag and displays all related posts to it so it is many to many relation ship post belongs to many tags and tags belongs to many posts so i declared the relation ship but I can't find a way to make where clause with the relation ship to be obvious I don't know what to put in the model name and in the relationship function
This is my search function
public function search()
{
$q = Input::get ( 'q' );
$posts = post::where('tag_name','LIKE','%'.$q.'%')->tags()->get;
dd($posts);
if(count($posts) > 0)
return view('guest.blog.search_result' , ['title' => 'Resaults'])->withDetails($posts)->withQuery ( $q );
else return view ('guest.blog.no_resault' , ['title' => 'Resaults'])->withMessage('No Details found');
}
my posts model
public function tags()
{
return $this->belongsToMany('\Conner\Tagging\Model\Tagged');
}
my tags model (it is from rtconner package)
this relationship is from the original package
public function tag()
{
$model = $this->taggingUtility->tagModelString();
return $this->belongsTo($model, 'tag_slug', 'slug');
}
and i declared this relation
public function posts()
{
return $this->belongsToMany('App\Post');
}
my question is how to do the search query works correctly without errors
Note: there is a search field where the user will write the tags only so i should take this input which is the tag then look for its corresponding post and then i will display it to the user
EDIT
my search function
public function search()
{
$q = Input::get ( 'q' );
// i used get because i want all the posts which belongs to the same tag $posts = Tagged::where('tag_name','LIKE','%'.$q.'%')->with('posts')->get(); dd($posts); if(count($posts) > 0) return view('guest.blog.search_result' , ['title' => 'Results'])->withDetails($posts)->withQuery ( $q ); else return view ('guest.blog.no_result' , ['title' => 'Results'])->withMessage('No Details found'); }
my tagged and posts models are the same
I added a table in the database the name of it is post_tagged, and in it I have two columns post_id
and tagged_id
.
So the relation ship will happen through this table if you didn't create it it will throw SQL state error
Upvotes: 0
Views: 185
Reputation: 1384
From my understanding you are trying to query posts based on tags they belong to.
The first thing you're doing wrong, unless you have a tag_name column on your post table, is that you're querying the post tables tag_name and not the tag table.
public function search()
{
$q = Input::get ( 'q' );
//Use first instead of get incase of multiple results
$tag = Tag::where('tag_name','LIKE','%'.$q.'%')->with('posts')->first();
return view('guest.blog.search', compact('tag','q'));
}
What you need to do is query the tag based on its name. Then eager load all of the tags posts. You don't need the conditional statement in the controller as well. You can have a conditional statement in your blade template like so:
@if($tag->count())
@if($tag->post->count())
@foreach($tag->post as $post)
{{$post->title}}
@endforeach
@else
<h1>No posts found
@endif
@else
<h1>There was no tag. {{$q}}</h1>
@endif
Upvotes: 1