
How to check data inserted or deleted in pivot after toggle method
How to check data inserted or deleted in pivot after toggle method
We will use programming in this lesson to attempt to solve the "How to check data inserted or deleted in pivot after toggle method".
You can easily check if data is inserted or deleted in the pivot table after the toggle method. Basically, toggle method insert data in pivot table if record with same (provided) data do not exist in it else it deletes the record from the pivot table.-
Return json response after toggle method on pivot table
$user = User::find($userId); $roleId = $request->id; $response = $user->roles()->toggle($roleId); return response()->json(['success'=>$response]);
The toggle method returns an object if the new record is created in the pivot table, else it returns 1 as a response after the toggle method deletes the existing record. Basically, the toggle method deletes the record if the record already exists in it, else insert the new record.
-
JQuery check the object is empty or not in HTML page on ajax call
//Anchor tag to get the role id on click it <a href="javascript:void(0)" class="add-role" data-id="1"> Role </a> //Call ajax on click anchor tag $('.add-role').click(function(){ var id = $(this).data('id'); $.ajax({ type:'POST', url:'/ajax-role', data:{id:id}, success:function(data){ if($.isEmptyObject(data.success.attached)){ console.log('Record is deleted from pivot table'); }else{ console.log('Record is created in pivot table'); } } }); });
You can easily check that the object is empty or not in HTML using the jquery method $.isEmptyObject(). It will return true if the data is an empty object.
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 add dynamic page title in Laravel view
- Laravel route parameter
- Insert current date time in a column using Laravel
- How to create static page in Laravel
- How to send ID to another page in Laravel
- Laravel append URI in route
- How to get id of next record in laravel
- Permanently delete a record in laravel
- OrderBy on Eloquent relationships method in Laravel
- Multiple Level eager loading in Laravel
- Laravel order by date not working
- How to print form data in laravel
- How to get session in blade Laravel ?
- How to check duplicate entry in laravel
- Call to a member function pluck() on null
- Return view from route Laravel
- Validation errors for multiple forms on same page Laravel
- Order by multiple columns in Laravel
- How to get laravel errors folder in views directory in laravel
- Laravel get count with where condition
- Use withCount() to Calculate Child Relationship Records
- How to get images from AWS s3 and display in Laravel blade
- Laravel 5.4 save data to database
- Insert Comma Separated Values in laravel
- Insert data with form validation using ajax in laravel