Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Route Model Binding not working on server

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?

like image 933
shock_gone_wild Avatar asked Feb 23 '15 09:02

shock_gone_wild


1 Answers

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.

like image 171
jhoff Avatar answered Nov 15 '22 08:11

jhoff