I'm trying to insert data into the pivot table between Advertisement and AdvertisementCategory. First of all, yes in my case one advertisement can have many categories. With this out of the way:
All my Models inherit from my 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;
}
Here is my Advertisement model:
<?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');
}
}
Here is my AdvertisementCategory model:
<?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');
}
}
And this is my pivot table object:
<?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';
}
And this is the migration creation:
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');
});
This is the error I receive while trying to insert like this from a request:
$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))
CodePudding user response:
Your problem is just the order of operations.
You are trying to save record of many to many realtion while the $newAdvertisement is not saved yet, so it does not have own ID (Thats why you are getting error Column 'advertisementCategory_id' cannot be null
).
First save $newAdvertisement
and than make relationship attaching.
$newAdvertisement->save();
$newAdvertisement->advertisementCategories()->attach($request->selectedCategory);