jeudi 12 avril 2018

Laravel 5: Relations through several tables

I have the following models: Merchant, Product and Store:

Merchant:

class Merchant extends Model
{
    public function products() {
        return $this->hasMany('App\Product');
    }
}

Product:

class Product extends Model
{
    public function merchants() {
        return $this->belongsTo('App\Merchant');
    }


    public function stores() {
        return $this->belongsTo('App\Store');
    }
}

Store:

class Store extends Model
{
    public function products() {
        return $this->hasMany('App\Product');
    }
}

In my MerchantController, I have the following method show():

public function show() {

    $merchants = Merchant::selectRaw('merchants.*, REPLACE(abstract, \'[[name]]\', name) AS abstract')
    ->with('products')
    ->where('active', 'yes')
    ->Paginate(10);

    dd($merchants);

    //return view('merchants', compact('merchants'));
}

How can I access the stores, the several products are in? I tried ->with(['products', 'stores']) and got an error: Call to undefined relationship [stores] on model [App\Merchant].

What can I do to solve my problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire