我是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');
}
}
错误进一步说明: at Collection-&gt; __ call('with',array('parentCategory'))在categoryController.php第17行
答案 0 :(得分:0)
在<!DOCTYPE html>
<html>
<head>
<script src="https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-xpa1/t39.3284-6/11057025_805715566176382_77439371_n.js"></script>
<!-- version 0.13.3.min.js-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.29/browser.js"></script>
<title>React</title>
</head>
<body>
<script type="text/babel">
var HelloWorld = React.createClass({
render: function() {
return <div>
<h1>Hello World</h1>
<p>This is some text</p>
</div>
}
)};
React.render(<HelloWorld />, document.body);
</script>
</body>
</html>
功能中,您可以尝试
index()
这应该可以解决问题。请关注Eager Loading文档。