【问题标题】:BadMethodCallException in Macroable.php line 81: Method with does not exist. Laravel ORMMacroable.php 第 81 行中的 BadMethodCallException:不存在的方法。 Laravel ORM
【发布时间】:2016-07-21 19:09:13
【问题描述】:

我是 laravel 的新手,目前使用 ORM 我正在​​尝试使用类别表中存在的外键从 parentCategory 表中检索数据。

以下是我的类别模型中的代码:

  <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class category extends Model
{
    protected $table='category';

    public function parentCategory(){

        return $this->belongsTo('App\parentCategory','mCategoryId');

    }

}

以下是我的 parentCategory 型号代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class parentCategory extends Model
{
    protected $table='maincategory';



   public function categories(){

        return $this->hasMany('App\category');

    }

}

以下是我用来检索数据的 categoryController 的代码:

class categoryController extends Controller
{
    function index(){

        $category=category::all()->with('parentCategory');
        $parentCategory=parentCategory::all();
        return view('admin.category',['categories'=>$category,'parentCategories'=>$parentCategory]); 

    }

    function add(Request $request){

        $category= new category();
        $category->categoryName=$request["name"];
        $category->mCategoryId=$request["parentCategory"];      
        $category->save();
        return redirect()->route('category');

    }
}

错误进一步说明: 在 Collection->__call('with', array('parentCategory')) 在 categoryController.php 第 17 行

【问题讨论】:

  • 你使用的是哪个 Laravel 版本?

标签: laravel orm eloquent


【解决方案1】:

在你的index()函数中,你可以试试

$category = category::with('parentCategory')->get();

这应该可以解决问题。遵循Eager Loading 文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2017-03-28
    • 1970-01-01
    • 2015-12-17
    相关资源
    最近更新 更多