
Laravel create multiple records in Pivot table
You can create multiple records in pivot table in Laravel. You have to call attach method on relationship method with array of ids which create multiple records with additional column.
Answers 2
-
Create multiple records in pivot table with additional column using attach method
--PATH routes\web.phpRoute::get('/create-multiple-pivot-record', function(){ $user = App\Models\User::findOrFail(1); $user->projects()->attach([ 1 => ['is_manager' => false], 2 => ['is_manager' => true], ]); });
0This code snippet will create multiple records in pivot table in laravel. It will create two records in pivot table with different value in additional column of pivot table. You can pass dynamic value to additional column (is_manager) as well as project id instead of 1 and 2.
-
Create multiple records in pivot table with attach method
--PATH routes\web.phpRoute::get('/create-multiple-pivot-record', function(){ $user = App\Models\User::findOrFail(1); $user->projects()->attach([1,2]); });
0This code snippet is used to create multiple records in pivot table in Laravel.
Random Code Snippet Queries: Laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Ignore Records where a field has NULL value in Laravel
- The use statement with non-compound name 'DB' has no effect
- How to add dynamic page title in Laravel view
- Return view from route Laravel
- How to use bootstrap pagination in laravel 8
- Update if exist else insert new record in laravel
- Delete file from amazon s3 bucket using Laravel
- How to call controller function from view in Laravel
- How to check records exist in loaded relationship in Laravel blade view
- Insert data with form validation using ajax in laravel
- How to get selected categories on edit record with Select2
- Laravel 5.4 save data to database
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- How to create projects method with belongstomany relationship in user model
- How to get query string value in laravel
- How to restore multiple records after soft-deletes in Laravel
- Order by multiple columns in Laravel
- Drop foreign key column in Laravel using migration
- Get duplicate records in laravel
- How to validate URL with https using regex in laravel
- How to validate form input data in laravel
- Get id of last inserted record in laravel
- Laravel csrf token mismatch for ajax POST Request
- Redirect to another view from controller in laravel