mercredi 30 septembre 2015

How to query pivot table using Eloquent in Laravel 5

I have a many-to-many relationship between my client and tag tables. A client can have many tags, and each tag can be related to multiple clients.

On the client show view, i'm trying to display the client info plus all tags associated to this client.

How do I change the query below to retrieve the client row with all its related tags?

public function show($id)
{
    $client = Client::findOrFail($id);

    return view('clients.show')->with(['client' => $client]);
}

Client model

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

Clienttag model

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

Client_clientags table migration

public function up()
{
    Schema::create('client_clienttag', function(Blueprint $table)
    {
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->integer('clienttag_id')->unsigned();
        $table->foreign('clienttag_id')->references('id')->on('clienttags')->onDelete('cascade');

        $table->timestamps();
    });
}   



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1O2f1dZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire