【发布时间】: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 版本?