Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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.

0

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

0

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');
}
Last updated 8 years ago.
0

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).

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Thyvo thyvo Joined 22 Aug 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.