属于许多具有主键关系 returns 空
Belongs To Many with pimary key relation returns null
在我的应用程序中,我想在插件和产品之间建立关系,其中一个产品可以有很多插件,
我创建了 addons table
、product table
和 addon_product
table 来保存 ids
、
已经定义了模型中的关系,甚至创建了一个模型 addonsProducts
以将密钥插入我的 controller
我希望在调用我的产品时在我的 Api resources
响应中显示插件。
插件模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Addon extends Model
{
protected $fillable = ['name','price', 'icon', 'description'];
protected $table = 'addon_products';
public function products()
{
return $this->belongsToMany(Product::class);
}
}
插件迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Addons extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addons', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('price');
$table->string('icon');
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('addons');
}
}
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name','brand', 'type', 'icon', 'price', 'description'];
// protected $table = 'products';
public function stores()
{
return $this->belongsToMany(Store::class);
}
public function orders()
{
return $this->belongsToMany(Order::class);
}
public function addons()
{
return $this->hasMany(Addon::class);
}
}
产品迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('icon');
$table->string('brand');
$table->string('type');
$table->string('price');
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
addon产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AddonProducts extends Model
{
protected $fillable = ['addon_id','product_ids', 'active'];
//protected $table = 'addon_product';
public function products()
{
return $this->belongsToMany(Product::class);
}
public function addons()
{
return $this->hasMany(Product::class);
}
}
addon产品迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddonProduct extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addon_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('addon_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('active')->unsigned();
$table->timestamps();
}); }
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('addon_product');
}
}
产品资源
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'productname' => $this->productname,
'icon' => $this->icon,
'brand' => $this->brand,
'type' => $this->type,
'price' => $this->price,
'addons' => $this->addon,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
}
}
插件资源
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class AddonResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'price' => $this->price,
'icon' => $this->icon,
];
}
}
无需在classAddon
中声明table,或正确声明并添加数据透视字段active
.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Addon extends Model
{
protected $fillable = ['name','price', 'icon', 'description'];
protected $table = 'addons';
public function products()
{
return $this->belongsToMany(Product::class)->withPivot('active');
}
}
在 Product
class 中,也将关系声明为 belongsToMany
。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name','brand', 'type', 'icon', 'price', 'description'];
public function stores()
{
return $this->belongsToMany(Store::class);
}
public function orders()
{
return $this->belongsToMany(Order::class);
}
public function addons()
{
return $this->belongsToMany(Addon::class)->withPivot('active');
}
}
您无需声明 class AddonProduct
,因为它代表联络人 table。
现在你可以简单地做:
$products = Product::with('addons')->get();
您还可以 运行 在 active
字段上设置条件。
$products = Product::with(['addons' => function($addon) {
$addon->where('pivot.active', '=', 1);
}->get();
在我的应用程序中,我想在插件和产品之间建立关系,其中一个产品可以有很多插件,
我创建了 addons table
、product table
和 addon_product
table 来保存 ids
、
已经定义了模型中的关系,甚至创建了一个模型 addonsProducts
以将密钥插入我的 controller
我希望在调用我的产品时在我的 Api resources
响应中显示插件。
插件模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Addon extends Model
{
protected $fillable = ['name','price', 'icon', 'description'];
protected $table = 'addon_products';
public function products()
{
return $this->belongsToMany(Product::class);
}
}
插件迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Addons extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addons', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('price');
$table->string('icon');
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('addons');
}
}
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name','brand', 'type', 'icon', 'price', 'description'];
// protected $table = 'products';
public function stores()
{
return $this->belongsToMany(Store::class);
}
public function orders()
{
return $this->belongsToMany(Order::class);
}
public function addons()
{
return $this->hasMany(Addon::class);
}
}
产品迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('icon');
$table->string('brand');
$table->string('type');
$table->string('price');
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
addon产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AddonProducts extends Model
{
protected $fillable = ['addon_id','product_ids', 'active'];
//protected $table = 'addon_product';
public function products()
{
return $this->belongsToMany(Product::class);
}
public function addons()
{
return $this->hasMany(Product::class);
}
}
addon产品迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddonProduct extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addon_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('addon_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('active')->unsigned();
$table->timestamps();
}); }
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('addon_product');
}
}
产品资源
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'productname' => $this->productname,
'icon' => $this->icon,
'brand' => $this->brand,
'type' => $this->type,
'price' => $this->price,
'addons' => $this->addon,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
}
}
插件资源
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class AddonResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'price' => $this->price,
'icon' => $this->icon,
];
}
}
无需在classAddon
中声明table,或正确声明并添加数据透视字段active
.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Addon extends Model
{
protected $fillable = ['name','price', 'icon', 'description'];
protected $table = 'addons';
public function products()
{
return $this->belongsToMany(Product::class)->withPivot('active');
}
}
在 Product
class 中,也将关系声明为 belongsToMany
。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name','brand', 'type', 'icon', 'price', 'description'];
public function stores()
{
return $this->belongsToMany(Store::class);
}
public function orders()
{
return $this->belongsToMany(Order::class);
}
public function addons()
{
return $this->belongsToMany(Addon::class)->withPivot('active');
}
}
您无需声明 class AddonProduct
,因为它代表联络人 table。
现在你可以简单地做:
$products = Product::with('addons')->get();
您还可以 运行 在 active
字段上设置条件。
$products = Product::with(['addons' => function($addon) {
$addon->where('pivot.active', '=', 1);
}->get();