Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
借助关系获取Laravel中父类别和子类别内的所有产品_Laravel_Laravel 5_Eloquent - Fatal编程技术网

借助关系获取Laravel中父类别和子类别内的所有产品

借助关系获取Laravel中父类别和子类别内的所有产品,laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,我有如下类别和产品表: 类别: 产品: 产品类别: 产品型号: id | name | slug | parent_id id | brand_id | name | slug | price | desc id | category_id | product_id <?php namespace App\Models; use App\User; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model;

我有如下类别和产品表:

类别:

产品:

产品类别:

产品型号:

id | name | slug | parent_id
id | brand_id | name | slug | price | desc
id | category_id | product_id
<?php

namespace App\Models;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    /**
     * @var string
     */
    protected $table = 'products';

    /**
     * @var array
     */
    protected $fillable = [
        'brand_id', 'sku', 'name', 'slug', 'description', 'quantity',
        'weight', 'price', 'sale_price', 'status', 'featured',
    ];

    /**
     * @var array
     */
    protected $casts = [
        'quantity'  =>  'integer',
        'brand_id'  =>  'integer',
        'status'    =>  'boolean',
        'featured'  =>  'boolean'
    ];

     /**
     * @param $value
     */
    public function setNameAttribute($value)
    {
        $this->attributes['name'] = $value;
        $this->attributes['slug'] = Str::slug($value);
    }

    public function categories()
    {
    return $this->belongsToMany(Category::class, 'product_categories', 'product_id', 'category_id');
    }

    public function product_attributes()
    {
    return  $this->belongsToMany(Attribute::class, 'product_attributes')->withPivot('value', 'quantity');
    }

    public function users()
    {
    return $this->belongsToMany(User::class,'wishlists');
    }

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TypiCMS\NestableTrait;
use Illuminate\Support\Str;


class Category extends Model
{
    use NestableTrait;
    protected $table = 'categories';

    protected $fillable = [
        'name', 'slug', 'description', 'parent_id', 'featured', 'menu', 'image',
    ];

    protected $casts = [
        'parent_id' => 'integer',
        'featured' => 'boolean',
        'menu' => 'boolean',
    ];

    public function setNameAttribute($value)
    {
        $this->attributes['name'] = $value;
        $this->attributes['slug'] = str_slug($value);
    }

    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }


    public function products()
    {
    return $this->belongsToMany(Product::class, 'product_categories', 'category_id', 'product_id');
    }
 }
public function show($slug)
    {
        $category = $this->categoryRepository->findBySlug($slug);
        $child_categories=$this->categoryRepository->getChildCategories($category->id); 
        dd($child_categories->children);  //return all child categories which are inside parent category
        return view('site.pages.category', compact('category','child_categories'));
    }