phillipsharring's avatar

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?

0 likes
7 replies
p4bloch's avatar

Is the controller being called? Can you dd($domain) and see the results?

Juukie's avatar
Juukie
Best Answer
Level 22
public function urls()
{
        return $this->hasMany('App\Url');
}

This should be correct..

Try removing your compiled.php with php artisan clear-compiled

1 like
phillipsharring's avatar

@p4bloch - Yes, the domain was being loaded. dd($domain) shows the model. Good idea to check, though, thanks :-)

Please or to participate in this conversation.