我正在嘗試將資料插入到Advertisement和AdvertisementCategory之間的資料透視表中。首先,是的,就我而言,一個廣告可以有多個類別。有了這個:
我所有的模型都繼承自我的DatabaseObject類。
<?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');
}
}
這是我的廣告類別模型:
<?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');
}
}
這是我的資料透視表物件:
<?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))
uj5u.com熱心網友回復:
您的問題只是操作順序。您正在嘗試保存多對多房地產的記錄,而 $newAdvertisement 尚未保存,因此它沒有自己的 ID(這就是您收到錯誤的原因Column 'advertisementCategory_id' cannot be null)。
首先保存$newAdvertisement,然后建立關系附加。
$newAdvertisement->save();
$newAdvertisement->advertisementCategories()->attach($request->selectedCategory);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398929.html
