0

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); 
}
vimuth
  • 5,064
  • 33
  • 79
  • 116
koko_dump
  • 35
  • 9

1 Answers1

1

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.

Charles Marceau
  • 93
  • 1
  • 2
  • 7
  • Thanks, it worked but right now it showed me "undefined method stdClass::toBase()" am I still missing something to include on my controller ? – koko_dump May 25 '19 at 04:41
  • Maybe you should try converting the stdclass to PB objects. You can do this in a few steps: 1.[Convert stdClass object to an array](https://stackoverflow.com/questions/18576762/php-stdclass-to-array). 2.Passing the array as a constructor parameter for you PB Model ([Check this article about the $fillable property](https://lavalite.org/blog/laravel-mass-assignment-fillable-or-guarded)). – Charles Marceau May 25 '19 at 06:00
  • Alternatively, you could simply map the attributes from your stdClass objects to PB. – Charles Marceau May 25 '19 at 06:08