Code Lover
Code Lover

Reputation: 8378

Laravel resource always return null

I am having strange issue. The resource method always returns null.

Table name

operating_days

Model

class OperatingDay extends Model
{
    /**
     * @var string[]
     */
    protected $fillable = ['day', 'date'];
}

Controller@edit

public function edit(OperatingDay $operatingDay)
{
    return view('admin.day.form')->with('operatingDay', $operatingDay)->with('title', __('admin.day.edit_day'));
}

Route

Route::resource('days', 'OperatingDayController')->names([
    'index'   => 'admin.days.index',
    'store'   => 'admin.days.store',
    'create'  => 'admin.days.create',
    'show'    => 'admin.days.show',
    'update'  => 'admin.days.update',
    'destroy' => 'admin.days.destroy',
    'edit'    => 'admin.days.edit',
]);

dd var in view

@dd($operatingDay)


App\OperatingDay {#1400 ▼
  #fillable: array:2 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

@dd($operatingDay->id)

// output null

Upvotes: 1

Views: 1345

Answers (1)

Remul
Remul

Reputation: 8252

The problem is that you named your resource days therefore Laravel will try to route it to a model called Day, but your model is called OperatingDay, in order to route days to OperatingDay you can use explicit binding.

From the docs:

To register an explicit binding, use the router's model method to specify the class for a given parameter. You should define your explicit model bindings in the boot method of the RouteServiceProvider class:

public function boot()
{
    parent::boot();

    Route::model('user', App\User::class);
}

So in your case it would look something like this:

use App\OperatingDay; // or whatever the namespace to your model is
use Illuminate\Support\Facades\Route;

public function boot()
{
    parent::boot();

    Route::model('day', OperatingDay::class);
}


If you only need this in one route you could also rename the route parameter:

From the docs:

By default, Route::resource will create the route parameters for your resource routes based on the "singularized" version of the resource name. You can easily override this on a per resource basis by using the parameters method. The array passed into the parameters method should be an associative array of resource names and parameter names:

Route::resource('users', 'AdminUserController')->parameters([
    'users' => 'admin_user'
]);

So in your case it would be something like this:

Route::resource('days', 'OperatingDayController')
    ->names([
        'index' => 'admin.days.index',
        'store' => 'admin.days.store',
        'create' => 'admin.days.create',
        'show' => 'admin.days.show',
        'update' => 'admin.days.update',
        'destroy' => 'admin.days.destroy',
        'edit' => 'admin.days.edit',
    ])
    ->parameters(['days' => 'operatingDay']);

Upvotes: 1

Related Questions