Home  >  Q&A  >  body text

How to merge two query results and display them on resource table in Laravel Nova

I'm trying to merge the results of two queries in Laravel Nova. I've read the documentation but haven't found a solution yet. Basically, I want to merge two query results and display them in a resource table.

I tried overriding the indexQuery method but failed. refer to

public static function indexQuery(NovaRequest $request, $query){
     $query_1 =  Model::where('some condition')->get();
     $query_2 = Model2::where('some condition')->get();
     //merge both queries result
     $result = $query_1->merge($query_2);
     return $result
}

P粉755863750P粉755863750251 days ago368

reply all(1)I'll reply

  • P粉744691205

    P粉7446912052024-01-17 15:17:11

    You can try the following, although the way to do it in nova is weird:

    $query_1 = Model::where('some condition')->get()->toArray();
    $query_2 = Model2::where('some condition')->get()->toArray();
    
    $result = collect(array_merge($query_1, $query_2));

    I prefer dd($result); before passing it back to the field to ensure the field is built based on the new collection. You can view the results in the Network tab.

    reply
    0
  • Cancelreply