【问题标题】:Laravel HasMany relationship not displaying all recordsLaravel HasMany 关系不显示所有记录
【发布时间】:2022-11-23 23:37:11
【问题描述】:

我有两个模型 EcommerceOrders 和 Medicines

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 模型中,产品字段包含有关客户购买的商品的信息 我想做的是获取有关这些产品的数据。这就是我在每个模型中创建关系的方式

电商订单

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,
             },
           ],
         },
       },
     ],
   }

【问题讨论】:

  • 您可以编辑问题并添加查询吗?
  • 这是我的查询 EcommerceOrders::where('user_id', auth()->id)->with('items')->get()

标签: laravel eloquent eloquent-relationship


【解决方案1】:
EcommerceOrders::where('id',3)->with('items')->get()

此查询仅获取 id 为 3 的行 并且因为它是和递增 ID,所以只有 1 项将是 ID:3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2015-04-16
    • 2015-10-20
    • 1970-01-01
    • 2013-12-13
    相关资源
    最近更新 更多