Belhedi
269
3
Laravel

Retrieve data with eloquent relationships

Posted 3 months ago by Belhedi

Hi guys.. I have a model called (Departement) which have a one to many relationship with the (Teacher) model :

class Departement extends Model
{
    protected $fillable = [
    'name', 'speciality', 'faculty_id'];

    public function teachers()
    {
        return $this->hasMany(Teacher::class);
    }



}

And the Departement model has a one to many relationship with the Faculty model :

class Faculty extends Authenticatable
{
    public function departements()
    {
        return $this->hasMany(Departement::class);
    }
}

I use in my application the faculty as a user which can login and logout , then i can add a teacher in my teachers table when i was connected as a faculty , i would to attach the departement id in my teaches table So in the register form i listed the departements names and when i select mathematic departement which has an id = 3 , in the teachers table this value stored in the departements_id column . I did that but it doesn't work:

'departement_id' => Faculty::find($facultyId)->departements()->where('name', $request['departementName'])->get()

Please sign in or create an account to participate in this conversation.