How to create belongstomany relation using custom name on custom pivot table
How to create belongstomany relation using custom name on custom pivot table
Through many examples, we will learn how to resolve the "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).
There 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.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to call model in blade laravel
- Laravel 5.4 save data to database
- Wheredate in laravel not working
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- How to restore deleted records in laravel
- How to check find method executed successfully in laravel
- How to display 1 day ago in comments in laravel view
- Trying to get property 'title' of non-object
- Laravel create default admin user
- Rendering HTML from database table to view in Laravel
- Laravel 9 route group with controller
- Get today records in Laravel
- Use withCount() to Calculate Child Relationship Records
- Extract only time from datetime in laravel
- Laravel onclick function not working
- The use statement with non-compound name 'Auth' has no effect
- How to add unique records in pivot columns of Laravel pivot table
- Laravel upload file with original file name
- Remove several global scope from query
- Sample .htaccess file and index.php file under public directory in laravel
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- How to insert value to additional columns in pivot table in laravel
- How to check email is valid or not in Laravel
- Insert current date time in a column using Laravel
- How to add active class to menu item in laravel