mardi 20 mars 2018

Laravel - Query builder get relation

In my project I have a Polymorphic relation. This relation is as followes:

Plugins
    id - integer
    composer_package - string

Themes
    id - integer
    composer_package - string

Products
    id - integer
    name - text
    ...
    commentable_id - integer
    commentable_type - string

When I need to display all the products that are a Theme I do the following:

$themes = DB::table('products')->orderBy('id', 'asc')->where('productable_type', Theme::class)->get();

The code above provides me with a list of all the products where the productable_type field is filled with App/Theme, I display them with a foreach loop like this @foreach($themes as $theme). Now my problem is. I need to get the composer_package from the themes table that belongs to that product. So say for instance. I get a product from the products table where the productable_id is 3. I want the value of the composer_package field in the themes table where the ID is 3. When I do or It gives me the error Undefined property What is causing this?

This is my product model

public function product()
{
    return $this->morphTo();
}


public function order_items()
{
    return $this->hasMany(Orderitems::class);
}

And this is my theme model

public function webshops()
{
    return $this->hasMany(Webshop::class);
}


public function products()
{
   return $this->morphMany(Product::class, 'productable');
}

Thanks in advance!



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

Aucun commentaire:

Enregistrer un commentaire