7

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?

12
  • The missing attributes is because it cannot find the record. Is the data the same in the database? i.e. same database, tables and fields? Specifically - does the record itself exist? Commented Feb 23, 2015 at 9:28
  • Is your filename correct on the server? Is it PrimaryLocation.php with the case sensitivity the same? Commented Feb 23, 2015 at 9:29
  • If it could not find the record, there should be a 404 error (at leas the docs state this) , if i am correct. And if the filename is not correct there should be some kind of 'hard' error... (Whoops something went wrong) Commented Feb 23, 2015 at 9:34
  • Yes - correct - but I'm just trying to rule out various issues. Commented Feb 23, 2015 at 9:35
  • Just to clarify - the issue is that the record is not been found. I dont know why it is not throwing a 404 - but the fact you get null for the id is proof of that. Plus when you dd() the object you can clearly see exists: false. So we need to confirm the record is actually in the database - then work it out from there. Commented Feb 23, 2015 at 9:40

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

Comments

0

just add this line to serviceProvider::boot()

 $router->model('attribute', Attribute::class);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.