Skrudox
Skrudox

Reputation: 939

laravel eager loading with "with"

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Place extends Model
{
    public function parent()
    {
        return $this->belongsTo('place', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('place', 'parent_id');
    }
}

this is my model

and here in my controller I try to make a load with "with", but after I call the get() method, I get an error:

Class 'place' not found

this is how I do it:

    $data['places'] = Place::with('children', 'parent')->get();

the class Itself is there, but this happens only if I call the get() or find() methods any clues?

Upvotes: 0

Views: 163

Answers (2)

arun
arun

Reputation: 4815

Your relationships are wrong, use App\Place note App and capital P in place.

class Place extends Model
{
    public function parent()
    {
        return $this->belongsTo('App\Place', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('App\Place', 'parent_id');
    }
}

Upvotes: 1

DsRaj
DsRaj

Reputation: 2328

Change it like this, as in you need to mention the namespace and model, In both function 'App\Place'

public function parent()
{
   return $this->belongsTo('App\Place', 'parent_id');
}

Place::with(['children', 'parent'])->get();
OR
Place::with('children')->with('parent')->get();

Upvotes: 1

Related Questions