我想创建一个带有子菜单的动态菜单。 将是 Level1、Level2、Level3。
但我收到此错误: 调用未定义的方法 Illuminate\Database\Query\Builder::has_many() 和 调用未定义的方法 Illuminate\Database\Query\Builder::has_many() ( View :C:\xampp\htdocs****\resources\views\test.blade.php)
这是我到目前为止所拥有的:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class WorkoutLevel1 extends Model
{
protected $table ='workout_level1s';
protected $fillable = ['title','icon','order_no'];
public function workoutlvl2(){
return $this->has_many('WorkoutLevel2');
}
}`
`
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class WorkoutLevel2 extends Model
{
protected $table = 'workout_level2s';
public function workoutlvl1(){
return $this->belongs_to('WorkoutLevel1');
}
public function workoutlvl3(){
return $this->has_many('WorkoutLeve3');
}
protected $fillable = ['title','order_no'];
}
`
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class WorkoutLevel3 extends Model
{
protected $table = 'workout_level3s';
public function workoutlvl2(){
return $this->belongs_to('WorkoutLevel2');
}
protected $fillable = ['title','order_no'];
}
路线:
Route::get('/menu', function () {
$lvl1s = App\WorkoutLevel1::all();
return View::make('test',compact('lvl1s'));
});`
View `
<div class="categories">
<!-- // left menu.... -->
<ul class="main">
@foreach($lvl1s as $lvl1)
<li><a href="">{{$lvl1->title}}</a></li>
@foreach($lvl1->workoutlvl2->take(2) as $lvl2)
<li><a href="">{{$lvl2->title}}</a></li>
@endforeach
@endforeach
</ul>
</div>
最佳答案
错误提示:
Call to undefined method Illuminate\Database\Query\Builder::has_many()
因此,将 has_many
替换为 hasMany
(并将 belongs_to
替换为 belongsTo
)
关于php - 调用未定义的方法 Illuminate\Database\Query\Builder::has_many(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505620/