Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

garrettmassey's avatar

Class not found, but namespaces are correct, class exists, and composer dump-autoload works

I am trying to use the with method for Eloquent, but I keep getting the error that Class 'App/Models/Assignments' not found. I have checked my namespaces, and it all is set up properly as best as I can see, and I have run composer dump-autoload and composer dump-autoload -o, along with php artisan cache:clear, to no avail.

Here is my "Assignments" model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Assignments extends Model
{
    protected $table = 'assignments';
    protected $fillable = ['*'];

    public function questions() {
        return $this->hasMany('App/Models/Questions');
    }
}

And here is my "ProvidersAssignments" model that is trying to access the "Assignments" model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProvidersAssignments extends Model
{
    protected $table = 'providers_assignments';

    protected $fillable = [
        'assignments_id',
        'total',
        'grade',
    ];

    public function provider() {
        return $this->belongsTo('App/Models/Provider', 'providers_id');
    }
    public function assignment() {
        return $this->belongsTo('App/Models/Assignments');
    }
}

And finally, here is the part of the controller that is using with to load ProvidersAssignments::with(['assignment'])->get();

<?php

namespace App\Http\Controllers;

/* core */
use Illuminate\Http\Request;
use Illuminate\Validation\Validator;
use Ixudra\Curl\Facades\Curl;

/* traits */
use App\Traits\Grader;

/* models */
use App\Models\Provider;
use App\Models\ProgressReport;
use App\Models\Assignments;
use App\Models\ProvidersAssignments;

class ProgressReportController extends Controller
{

    use Grader;

    public function show($id)
    {

        $provider = Provider::find($id);

        $reportcard = $this->createReportcard($provider);

        $assignments = ProvidersAssignments::with('assignment')->where('providers_id', $id)->get();

        return view('view.name', [
            'provider' => $provider,
            'reportcard' => $reportcard,
            'assignments' => $assignments,
        ]);

    }
}

Like I said, I have triple checked the namespaces, and I have run php artisan cache:clear and composer dump-autoload and neither seems to fix the error.

0 likes
3 replies
Nakov's avatar

You should also rename your method to this:

public function assignments() {
        return $this->belongsTo('App/Models/Assignments');
    }

since you are using a plural version in with('assignments'). But following Laravel's convention I would've used singular form of all the models, like Assignment that way you can avoid setting the $table as well.

Make sure also that your file is not Assignment.php but it is Assignments.php

tykus's avatar
tykus
Best Answer
Level 104

Backslashes:

 public function provider() {
    return $this->belongsTo('App\Models\Provider', 'providers_id');
}

public function assignments() {
    return $this->belongsTo('App\Models\Assignments');
}

Or, since you are already in the same namespace:

 public function provider() {
    return $this->belongsTo('Provider', 'providers_id');
}

public function assignments() {
    return $this->belongsTo('Assignments');
}
garrettmassey's avatar

@tykus this was it!

I've been working on setting all of this up for so long I didn't even realize the slashes were in the wrong direction.

Thank you!

Please or to participate in this conversation.