0

So i have a Post table, where each post is in a album, i want to search by post attribute, but also bring the post's album in view, how do i do that?

i've tried using this in controller

$posts = album::whereHas('post', function($q) use ($search) {
            $q->where('nama', 'like', "%$search%", 'or')->where('deskripsi', 'like', "%$search%");
        })->get();

and this is my blade view to display the album first then the post

@foreach($posts as $p )
.....
    @foreach($p->post->where('nama', 'like', "%$search%", 'or')->where('deskripsi', 'like', "%$search%") as $post)
    ....
    @endforeach
@endforeach

but the problem is its giving me all the album, even the one that dont have post in it. my english is not the best but i hope you can understand it

mdexp
  • 3,492
  • 2
  • 9
  • 20
Rznboth
  • 3
  • 2

3 Answers3

0

Add this in your Post model: $with = ['album'];, it suppose to add the album model to each and every post retrieved from the database. If the relationship method (in Post model) has a different name than album, change this array accordingly.

Then just use this query:

$posts = Post::where('nama', 'like', "%$search%")->orWhere('deskripsi', 'like', "%$search%")
        ->get();

And iterate like this:

@foreach($posts as $p)
    {{ $p->album->title }}
@endforeach
0

Change your query as below:

$albums = album::whereHas('post', function($q) use ($search) {
                  $q->where('nama', 'like', '%'.$search.'%')
                })
                ->orWhereHas('post', function($q) use ($search) {
                  $q->where('deskripsi', 'like', '%'.$search.'%')
                })
                ->get();

Chagne in your view :

@foreach($albums as $album )
..... // display here album data
    @foreach($album->post as $post)
    .... // display here post data
    @endforeach
@endforeach

Edit:

In your album model, define new relationship for post:

public function searchedPosts($search = null)
{
    $posts = $this->hasMany('App\Post'); // your model path
    if($search!=null){
      $posts->where('nama', 'like', '%'.$search.'%')->orWhere('deskripsi', 'like', '%'.$search.'%');
    }

    return $posts;
}

Change your query as below:

$albums = album::with('post)->whereHas('post', function($q) use ($search) {
                  $q->where('nama', 'like', '%'.$search.'%')
                })
                ->orWhereHas('post', function($q) use ($search) {
                  $q->where('deskripsi', 'like', '%'.$search.'%')
                })
                ->get();

Change your view as below :

@foreach($albums as $album )
..... // display here album data
    @foreach($album->searchedPosts($search)->get() as $post)
    .... // display here post data
    @endforeach
@endforeach
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
  • okay this is work, so im gonna accept this but how do i display the posts that only have $search in their nama or deskripsi ? thanks – Rznboth Feb 07 '20 at 11:28
  • okay its a improvment, but now it shows post inside another albums. for example in albums 1 thers only 2 result but instead it showing all result even the third result is for albums 2, and so albums 2 give all 3 result too, i hope you can understand it, and sorry for the noob question, i just started into laravel. – Rznboth Feb 07 '20 at 13:30
0
 public function search(Request $request)
    {
        $query = $request->input('query');
        $posts = Post::where('title','LIKE',"%$query%")
            ->approved()->publish()->get();
        return view('FrontPage.search',compact('posts','query'));
    }