PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] Laravel 5 Route Model Binding not working on server

 January 18, 2022     laravel, laravel-5     No comments   

Issue

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?


Solution

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.



Answered By - jhoff
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing