简体   繁体   中英

Laravel RelationShips eloquent Laravel with ( 5 tables )

I have 5 tables

  • Products
    -- Categories
    --- SubCategories
    ---- Marks
    ----- Suppliers

I want to know how to make relationship I have Product Model structure with:

 namespace App\Models\Products;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;
    protected $fillable = [.... ];

    public function categorie(){
      return $this->belongTo(categories::class);
    }

    public function sub_categorie(){
      return $this->belongTo(sub_categorie::class);
    }

    public function marks(){
      return $this->belongTo(marks::class);
    }

    public function suppliers(){
      return $this->belongTo(suppliers::class);
    }
}

i want to know if it's correct? Another Question how would be looks other Model relationships such Suppliers, Categories, SubCategories...

It depends on the cardinality and the foreign_keys that are involved.

Products

-- Categories

--- SubCategories

---- Marks

----- Suppliers

The context that you gave me, I'm guessing that each table depends on the previous one as showed in the context that you gave. If so, then you can do the following:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Suppliers extends Model
{
    protected $fillable = [.... ];

    public function mark()
    {
        return $this->belongsTo(Mark::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Marks extends Model
{
    protected $fillable = [.... ];

    public function subCategorie()
    {
        return $this->belongsTo(SubCategorie::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SubCategory extends Model
{
    protected $fillable = [.... ];

    public function categorie()
    {
        return $this->belongsTo(Categorie::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Categorie extends Model
{
    protected $fillable = [.... ];

    public function categorie()
    {
        return $this->belongsTo(Product::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [.... ];
}

For more in depht inormation, you can consult the section about relationshipts on laravel docs: https://laravel.com/docs/8.x/eloquent-relationships .

Please use the correct nomeclature of relationship's, instead of "categorie" write "category" in the function name, with that laravel will automaticaly recognise the foreign key.

Example:

public function category(){
  return $this->belongTo(categories::class);
}

public function sub_category(){
  return $this->belongTo(SubCategory::class);
}

public function mark(){
  return $this->belongTo(Mark::class);
}

public function supplier(){
  return $this->belongTo(Supplier::class);
}

Show me your models folder please if you have any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM