mardi 5 décembre 2017

Multi level relationship in Laravel Eloquent

I am not quite sure if the question title makes any sense.

I have a total of 6 Models

Type, Servicetype, Brand, Modelno, Problem, Problemprice

Database Example:

types has Services and Products

servicetypes has Mobile, Laptop, Tablets

brands has Apple, Dell, LG

modelnos has iPhone6, Inspiron, 2525

problems has all the problems that could occur like Heating Issue, Screen Issue

problemprices has problems_id, modelno_id and price of to solve the problem.

This is how I have defined the relationship of my models:

Type.php

class Type extends Model
{
    public function servicetypes()
    {
        return $this->hasMany('App\Servicetype');
    }
}

Servicetype.php

class Servicetype extends Model
{
    public function types()
    {
        return $this->belongsTo('App\Type');
    }

     public function brands()
    {
        return $this->hasMany('App\Brand');
    }
}

Brand.php

class Brand extends Model
{
    public function servicetypes()
    {
        return $this->belongsTo('App\Servicetype');
    }

     public function modelnos()
    {
        return $this->hasMany('App\Modelno');
    }
}

Modelno.php

class Modelno extends Model
{
    public function brands()
    {
        return $this->belongsTo('App\Brand');
    }

     public function problemprices()
    {
        return $this->hasMany('App\Problemprice');
    }
}

Problem.php

class Problem extends Model
{
    public function problemprices()
    {
        return $this->hasMany('App\Problemprices');
    }
}

Problemprice.php

class Problemprice extends Model
{
    public function problems()
    {
        return $this->belongsTo('App\Problem');
    }

    public function modelnos()
    {
        return $this->belongsTo('App\Modelno');
    }
}

All the models are defined with hasMany and belongsTo

My Question Is: Is this the correct way to define these kinds of relationship? Or Many-to-Many relationsihip (belongsToMany) has to be used? Any suggestions or insights are highly appreciated :)

P.s The code works fine.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire