
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 call controller function from view in Laravel
- Get Array of IDs from Eloquent Collection
- If condition in foreach loop in laravel
- Laravel order by date not working
- Global scope in Laravel with example
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Laravel hasmany select not working
- Class 'App\Http\Controllers\User' not found
- How to upload files to amazon s3 bucket using Laravel
- Get posts belongs to a specific user in Laravel
- How to generate .env file for laravel?
- Get the post details if it has at least one comment in comments table
- How to add a key value pair to existing array in laravel
- How to restore multiple records after soft-deletes in Laravel
- How to include header file in laravel
- Get content from web URL in laravel
- How to check relationship is loaded or not in Laravel
- Illuminate\Database\QueryException could not find driver
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Get last week data in Laravel
- Add a subselect based on relationship using withAggregate method
- Composer\Exception\NoSslException
- Generate random string lowercase in Laravel
- Laravel upload file with original file name