codi05ro codi05ro - 4 months ago 42
PHP Question

Call to undefined method Illuminate\Database\Query\Builder::has_many()

I want to create a dynamic menu, with sub menus.
Will be Level1, Level2, Level3.

But I get this error:
Call to undefined method Illuminate\Database\Query\Builder::has_many()
and
Call to undefined method Illuminate\Database\Query\Builder::has_many() (View: C:\xampp\htdocs****\resources\views\test.blade.php)

This is what I have so far:

<?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'];
}


The route:

Route::get('/menu', function () {
$lvl1s = App\WorkoutLevel1::all();

return View::make('test',compact('lvl1s'));
});`


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

Answer

The error says:

Call to undefined method Illuminate\Database\Query\Builder::has_many()

So, replace has_many with hasMany (and belongs_to with belongsTo)

Comments