I have two model: Colleague and Work.
Colleague.php
class Colleague extends Model
{
use HasFactory, AsSource, Filterable, Attachable;
protected $primaryKey = 'slug';
protected $keyType = 'string';
public $incrementing = false;
public function works() : BelongsToMany
{
return $this->belongsToMany(Work::class);
}
}
Work.php
class Work extends Model
{
use HasFactory, AsSource, Filterable, Attachable;
public function colleagues() : BelongsToMany
{
return $this->belongsToMany(Colleague::class);
}
}
I have migrations for Work, Colleague and a table for colleague_work. colleagues migration
Schema::create('colleagues', function (Blueprint $table) {
$table->string('slug');
$table->string('secondName');
$table->string('firstName');
$table->string('lastName');
$table->string('jobTitle');
$table->string('socialLinkName');
$table->string('socialLink');
$table->string('avatar');
$table->string('cover');
$table->timestamps();
});
works migration
Schema::create('works', function (Blueprint $table) {
$table->increments('id');
$table->string('workTitle');
$table->string('workCompany');
$table->string('workType');
$table->string('workThumbnail');
$table->string('workSizeType');
$table->timestamps();
});
and colleague_work table
Schema::create('colleague_work', function (Blueprint $table) {
$table->id();
$table->integer('work_id');
$table->string('colleague_slug');
$table->timestamps();
});
In Laravel Orchid I create a resource WorkResource for creating and editing the works.
WorkResource.php
class WorkResource extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = Work::class;
/**
* Get the fields displayed by the resource.
*
* @return array
*/
public function fields(): array
{
return [
Input::make('workTitle')
->required()
->title('Work name')
->placeholder('Work Name'),
Input::make('workCompany')
->required()
->title('Nickname')
->placeholder('24mkk'),
Input::make('workType')
->required()
->title('Work type')
->placeholder('site'),
Picture::make('workThumbnail')
->required()
->title('Thumbnail'),
Relation::make('colleagues.')
->fromModel(Colleague::class, 'slug')
->multiple()
->title('Colleagues'),
Select::make('workSizeType')
->required()
->title('Work size type')
->options([
'sm' => 'Small',
'md' => 'Medium',
'xl' => 'Big',
]),
];
}
/**
* Get the columns displayed by the resource.
*
* @return TD[]
*/
public function columns(): array
{
return [
TD::make('id'),
TD::make('created_at', 'Date of creation')
->render(function ($model) {
return $model->created_at->toDateTimeString();
}),
TD::make('updated_at', 'Update date')
->render(function ($model) {
return $model->updated_at->toDateTimeString();
}),
];
}
/**
* Get the sights displayed by the resource.
*
* @return Sight[]
*/
public function legend(): array
{
return [];
}
/**
* Get the filters available for the resource.
*
* @return array
*/
public function filters(): array
{
return [];
}
}
When I create new work I have this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'colleague' in 'field list'
insert into
works (
workTitle,
workCompany,
workType,
workThumbnail,
colleague,
workSizeType,
updated_at,
created_at) values (Work1, 24mkk, site, 123, ?, sm, 2024-03-20 18:28:13, 2024-03-20 18:28:13)
What I need to do for adding many-to-many right?