Php 使用Laravel模型与多个产品相关的产品有多个();属于谁?

Php 使用Laravel模型与多个产品相关的产品有多个();属于谁?,php,laravel,eloquent,Php,Laravel,Eloquent,到目前为止,我有两种不同的型号 namespace App\Models; use Illuminate\Database\Eloquent\Model; use App\User; class ProductModel extends Model { public $timestamps = true; protected $table = 'product'; /** * The attributes that are mass assignable. * * @var array

到目前为止,我有两种不同的
型号

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\User;

class ProductModel extends Model
{
public $timestamps = true;
protected $table = 'product';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'id',
    'prodTitle',
    'prodDesc',
    'prodCategory',
    'attachment',
    'prodSize',
    'prodPrice',
    'created_by',
    'is_best_seller',
    
    'prod_included',
    'prod_instruction',
];
}
ProductModel.php
这是我的产品列表

ProductRelated.php
它是为每个特定产品分配的相关产品

情况是

如果我的
ProductModel
中有
id=1
<代码>id(产品型号主键)

在我的
ProductRelated.php

prodID
(相当于
id(产品型号主键)
这将用于识别相关产品正在识别的特定产品id

prodRelatedID
这是与该产品相关的产品

因此,我的
ProductModel
将包含此数据

  id | prodTitle | prodDesc    | More fields here...
   1 | Product 1 | Description | More FIeld value here ..
   2 | Product 2 | Description | More FIeld value here ..
   3 | Product 3 | Description | More FIeld value here ..
  id | prodID | prodRelatedID | More fields here...
   1 | 1      | 2             | More FIeld value here ..
   2 | 1      |  3            | More FIeld value here ..
在my
ProductRelated
中,将有此数据

  id | prodTitle | prodDesc    | More fields here...
   1 | Product 1 | Description | More FIeld value here ..
   2 | Product 2 | Description | More FIeld value here ..
   3 | Product 3 | Description | More FIeld value here ..
  id | prodID | prodRelatedID | More fields here...
   1 | 1      | 2             | More FIeld value here ..
   2 | 1      |  3            | More FIeld value here ..
这意味着,
ProductModel
中的
id=1
有两个不同的
prodRelatedID

这些是
2和
3

我这样做的想法是使用
hasMany()

这是我的
ProductModel

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\User;

class ProductModel extends Model
{
public $timestamps = true;
protected $table = 'product';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'id',
    'prodTitle',
    'prodDesc',
    'prodCategory',
    'attachment',
    'prodSize',
    'prodPrice',
    'created_by',
    'is_best_seller',
    
    'prod_included',
    'prod_instruction',
];
}
我的
ProductRelated

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\User;

class ProductRelated extends Model
{
public $timestamps = true;
protected $table = 'product_related';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'id',
    'prodID',
    'prodRelatedID',
    'created_by'
];

}

如果您只想获得每个特定产品的所有相关产品,我认为您不需要
ProductRelated

在您的
产品模型中

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\User;

class ProductModel extends Model
{
public $timestamps = true;
protected $table = 'product';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'id',
    'prodTitle',
    'prodDesc',
    'prodCategory',
    'attachment',
    'prodSize',
    'prodPrice',
    'created_by',
    'is_best_seller',
    
    'prod_included',
    'prod_instruction',
];
}
公共功能相关产品()
{
返回$this->belongToMany(ProductModel::class,'product_related','prodID','prodRelatedID');
}
然后,您应该能够获得如下所有记录:

$relatedProducts=ProductModel::find(1)->relatedProducts()->get();

你在问什么?这听起来像是多对多。是的,如果我没有弄错的话,是多对多。我只想得到每个特定产品的所有相关产品。我能把
放在哪里()
在这里?@RaeIan是,对于产品?或相关产品?我的产品和相关产品中都有此已删除字段。尝试将其中的已删除产品等于0,已删除相关产品等于0@RaeIan
ProductModel::where('deleted',0)->relatedProducts()->where('deleted',0)->get();
这可能有效。