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粉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.