Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Resaizu's avatar

Laravel apiResource: show, update, delete method not working

I'm figuring this out on why it always give me an empty array while the other controllers give me the result i want.

Booking Model:

use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
       'status_id'
    ];

    /**
     * The relationships that should always be loaded.
     *
     * @var array
     */
    protected $with = ['status'];

BookingController (not working):

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Booking  $booking
     * @return \Illuminate\Http\Response
     */
    public function show(Booking $booking)
    {
        return new BookingResource($booking);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Booking  $booking
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Booking $booking)
    {
        $status_id = 1;

        $booking->update([
            'status_id' => $status_id
        ]);

        return new BookingResource($booking);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Booking  $booking
     * @return \Illuminate\Http\Response
     */
    public function destroy(Booking $booking)
    {
        ...
    }

Other Controller (SizeController) (working)

/**
     * Display the specified resource.
     *
     * @param  \App\Models\Size  $size
     * @return \Illuminate\Http\Response
     */
    public function show(Size $size)
    {
        return new SizeResource($size);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Size  $size
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Size $size)
    {
        $request->validate([
            'type' => ['required', 'string', 'unique:sizes,type,'.$size->id],
            'abbrv' => ['nullable', 'alpha_num', 'string']
        ]);

        $size->update(['type' => $request->type, 'abbrv' => $abbrv]);

        return new SizeResource($size);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Size  $size
     * @return \Illuminate\Http\Response
     */
    public function destroy(Size $size)
    {
       ...
    }

Size Model:

use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'abbrv',
        'type'
    ];

Example result I want from BookingController at show method:

{
	"data": {
		"status_id": 1
   }
}

The result I got:

{
	"data": []
}

Example result I want from SizeController at show method:

{
	"data": {
		"type": "test"
		"abbrv": "test"
   }
}

The result I got:

{
	"data": {
		"type": "test"
		"abbrv": "test"
   }
}
0 likes
8 replies
Resaizu's avatar

@priyalaks

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class BookingResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}
Resaizu's avatar

@priyalaks Hello, the returned value is null. It looks like at the model parameter of show(Request $request, Booking $booking) is not getting/querying anything I passed. But when I change to show(Request $request, int $id) I can see what I passed.

priyalaks's avatar

@Resaizu Can you check what are you getting when you dd $booking.

public function show(Booking $booking)
    {
dd($booking);
        return new BookingResource($booking);
    }
Resaizu's avatar

@priyalaks

 App\Models\Booking {#1198
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: array:1 [
    0 => "status"
  ]
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:1 [
    0 => "status_id"
  ]
  #guarded: array:1 [
    0 => "*"
  ]
}

As expected none. the Booking $booking from show method doesnt get anything I passed.

Resaizu's avatar

When I do this:

public function show(Request $request, int $id)
{
    $booking = Booking::findOrFail($id);

    return new BookingResource($booking);
}

That works but why it doesn't work on type hint?

Resaizu's avatar
Resaizu
OP
Best Answer
Level 1

Ok, I rediscovered the root of the problem. My api route is:

Route::apiResource('/test-booking', BookingController::class);

Where it should be:

Route::apiResource('/booking', BookingController::class);

The endpoint must match with the given controller name.

Please or to participate in this conversation.