Php Laravel:使用雄辩的语言从三个表格中选择

Php Laravel:使用雄辩的语言从三个表格中选择,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有三张桌子 服务猫(id-猫名) 服务分类猫(id-分类猫id-分类猫名称) 子目录内容(id-子目录id-标题-内容) 我做了这件事 serviceCat public function serviceSubCat() { return $this->hasMany('ServiceSubCat'); } public function ServicesCat() { return $this->belongsTo('Se

我有三张桌子

  • 服务猫(id-猫名)
  • 服务分类猫(id-分类猫id-分类猫名称)
  • 子目录内容(id-子目录id-标题-内容)
  • 我做了这件事

    serviceCat

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    serviceSubCat

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    子内容

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    我需要从三个表中提取数据以使用它们

    这是我的控制器

    public function show($id)
        {
            $serCat = ServiceCat::where('id', $id)->first();
            $getId = $serCat->sub_status;
            if ($getId == 1) {
                $subCats = ServiceSubCat::with('ssContent')->where('cat_id', $id)->get();
                //dd($subCats);
                return View::make('portal.services.servicesDetailsList', compact('serCat', 'subCats'));
            } else {
                return View::make('portal.services.servicesDetails');
            }
        }
    
    public function show($id)
    {
        $serCat = ServiceCat::where('id', $id)->with('serviceSubCat.ssContent')->first();
        $getId = $serCat->sub_status;
        if ($getId == 1) {
            return View::make('portal.services.servicesDetailsList', compact('serCat'));
        } else {
            return View::make('portal.services.servicesDetails');
        }
    }
    
    现在我发现了一个错误

    找不到列


    我需要获取猫名、子猫名、标题、内容

    确保您的关系定义为:

    serviceCat

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    serviceSubCat

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    子内容

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    您的控制器

    public function show($id)
        {
            $serCat = ServiceCat::where('id', $id)->first();
            $getId = $serCat->sub_status;
            if ($getId == 1) {
                $subCats = ServiceSubCat::with('ssContent')->where('cat_id', $id)->get();
                //dd($subCats);
                return View::make('portal.services.servicesDetailsList', compact('serCat', 'subCats'));
            } else {
                return View::make('portal.services.servicesDetails');
            }
        }
    
    public function show($id)
    {
        $serCat = ServiceCat::where('id', $id)->with('serviceSubCat.ssContent')->first();
        $getId = $serCat->sub_status;
        if ($getId == 1) {
            return View::make('portal.services.servicesDetailsList', compact('serCat'));
        } else {
            return View::make('portal.services.servicesDetails');
        }
    }
    
    在您看来,您可以执行以下操作:

    {{ $serCat->cat_name }}
    @foreach ($serCat->serviceSubCat as $ser)
        {{ $ser->sub_cat_name }}
        @foreach ($ser->ssContent as $subCatContent)
            {{ $subCatContent->title }}
            {{ $subCatContent->content }}
        @endforeach
    @endforeach
    

    确保您的关系定义为:

    serviceCat

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    serviceSubCat

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    子内容

    public function serviceSubCat()
        {
            return $this->hasMany('ServiceSubCat');
        }
    
    public function ServicesCat()
        {
            return $this->belongsTo('ServiceCat', 'cat_id');
        }
    
        public function ssContent()
        {
            return $this->hasMany('SubCatContent');
        }
    
    public function ssCat()
        {
            return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
        }
    
    public function serviceSubCat()
    {
        return $this->hasMany('ServiceSubCat', 'cat_id');
    }
    
    public function ServicesCat()
    {
        return $this->belongsTo('ServiceCat', 'cat_id');
    }
    
    public function ssContent()
    {
      return $this->hasMany('SubCatContent', 'sub_cat_id');
    }
    
    public function ssCat()
    {
      return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
    }
    
    您的控制器

    public function show($id)
        {
            $serCat = ServiceCat::where('id', $id)->first();
            $getId = $serCat->sub_status;
            if ($getId == 1) {
                $subCats = ServiceSubCat::with('ssContent')->where('cat_id', $id)->get();
                //dd($subCats);
                return View::make('portal.services.servicesDetailsList', compact('serCat', 'subCats'));
            } else {
                return View::make('portal.services.servicesDetails');
            }
        }
    
    public function show($id)
    {
        $serCat = ServiceCat::where('id', $id)->with('serviceSubCat.ssContent')->first();
        $getId = $serCat->sub_status;
        if ($getId == 1) {
            return View::make('portal.services.servicesDetailsList', compact('serCat'));
        } else {
            return View::make('portal.services.servicesDetails');
        }
    }
    
    在您看来,您可以执行以下操作:

    {{ $serCat->cat_name }}
    @foreach ($serCat->serviceSubCat as $ser)
        {{ $ser->sub_cat_name }}
        @foreach ($ser->ssContent as $subCatContent)
            {{ $subCatContent->title }}
            {{ $subCatContent->content }}
        @endforeach
    @endforeach
    

    感谢您的重播,但出现以下错误
    未定义属性:illumb\Database\elounce\Collection::$title
    请尝试更新的答案。视图的代码中缺少一个
    @foreach
    。是的,这项工作刚刚完成。find没有考虑在
    foreach
    下创建
    foreach
    ,非常感谢@AmitThanks的重播,但出现了此错误
    未定义属性:Illumb\Database\Eloquent\Collection:$title
    尝试更新的答案。视图的代码中缺少一个
    @foreach
    。是的,这很有效,只是查找没有考虑在
    foreach
    下创建
    foreach
    ,非常感谢@Amit