vendredi 19 avril 2019

Many to Many wherehas

i want to search a post that have simillar tag only with tags id number

i've tried this

$tags = Tag::find($id);
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('id',$id);})->paginate(5);

    return view('tagdetail', compact('tags', 'post'));

but the output is SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (23000).

I've already search through the internet and still cant.

Tag.php

public function post()
    {
        return $this->belongsToMany('App\Post');
    }

Post.php

public function tags()
    {
        return $this->belongsToMany('App\Tag');
    }

controller

public function detailTag($id)
    {
        //i know this will get all the post with the tag id, but i want to paginate it too. 
        $tags = Tag::find($id);

        $post = Post::whereHas('tags', function($q) use ($id){
    $q->where('id',$id);
})->paginate(5);

        return view('tagdetail', compact('tags', 'post'));
    }

The expected result : to show all post that have simillar tag with paginate.

Actual result : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (23000)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire