Laravel 8 Eloquent 多对多插入枢轴 table 不工作
Laravel 8 Eloquent Many To Many insert into pivot table not working
我正在尝试将数据插入 Advertisement 和 AdvertisementCategory 之间的枢轴 table。首先,是的,在我的例子中,一个广告可以有很多类别。
有了这个:
我所有的模型都继承自我的 DatabaseObject class.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DatabaseObject extends Model
{
/**
* Disables the
* created_at and updated_at column creation
* @var bool
*/
public $timestamps = false;
}
这是我的广告模特:
<?php
namespace App\Models\Advertisement;
use App\Models\DatabaseObject;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* Basic Advertisement
*/
class Advertisement extends DatabaseObject
{
use HasFactory;
/**
* Custom table name
* @var string
*/
protected $table = 'advertisement';
public $fillable = ['name', 'description', 'sellingType', 'advertisementType', 'sellingPrice'];
/**
* A Advertisement has one AdvertisementCategory
* @return BelongsToMany
*/
public function advertisementCategories(): BelongsToMany
{
return $this->belongsToMany(AdvertisementCategory::class
, 'advertisement2advCategory'
, 'advertisementCategory_id'
, 'advertisement_id');
}
}
这是我的 AdvertisementCategory 模型:
<?php
namespace App\Models\Advertisement;
use App\Models\DatabaseObject;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class AdvertisementCategory extends DatabaseObject
{
/**
* Custom table name
* @var string
*/
protected $table = 'advertisementCategory';
/**
* A AdvertisementCategory can have many advertisements
* @return BelongsToMany
*/
public function advertisements(): BelongsToMany
{
return $this->belongsToMany(Advertisement::class
, 'advertisement2advCategory'
,'advertisement_id'
,'advertisementCategory_id');
}
}
这是我的支点 table 对象:
<?php
namespace App\Models\Advertisement;
use Illuminate\Database\Eloquent\Relations\Pivot;
class Advertisement2AdvertisementCategory extends Pivot
{
/**
* Custom name for the table
* @var string
*/
protected $table = 'advertisement2advCategory';
}
这是迁移创建:
Schema::create('advertisement2advCategory', function (Blueprint $table) {
$table->id();
$table->foreignId('advertisement_id');
$table->foreign('advertisement_id')
->references('id')
->on('advertisement')
->onDelete('cascade');
$table->foreignId('advertisementCategory_id');
$table->foreign('advertisementCategory_id')
->references('id')
->on('advertisementCategory')
->onDelete('cascade');
});
这是我在尝试从请求中插入时收到的错误:
$newAdvertisement = new Advertisement();
$newAdvertisement->name = $request->name;
$newAdvertisement->description = $request->description;
$newAdvertisement->sellingType = $request->sellingType;
$newAdvertisement->advertisementType = $request->advertisementType;
$newAdvertisement->sellingPrice = $request->sellingPrice;
$newAdvertisement->createdAt = date('Y-m-d H:i:s');
$newAdvertisement->images = $allImages;
$newAdvertisement->unlockedAt = null;
$newAdvertisement->advertisementCategories()->attach($request->selectedCategory);
$newAdvertisement->save();
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'advertisementCategory_id' cannot be null (SQL: insert into `advertisement2advCategory` (`advertisementCategory_id`, `advertisement_id`) values (?, 7))
你的问题只是操作的顺序。
您正在尝试保存多对多 realtion 的记录,而 $newAdvertisement 尚未保存,因此它没有自己的 ID(这就是您收到错误 Column 'advertisementCategory_id' cannot be null
的原因)。
先保存$newAdvertisement
然后建立关系。
$newAdvertisement->save();
$newAdvertisement->advertisementCategories()->attach($request->selectedCategory);
我正在尝试将数据插入 Advertisement 和 AdvertisementCategory 之间的枢轴 table。首先,是的,在我的例子中,一个广告可以有很多类别。 有了这个:
我所有的模型都继承自我的 DatabaseObject class.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DatabaseObject extends Model
{
/**
* Disables the
* created_at and updated_at column creation
* @var bool
*/
public $timestamps = false;
}
这是我的广告模特:
<?php
namespace App\Models\Advertisement;
use App\Models\DatabaseObject;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* Basic Advertisement
*/
class Advertisement extends DatabaseObject
{
use HasFactory;
/**
* Custom table name
* @var string
*/
protected $table = 'advertisement';
public $fillable = ['name', 'description', 'sellingType', 'advertisementType', 'sellingPrice'];
/**
* A Advertisement has one AdvertisementCategory
* @return BelongsToMany
*/
public function advertisementCategories(): BelongsToMany
{
return $this->belongsToMany(AdvertisementCategory::class
, 'advertisement2advCategory'
, 'advertisementCategory_id'
, 'advertisement_id');
}
}
这是我的 AdvertisementCategory 模型:
<?php
namespace App\Models\Advertisement;
use App\Models\DatabaseObject;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class AdvertisementCategory extends DatabaseObject
{
/**
* Custom table name
* @var string
*/
protected $table = 'advertisementCategory';
/**
* A AdvertisementCategory can have many advertisements
* @return BelongsToMany
*/
public function advertisements(): BelongsToMany
{
return $this->belongsToMany(Advertisement::class
, 'advertisement2advCategory'
,'advertisement_id'
,'advertisementCategory_id');
}
}
这是我的支点 table 对象:
<?php
namespace App\Models\Advertisement;
use Illuminate\Database\Eloquent\Relations\Pivot;
class Advertisement2AdvertisementCategory extends Pivot
{
/**
* Custom name for the table
* @var string
*/
protected $table = 'advertisement2advCategory';
}
这是迁移创建:
Schema::create('advertisement2advCategory', function (Blueprint $table) {
$table->id();
$table->foreignId('advertisement_id');
$table->foreign('advertisement_id')
->references('id')
->on('advertisement')
->onDelete('cascade');
$table->foreignId('advertisementCategory_id');
$table->foreign('advertisementCategory_id')
->references('id')
->on('advertisementCategory')
->onDelete('cascade');
});
这是我在尝试从请求中插入时收到的错误:
$newAdvertisement = new Advertisement();
$newAdvertisement->name = $request->name;
$newAdvertisement->description = $request->description;
$newAdvertisement->sellingType = $request->sellingType;
$newAdvertisement->advertisementType = $request->advertisementType;
$newAdvertisement->sellingPrice = $request->sellingPrice;
$newAdvertisement->createdAt = date('Y-m-d H:i:s');
$newAdvertisement->images = $allImages;
$newAdvertisement->unlockedAt = null;
$newAdvertisement->advertisementCategories()->attach($request->selectedCategory);
$newAdvertisement->save();
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'advertisementCategory_id' cannot be null (SQL: insert into `advertisement2advCategory` (`advertisementCategory_id`, `advertisement_id`) values (?, 7))
你的问题只是操作的顺序。
您正在尝试保存多对多 realtion 的记录,而 $newAdvertisement 尚未保存,因此它没有自己的 ID(这就是您收到错误 Column 'advertisementCategory_id' cannot be null
的原因)。
先保存$newAdvertisement
然后建立关系。
$newAdvertisement->save();
$newAdvertisement->advertisementCategories()->attach($request->selectedCategory);