Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

php - 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 AppModelsProducts;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

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

question from:https://stackoverflow.com/questions/65908019/laravel-relationships-eloquent-laravel-with-5-tables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 AppModels;

use IlluminateDatabaseEloquentModel;

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

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

namespace AppModels;

use IlluminateDatabaseEloquentModel;

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

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

namespace AppModels;

use IlluminateDatabaseEloquentModel;

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

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

namespace AppModels;

use IlluminateDatabaseEloquentModel;

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

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

namespace AppModels;

use IlluminateDatabaseEloquentModel;

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...