调用未定义的方法Illuminate

调用未定义的方法Illuminate

我想创建一个带有子菜单的动态菜单。
将是Level1,Level2,Level3。

但是我得到这个错误:
调用未定义的方法Illuminate \ Database \ Query \ Builder :: has_many()

调用未定义的方法Illuminate \ Database \ Query \ Builder :: has_many()(视图: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'));
});`


视图`

<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>

最佳答案

错误提示:


  调用未定义的方法Illuminate \ Database \ Query \ Builder :: has_many()


因此,将has_many替换为hasMany(将belongs_to替换为belongsTo

10-08 13:59