koko_dump
koko_dump

Reputation: 35

Passing Stored Procedure result to Laravel API Resource

I wanted to pass the result of the executed stored procedure to my PB.vue

But the problem is, it's giving me an error saying:

Call to a member function first() on array

public function index()
{
    $pbs = DB::select('EXECUTE [dbo].[spQueryContainer]');

    return PBResource::collection($pbs); 
}

Upvotes: 0

Views: 1040

Answers (1)

Charles Marceau
Charles Marceau

Reputation: 93

DB::select($query) returns an array of stdclass objects, not a collection. However, it is possible to convert it using the collect() function:

$pbs = DB::select('EXECUTE [dbo].[spQueryContainer]');

$pbCollection = collect($pbs); //Transform the array into a Laravel Collection of stdclass

return PBResource::collection($pbCollection); 

Even if the Collection contains the wrong Type (stdclass instead of PB), the resource will still use it as long as the given objects have all the attributes used in your resource.

If you want a cleaner solution, you should try to convert the stdclass object to a PB object. The topic is discussed on this post.

Upvotes: 1

Related Questions