i have an issue with Laravel 5 Route Model Binding I am using the following Controller Method
public function destroy(PrimaryLocation $primaryLocation) {
dd($primaryLocation->id);
$primaryLocation->delete();
return redirect()->back()->with('locationDeleted', true);
}
Where PrimaryLocation is an Eloquent Model
My RouteServiceProvider's boot function:
public function boot(Router $router)
{
parent::boot($router);
$router->model('user', 'App\User');
$router->model('PrimaryLocation', 'App\PrimaryLocation');
}
And in my routes.php
Route::delete('deletePrimaryLocation/{PrimaryLocation}',
['as' => 'admin.deletePrimaryLocation', 'uses' => 'LocationsController@destroy']);
This setup works fine on my local Computer, but when i deploy the files to my development server, somwhere the model binding breaks; The Location won't get deleted on executing the method.
I did some var_dumps
dd($primaryLocation->id);
on local computer this returns the correct id, but on the server it will just return null;
However if I do a
dd($primaryLocation)
The result is locally
PrimaryLocation {#178 ▼
#fillable: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:4 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
On on my Server nearly the same... but the attributes are missing:
PrimaryLocation {#195 ▼
#fillable: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
}
Does anyone have a clue what might be going wrong?
[UPDATE]
if I comment out
// $router->model('PrimaryLocation', 'App\PrimaryLocation');
In me ServiceProvider, the local behaviour is the same as on the server. Maybe there's something wrong with loading the ServiceProvider? Maybe there is some sort of cache?
After going through the same issue, I discovered that in production, storage/framework/compiled.php
doesn't get rebuilt regularly like in development mode.
Basically, you are just running an old version of RoutesServiceProvider.php on your production server.
The fix is easy enough though. Just run php artisan clear-compiled
.
It would be a good practice to add line to any deployment scripts as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With