Autoloading with PSR-4 uses case sensitive strings. You're mixing app\models and App\Models. Make sure the strings in belongsTo('') are the same as your namespaces and it should work.
ftiersch said:
Autoloading with PSR-4 uses case sensitive strings. You're mixing app\models and App\Models. Make sure the strings in belongsTo('') are the same as your namespaces and it should work.
Folder structure on my server is app/Models/Job.php I already tried the different variants (like a buch of times) but I'm unable to make it work. it keeps giving me the same error message, and its kind of frustrating me at the moment since this/should be very easy and straight forward
First, change your namespaces to
namespace App\Models;
Then, in your Event model, you didn't set up the relationship.
public function jobs {
return $this->hasMany('App\Models\Job');
}
Oh yeah, now I saw it. @thomastkim is correct.
You'll have to set relationships "both ways" to use them both ways in PHP. You just set the belongsTo relationship on a job so you could say
$job->event->name
but to use it the other way around you'll have to set a relationship on the Event Model to.
Also if you want to use first() you should use the following line
$e->jobs()->first()
$e->jobs gives you all the related models already read from the database. $e->jobs() gives you the querybuilder object so you can add where(), orderBy and stuff like that (but still related to the model).
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community