
How to create belongstomany relation using custom name on custom pivot table
You can create belongstomany relation on custom pivot table with custom column name by passing the custom pivot table name with their custom pivot column name. You have to pass the arguments to method custom pivot table name with custom pivot column name to belongstomany methods.
-
Create custom belongstomany relation on custom pivot table with custom column name in laravel
//Pivot table model EventSpeaker with custom table name event_speakers use Illuminate\Database\Eloquent\Relations\Pivot; class EventSpeaker extends Pivot { use HasFactory; protected $table = 'event_speakers'; } //Basically, table event_speakers does not follow the naming convention for pivot tables as per laravel naming convention as per laravel it should be (event_user). //Event model with speakers method (custom belongsToMany) class Event extends Model { use HasFactory; protected $fillable = ['name', 'timezone_id', 'user_id', 'start_date', 'end_date', 'start_time', 'end_time', 'description', 'visibility', 'is_online', 'event_address', 'event_venue', 'broadcast_link']; public function speakers() { return $this->belongsToMany(User::class, 'event_speakers', 'event_id', 'speaker_id')->using(EventSpeaker::class)->withTimestamps(); } } //Speakers belong to the users' table. Default naming conventions are used to store data in pivot tables with tablename_primaryidoftable. We have passed the custom pivot table name with custom column speaker_id to this method. //Attach method on speakers() method to insert value in the pivot table. $speakers = $request->speaker_id; $event->speakers()->attach($speakers); //There are three tables (users, events, event_speakers).
0There are two tables used to create the pivot table, users table and events table. Here speakers method (custom method name) is used as a belongstomany relationship instead of the user's method in the model. Here we created a pivot table with custom name event_speakers which is used to store the event table id and users table id with custom name speaker_id.
Here we store user id as speaker id in the pivot table which is related to the event table which works as the second table.
Random Code Snippet Queries: Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Credit card validation in laravel
- Touch parent updated_at in Laravel
- Import/Use Storage facade in laravel
- How to get records in random order in laravel
- How to update record after save method in Laravel
- How to authenticate admin users in Laravel ?
- How to fetch single row data from database in laravel
- Route group with URI prefix using middleware and route name prefixes
- Laravel 9 pagination with search filter
- How to check duplicate entry in laravel
- How to display validation error in laravel
- How to call model in blade laravel
- How to get random string in Laravel
- How to check relationship is loaded or not in Laravel
- How to pass two variables in HREF in laravel
- How to insert ckeditor data into database in Laravel?
- Create a record if not exist in laravel
- Remove array keys and values if it does not exist in other array in Laravel
- First and last item of the array using foreach iteration in laravel blade
- Laravel get single row by id
- How to display 1 day ago in comments in laravel view
- How to create new user without form submission in laravel
- How to get single column value in laravel
- Display success message in laravel