如何通过一次查询得到这张树表?
这是我的产品表
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('slug');
$table->foreignId('subcategory_id')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->string('title');
$table->integer('price');
$table->text('description')->nullable();
$table->timestamps();
});
这是图片表
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->string('path');
$table->timestamps();
});
这是推车桌
Schema::create('carts', function (Blueprint $table) {
$table->id();
$table->string('session_id')->nullable();
$table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->foreignId('product_id')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->integer('item_qty')->nullable(false);
$table->timestamps();
});
这就是我获取产品表的方式
$cartItems = Cart::with('product')
->get();
但是我无法获取图像表
产品型号
public function images()
{
return $this->hasMany('App\Models\Image', 'product_id');
}
$cartItems = Cart::with('product.images')->get();