I'm building a system for loaning out things like camera's, lights, etc.
I'm using laravel 9.7.0 with php 8.1.2 and a mySQL database on a remote server.
I've got three models:
item_instance.php
class Item_instance extends Model
{
use HasFactory;
public function item()
{
return $this->belongsTo(Item::class);
}
/**
* @var array<string>
*/
protected $fillable = [
'damage',
'notes',
'status',
'loan_id'
];
public function loan()
{
return $this->belongsTo(Loan::class);
}
}
User.php
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'role'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function loan()
{
return $this->belongsTo(Loan::class);
}
}
Loan.php
class Loan extends Model
{
use HasFactory;
public function user()
{
return $this->hasOne(User::class);
}
public function item_instances()
{
return $this->hasMany(Item_instance::class);
}
}
In my migrations I defined the foreignId's like such:
loans:
$table->foreignId('user_id')->nullable()->references('id')->on('users')->onDelete('cascade');
$table->foreignId('item_instance_id')->nullable()->references('id')->on('item_instances')->onDelete('cascade');
users:
$table->integer('loan_id')->nullable()->references('id')->on('loans')->onDelete('cascade');
item_instances:
$table->foreignId('loan_id')->nullable()->references('id')->on('items')->onDelete('cascade');
In my seeder I generate 1 User and a couple item_instances. After this i try to insert a loan to check if everything is working:
$loan = new Loan();
$loan->save();
$user = User::find(1);
$i1 = Item_instance::find(1);
$loan->item_instances()->save($i1);
$loan->user()->save($user);
In both the user and the item_instance the loan_id is filled in. But the item_instance_id and the user_id for the loan entry remain NULL.
What am I doing wrong? I've bin trying different things for about 3 hours and can't seem to get it working.
Thanks!
CodePudding user response:
Your loans
table is a pivot table that establishes a many-to-many relationship between Users and Item Instances.
You need fix up you relations.
App\Models\User.php:
public function item_instances() {
// loans is the pivot table. you can also use Loan::class, since you've already created the class.
return $this->belongsToMany(Item_instance::class, 'loans', 'user_id', 'item_instance_id');
}
App\Models\Item_instance.php:
public function user() {
return $this->belongsToMany(User::class, 'loans', 'item_instance_id', 'user_id');
}
In your seeder:
$user = User::find(1);
$user->item_instances()->sync([1]); // 1 is the id of item instance you want to attach to this user instance (or id)
// you can also fill additional column in the pivot table like this (not required in your case):
$user->item_instances()->sync([1 => ['extra_column_in_lonas_table' => 'foo']]);
CodePudding user response:
You haven't defined $fillable
property for Loan Model, Define $fillable
property first