Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel many to many relationship with pivot table

$user = User::find(1); //any user we want to find 
$user->trophies()->attach($idOfTrophy); 
//pass id or array of a Trophy ids 
//suppose admin has selected the trophy from a form and trophy id
// is in $request object, then.
$trophyId = $request->trophy_id;
$user->trophies()->attach($trophyId); //record is created in DB.
attach and syncWithoutDetaching both does same job
Source by medium.com #
 
PREVIOUS NEXT
Tagged: #laravel #relationship #pivot #table
ADD COMMENT
Topic
Name
6+3 =