简体   繁体   English

Laravel RelationShips eloquent Laravel 与(5 表)

[英]Laravel RelationShips eloquent Laravel with ( 5 tables )

I have 5 tables我有 5 张桌子

  • Products产品
    -- Categories -- 分类
    --- SubCategories --- 子类别
    ---- Marks - - 分数
    ----- Suppliers ----- 供应商

I want to know how to make relationship I have Product Model structure with:我想知道如何与产品 Model 结构建立关系:

 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...另一个问题将如何看待其他 Model 关系,例如供应商、类别、子类别......

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 .有关详细信息,您可以查阅 laravel 文档中有关关系的部分: 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.请使用正确的关系命名法,而不是“类别”,在 function 名称中写“类别”,laravel 将自动识别外键。

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.如果您有任何问题,请告诉我您的模型文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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