0
0
Laravelframework~20 mins

Many-to-many (belongsToMany) in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Many-to-many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel Eloquent many-to-many relationship code?
Consider two models, User and Role, connected by a many-to-many relationship using belongsToMany. Given the code below, what will return $user->roles->pluck('name'); output if the user has roles 'Admin' and 'Editor'?
Laravel
<?php
class User extends Model {
    public function roles() {
        return $this->belongsToMany(Role::class);
    }
}

$user = User::find(1);
return $user->roles->pluck('name');
Anull
B["Admin"]
C["Admin", "Editor"]
D[]
Attempts:
2 left
💡 Hint
Remember that belongsToMany returns a collection of related models.
state_output
intermediate
2:00remaining
What is the value of $pivot after accessing a many-to-many relationship?
In Laravel, when accessing a many-to-many relationship, the intermediate table data is available via the pivot property. Given the code below, what will return $user->roles->first()->pivot->created_at; output?
Laravel
<?php
$user = User::find(1);
// Assume roles relationship is defined with timestamps on pivot
return $user->roles->first()->pivot->created_at;
Anull
BAn error because pivot does not exist
CThe user's created_at timestamp
DThe timestamp when the role was attached to the user (e.g., '2024-04-01 12:00:00')
Attempts:
2 left
💡 Hint
Pivot contains data from the intermediate table, including timestamps if enabled.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a many-to-many relationship with a custom pivot table name?
You want to define a many-to-many relationship between Post and Tag models using a pivot table named post_tags. Which code snippet correctly sets this up?
Apublic function tags() { return $this->belongsToMany(Tag::class, 'post_tags'); }
Bpublic function tags() { return $this->belongsToMany(Tag::class)->using('post_tags'); }
Cpublic function tags() { return $this->belongsToMany('post_tags'); }
Dpublic function tags() { return $this->belongsToMany(Tag::class, 'tags_posts'); }
Attempts:
2 left
💡 Hint
The second parameter of belongsToMany is the pivot table name.
🔧 Debug
advanced
2:00remaining
Why does this many-to-many relationship query cause an error?
Given the code below, why does calling $user->roles()->attach(5); cause an error?
Laravel
<?php
class User extends Model {
    public function roles() {
        return $this->belongsToMany(Role::class);
    }
}

$user = User::find(1);
$user->roles()->attach(5);
AThe pivot table does not exist or is misnamed, so attach fails.
BThe <code>roles</code> table does not have a primary key.
CThe <code>roles</code> relationship is missing the <code>withTimestamps()</code> method.
DThe <code>attach</code> method requires an array, not a single ID.
Attempts:
2 left
💡 Hint
Check if the pivot table exists and is named correctly.
🧠 Conceptual
expert
2:00remaining
How does Laravel determine the pivot table name in a many-to-many relationship by default?
When you define a many-to-many relationship using belongsToMany without specifying a pivot table name, how does Laravel decide which table to use?
AIt uses a table named 'pivot' by default.
BIt uses the alphabetical order of the two related model table names joined by an underscore.
CIt uses the name of the first model's table with '_pivot' appended.
DIt requires you to always specify the pivot table name explicitly.
Attempts:
2 left
💡 Hint
Think about how Laravel names pivot tables conventionally.