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
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Conditional validation in laravel
- Validation for multiple forms on same page in laravel
- How to validate form input data in laravel
- How to access the nth object from Laravel collection object ?
- How to use bootstrap pagination in laravel 8
- Docker important commands to run laravel application with docker
- Laravel insert query not working
- Laravel append URI in route
- Send post data from controller to view
- Route not defined in Laravel
- If no route matched route::fallback in laravel
- How to get the id of last record from collection object in laravel view
- Update last created record in Laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Get duplicate records in laravel
- Check if Relationship Method Exists in Laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- Eager loading dynamically in laravel
- Laravel route parameter
- Target class [HomeController] does not exist
- Laravel pagination links with query string
- Send id with route Laravel
- Laravel form request validation
- Route prefix with auth middleware in laravel