Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get all pivot table data for a user.
Laravel
$user = User::find(1); $pivotData = $user->roles()->[1]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using withPivot() alone does not fetch data, it only specifies pivot columns.
pivot is a property, not a method.
getPivot() is not a valid method.
✗ Incorrect
The get() method retrieves the related models including pivot data.
2fill in blank
mediumComplete the code to include the 'created_at' pivot column when fetching roles.
Laravel
$roles = $user->roles()->[1]('created_at')->get();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
selectPivot() is not a valid method.
pivotSelect() does not exist.
includePivot() is not a Laravel method.
✗ Incorrect
withPivot() specifies which pivot columns to include in the query.
3fill in blank
hardFix the error in accessing the pivot data for each role.
Laravel
foreach ($roles as $role) { echo $role->[1]->created_at; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pivotData or pivotData() which do not exist.
Trying to access pivot as a method instead of a property.
✗ Incorrect
The pivot property holds the pivot table data for the related model.
4fill in blank
hardFill both blanks to attach a role with a custom pivot value.
Laravel
$user->roles()->[1](3, ['[2]' => now()]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sync() replaces all roles instead of adding one.
updateExistingPivot() updates but does not add new role.
detach() removes roles.
✗ Incorrect
attach() adds a role with pivot data; 'created_at' is the pivot column name.
5fill in blank
hardFill all three blanks to update the pivot data for a role.
Laravel
$user->roles()->[1](3, ['[2]' => [3]]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using attach() adds a new role instead of updating.
Using wrong pivot column names.
Not using now() for current timestamp.
✗ Incorrect
updateExistingPivot() updates pivot data; 'created_at' is the column; now() sets current time.