Joe1992
Joe1992

Reputation: 468

FatalErrorException in Model.php line 750: Class not found Laravel5

I'm currently in the process of upgrading a Laravel 4.2 project to 5.0 I've been making reasonable progress until this particular error:

FatalErrorException in Model.php line 750: Class 'SAPProduct' not found.

My SapProduct class is called from my Product class with a hasOne relationship and I can't work out why Laravel can't find it.

SapProduct.php

namespace App\Models;

use Eloquent;

class SapProduct extends Eloquent
{
    public function brand()
    {
        return $this->belongsTo('App\Models\Brand', 'U_Brand', 'Name');
    }
}

Product.php

namespace App\Models;

use Eloquent;

class Product extends Eloquent
{
    ...
    public function sapProduct()
    {
        $relationship = $this->hasOne('App\Models\SapProduct', 'ItemCode', 'itemcode');
    }
    ...
}

Model.php (lines 746-755)

public function hasOne($related, $foreignKey = null, $localKey = null)
{
    $foreignKey = $foreignKey ?: $this->getForeignKey();

    $instance = new $related;

    $localKey = $localKey ?: $this->getKeyName();

    return new HasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
}

I apologise if I'm missing any other necessary information, I'll add more as/if requested.

Thanks

Upvotes: 1

Views: 1271

Answers (2)

IrfanAnwar
IrfanAnwar

Reputation: 79

If nothing seems to work and you have just stumbled upon this Question and you have just created the Model.

php artisan cache:clear

will do the magic, Along with giving the full path to the Model

 return $this->belongsTo('App\Models\YourModelClassName');

Source:: A wasted couple of hours going through the docs/posts and debugging.

Upvotes: 0

Joe1992
Joe1992

Reputation: 468

After spending far too much time on this problem I tracked it down to the offending line.

The relationship had chained through another model which had not had it's relationship to 'SapProduct' correctly namespaced.

The offending Model was ProductVariation.php and has the following incorrect line:

$relationship = $this->hasOne('SAPProduct', 'ItemCode', 'itemcode');

When it needed to be:

$relationship = $this->hasOne('App\Models\SAPProduct', 'ItemCode', 'itemcode');

This was also why laravel was bring the SapProduct error back in capitals rather than camel case.

Thanks to everybody who tried to help!

Upvotes: 1

Related Questions