Hello,
Somebody please help me out here.
I have a unit test for an api endpoint which is supposed to test the data structure of the json response. The issue is that the model id param which is being sent to the controller results in a null value for that model when I dd the model variable in the controller. I am using RefreshDatabase and WithoutMiddleware. The WithoutMiddleware is to forego the need for authorization. The request is an update PUT request. There are no global scopes being applied.
The route as outputted by php artisan route:list is PUT|PATCH api/venues/{venue}.
The relevant test code is as follows:
$response = $this
->actingAs($user)
->putJson('/api/venues/'.$venue->id, $data);
The relevant controller code is:
public function update(UpdateVenueRequest $request, Venue $venue)
{
$user = User::current();
dd($venue);
The venue in question is created using a factory in the test. When I do Venue::find($venue->id) under where I have created the venue, the newly created model is successfully returned. So the problem seems to lie in the passing of the $venue->id to the controller. The $data variable is a simple array of values to be used in the update PUT request.
When I run dd($venue); at the top of the controller I get the following:
App\Models\Venue^ {#4853 // app/Http/Controllers/Venue/VenuesController.php:131
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: array:6 [
"gallery" => "array"
"rating" => "integer"
"published" => "boolean"
"feature_multiple_bookings" => "boolean"
"last_outbound_comms_at" => "datetime"
"private_artists_enabled" => "boolean"
]
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: array:13 [
0 => "name"
1 => "about"
2 => "email"
3 => "telephone"
4 => "website"
5 => "icon"
6 => "gallery"
7 => "rating"
8 => "published"
9 => "address_id"
10 => "billing_address_id"
11 => "last_outbound_comms_at"
12 => "location_id"
]
#guarded: array:1 [
0 => "*"
]
#automaticTax: false
#customerIpAddress: null
#estimationBillingAddress: []
#collectTaxIds: false
#couponId: null
#promotionCodeId: null
#allowPromotionCodes: false
+mediaConversions: []
+mediaCollections: []
#deletePreservingMedia: false
#unAttachedMediaLibraryItems: []
}
When I run dd($venue->id) it returns null. It appears to be getting an empty venue model instance even though there appears to have been a model created in the test.
Any ideas? I have no idea why this does not pass the model to the controller.
I have only included what I think is the relevant code. I can provide the complete test and/or controller if necessary.
Thanks.