Using Laravel 5, btw.
Can't Find Related Model
I'm trying to load a one to many relationship. Domains has more than one Url. I'm getting this error:
[Fri Nov 28 19:53:15 2014] PHP Fatal error: Class 'Url' not found in /Users/phil/Code/Context/storage/framework/compiled.php on line 8265 [Fri Nov 28 19:53:15 2014] ::1:50888 [500]: /domains/show/1 - Class 'Url' not found in /Users/phil/Code/Context/storage/framework/compiled.php on line 8265
Here's the models...
// app/Domain.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
class Domain extends Eloquent
{
use SoftDeletes;
protected $fillable = [ 'domain' ];
public function urls()
{
return $this->hasMany('Url');
}
}
// app/Url.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
class Url extends Eloquent
{
use SoftDeletes;
protected $fillable = [ 'url', 'title' ];
}
Url: /domains/show/1
And here's my controller method that's causing the error.
// app/Http/Controllers/DomainsController.php
public function show(Domain $domain, $id)
{
$domain = $domain->find($id);
$urls = $domain->urls;
dd($urls);
}
I've tried:
// in the Domain model urls method
return $this->hasMany('App\Url');
// and
return $this->hasMany('\App\Url');
// having a use statement for App\Url in Domain.php model...
As well as doing $ php artisan dump-autoload and $ composer dump-autoload.
My composer.json file correctly has the App namespace mapped to the app directory w/ PSR-4.
What am I doing wrong? This seems like one of those "oughta work out of the box type things," so I'm sure I'm missing something. Thoughts?
public function urls()
{
return $this->hasMany('App\Url');
}
This should be correct..
Try removing your compiled.php with php artisan clear-compiled
Please or to participate in this conversation.