vendredi 14 septembre 2018

how can i replace raw query to laravel formate

DB::select("SELECT products.* ,categories.* FROM products 
INNER JOIN category_products ON category_products.product_id = products.id
INNER JOIN categories ON category_products.category_id = categories.id 
WHERE categories.slug='$slug'")

This row query working fine but i can not access their relation like

$product->Categories->first()->name

@foreach($product->Images as $image)

My product Model below :

class Product extends Model
{
    public function Category(){
        return $this->hasMany(CategoryProduct::class);
    }
    public function getCategory($id){
       return Category::find($id)->name;
    }
    public function Images(){
       return $this->hasMany(Image::class);
    }

    public function Categories()
    {
        return $this->belongsToMany(Category::class, 'category_products');
    }
}

so how can convert my code to Laravel eloquent



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

Aucun commentaire:

Enregistrer un commentaire