Laravel 8 & Yajra Datatables complicated search

I have a datatable that fetches orders and is working and displaying properly. I show the orders to the users that initiated them. Then they can only search on their owns orders. Now I need to display a message to a user, if an order was found but it was initiated by another user, instead of displaying an empty result in the datatable. This will happen after typing in the search box and not when loading the datatable in the beggining. The problem is that the query already filters the results by user id so I cannot change it during manual search.

I can display code if needed but the function is quite big and I don’t really need code but the logic/way of doing that.

Do you have any suggestions on how I could accomplish this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well, maybe not the best way to do it but that’s how I solved it:

In the controller, I check for the search field, and run a query on the relationship but only on the orders that have different seller than the logged in user:

        $otherSeller = "";
        if(!empty($request->search['value']))
        {
            $ordersOtherSeller = Order::where('status_id', "!=", 3)->where('seller_id', '!=', $loggedUser->id)->whereHas('user', function ($q) use ($request){               
            $q->searchFullName($request->search['value']);
            })->first();  

            if($ordersOtherSeller != NULL && $ordersOtherSeller->count() > 0)        
                $otherSeller = $ordersOtherSeller->user->full_name . ' ' . $ordersOtherSeller->seller->full_name;       
        }

And I set a custom variable with the table’s json:
...
->with('otherSeller', $otherSeller)          
->make(true);

Then on the datatable jquery drawCallBack, I check for a populated string that guarantees that the result is not returned by a query from the current user:
            fnDrawCallback: function( oSettings ) {             
            var api = this.api();   
            if(api.ajax.json().otherSeller != "") 
            {
                $('#alert-info span').text(api.ajax.json().otherSeller);
                $('#alert-info').show();
            }
            else
                $('#alert-info').hide();                
        },

And last is the toggling of the materialert element with updated text:
  <div id="alert-info" class="materialert info" style="display: none;">
      <i class="material-icons">info</i> <span></span>
      <button type="button" class="close-alert">×</button>
  </div>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x