You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
collection search method return empty array ? i need to create a simple api search but the method return an empty array inside the controller and if i used it out the controller it's working just fine
my code
search method
public function searchForBrand(Request $request) {
$brand_name = $request->query('brand_name');
$brands = Brand::where('brand_name','like','%'.$brand_name.'%')->get();
return new BrandCollection($brands);
}
resource class
class Brand extends JsonResource
{
/**
* Indicates if the resource's collection keys should be preserved.
*
* @var bool
*/
public $preserveKeys = true;
/**
* The "data" wrapper that should be applied.
*
* @var string
*/
public static $wrap = 'brands';
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'brand_id' => $this->brand_id,
'brand_name' => $this->brand_name,
'brand_logo' => asset('storage/'.$this->brand_logo),
'cars'=>CarResources::collection($this->whenLoaded('cars')),
'created_at'=>$this->created_at,
'updated_at'=>$this->updated_at
];
}
public function with($request){
return[
'status'=>'success',
];
}
}
collection class
class BrandCollection extends ResourceCollection {
/**
* The resource that this resource collects.
*
* @var string
*/
public $collects = Brand::class;
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request) {
return ['brands' => $this->collection];
}
}
API route Route::get('/brand/search',[App\Http\Controllers\BrandController::class,'searchForBrand'])->name('searchForBrand');
the same code out of the controller working just fine
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
collection search method return empty array ? i need to create a simple api search but the method return an empty array inside the controller and if i used it out the controller it's working just fine
my code
search method
resource class
collection class
API route
Route::get('/brand/search',[App\Http\Controllers\BrandController::class,'searchForBrand'])->name('searchForBrand');
the same code out of the controller working just fine
Beta Was this translation helpful? Give feedback.
All reactions