scorpions78
scorpions78

Reputation: 558

Relations in laravel with model

The first i´m sorry for this question, i know that maybe it´s very bad. I´m reading much for relation with model in laravel but i don´t understand i don´t know to apply.

my problem it´s:

i have one Model session and i have one model patient one patient receive one or many session and one session it´s for one patient, ok¿?? My application it´s for physiotherapy clinic.

in my model Session i have this:

public function patient()
    {
        return $this->hasOne(Patient::class, 'id');
    }

and in my model patient i don´t know if i have to to do anything or i have to to do this:

/**
     * @return \Illuminate\Database\Eloquent\Relations\HasMnay
     **/

    public function sesiones()
    {
        return $this->hasMany(\App\Sesion::class, 'id');
    }

one patient can get one or any sessions and one session it´s for one pattient i have more relations but i need to know to do one for to do next.

i´m doing this question, because my application web, returned this error:

 STATUS_BREAKPOINT

and i think that my relations it´s bad and hang up my application

Thanks so much for help. And sorry for my question

updated

if i want to show doctor in sessions... I have this:

in model Session:

public function doctor(){
        return $this->belongsTo(Doctor::class);
    }

and in model Doctor

public function Doctor(){
        return $this->belongsTo(Session::class);
    }

but i can´t show in my table doctor that did this session

Upvotes: 0

Views: 305

Answers (1)

Jacky.S
Jacky.S

Reputation: 394

Based on your description, your eloquent relationship definition seems fine, although you don't need to specify the foreign key 'id' as it is referenced by default, the only modification I would make is maybe you could try to set the relationship as one patient has many sessions, and one session belongs to one patient

So in session model:

public function patient()
{
    return $this->belongsTo(Patient::class);
}

and then in patient model:

public function sessions()
{
    return $this->hasMany(Session::class); 
}

Updated: For doctor model:

public function sessions()
{
    return $this->hasMany(Session::class);
}

and for Session Model:

public function doctor()
{
    return $this->belongsTo(Doctor::class);
}

Although I don't think this is the reason to cause the error, just would make it easier to define foreign keys and relationships. Your status breakpoint error may cause by some other issues if you could describe more your workflow and how you get to that error

Upvotes: 1

Related Questions