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.
Related Queries
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
- Array to string conversion laravel Controller
- How to run a specific seeder class in laravel
- How to use bootstrap pagination in laravel 8
- Get last record from table in laravel
- How to display validation error in laravel
- Add class to body in laravel view
- Return redirect laravel not working
- Laravel create table migration with model
- Laravel order by date not working
- Get laravel version
- How to make Copy or Duplicate table row in laravel
- How to get the id of last record from collection object in laravel view
- Laravel API response format
- Method Illuminate\Events\Dispatcher::fire does not exist
- How to use more than one query scope in Laravel
- Create project table with model and migration
- How to send email in Laravel 11
- Send post data from controller to view
- Route [password.request] not defined
- How to check column value of a record is null or not in laravel
- How to automatically update the timestamp of parent model in Laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- How to get the random value form a specific column in laravel ?
- Insert data with form validation using ajax in laravel