Laravel HasMany关系未显示所有记录

yc0p9oo0  于 2022-11-26  发布在  其他
关注(0)|答案(3)|浏览(203)

我有两个模型电子商务订单和药品

class EcommerceOrders extends Model
{
    use HasFactory;

    protected $table = 'ecommerce_orders';

    protected $fillable = [
        'user_id',
        'manager_id',
        'first_name',
        'last_name',
        'phone_number',
        'email',
        'address',
        'city',
        'zip_code',
        'other_info',
        'products',
        'total_price',
        'qr_code',
        'status',
        'assigned_to',
        'save_client_info',
    ];

class Medicines extends Model
{
    use HasFactory;
    use Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */

    protected $table = 'medicines';
  
    protected $fillable = [
        'name',
        'subcategory_id',
        'group',
        'unity',
        'description',
        'price',
        'discount',
        'type',
        'photo_path',
        'slug'
    ];

在EcommerceOrders模型中,products字段保存客户购买的商品的信息,我尝试做的是获取这些产品的数据。
电子商务订单

public function items()
    {
        return $this->hasMany(Medicines::class,'id');
    }

药品

public function ecomOrders()
 {
        return $this->belongsTo(EcommerceOrders::class);
 }

但是,当我尝试获取数据时,它只显示产品中第一个项目的信息

EcommerceOrders::where('id',3)->with('items')->get()

Illuminate\Database\Eloquent\Collection {#3313
     all: [
       App\Models\EcommerceOrders {#3298
         id: 3,
         user_id: 2,
         manager_id: 3,
         first_name: "test",
         last_name: "test1",
         phone_number: "3550123456",
         email: "asd@test.com",
         address: "addsss",
         city: "qwerr",
         zip_code: "625",
         other_info: null,
         products: "{"2":1,"3":1}", //id: quantity
         total_price: "8309,00",
         qr_code: "2416718593exKrLbcNEpVm3nYI6S31652402",
         status: "ordered",
         assigned_to: null,
         save_client_info: 1,
         created_at: "2022-11-22 17:35:32",
         updated_at: "2022-11-22 17:35:32",
         items: Illuminate\Database\Eloquent\Collection {#3315
           all: [
             App\Models\Medicines {#3327
               id: 3,
               group: 11,
               name: "quisquam",
               subcategory_id: 8,
               unity: "499mg",
               description: "Quo autem aut quibusdam dolorem aut sit.",
               price: 6935.0,
               discount: 9.0,
               type: 0,
               photo_path: "https://via.placeholder.com/640x480.png/009900?text=laudantium",
               slug: "quisquam",
               created_at: "2022-11-21 11:39:29",
               updated_at: "2022-11-21 11:39:29",
               deleted_at: null,
             },
           ],
         },
       },
     ],
   }
lqfhib0f

lqfhib0f1#

EcommerceOrders::where('id',3)->with('items')->get()

此查询仅获取id为3的行,由于它是并递增ID,因此只有1项的ID为3

zdwk9cvp

zdwk9cvp2#

//这是检索hasMany数据的方法
查找(1)-〉商品;
foreach($个项目作为$个项目){ //
}

dxxyhpgq

dxxyhpgq3#

//这就是如何在不同的表中检索hasMany数据。
用户名:用户名:用户名:用户名:用户名
foreach($个项目作为$个项目){
//
}

相关问题