Hi zachleigh. Thanks for your fast response - my function looks already like this. I updated my first post and removed the double stars (public function edit(* OfferType * $offerType) -> public function edit(OfferType $offerType)).
Controller-Method with type hinting give empty eloquent object
Hi all. I think the title says it all:
routes.php
Route::group([ 'middleware' => 'web' ], function () { Route::resource('offertype', 'OfferTypeController'); });
php artisan route:list
+--------+-----------+----------------------------+-------------------+-----------------------------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+----------------------------+-------------------+-----------------------------------------------------------------+------------+ | | GET|HEAD | / | | Closure | | | | GET|HEAD | offertype | offertype.index | App\Http\Controllers\OfferTypeController@index | web | | | POST | offertype | offertype.store | App\Http\Controllers\OfferTypeController@store | web | | | GET|HEAD | offertype/create | offertype.create | App\Http\Controllers\OfferTypeController@create | web | | | GET|HEAD | offertype/{offertype} | offertype.show | App\Http\Controllers\OfferTypeController@show | web | | | PUT|PATCH | offertype/{offertype} | offertype.update | App\Http\Controllers\OfferTypeController@update | web | | | DELETE | offertype/{offertype} | offertype.destroy | App\Http\Controllers\OfferTypeController@destroy | web | | | GET|HEAD | offertype/{offertype}/edit | offertype.edit | App\Http\Controllers\OfferTypeController@edit | web | +--------+-----------+----------------------------+-------------------+-----------------------------------------------------------------+------------+
OfferTypeController (example 1)
/**
* Show the form for editing the specified resource.
*
* @param OfferType $offerType
* @return \Illuminate\Http\Response
*/
public function edit(OfferType $offerType)
{
dd($offerType);
return view('offertype.edit', compact('offerType'));
}
= Wrong - empty eloquent object:
OfferType {#234 ▼
#fillable: array:1 [▼ 0 => "name" ]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼ 0 => "*" ]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
+wasRecentlyCreated: false
}
OfferTypeController (example 2)
/**
* Show the form for editing the specified resource.
*
* @param int $offerType
* @return \Illuminate\Http\Response
*/
public function edit($offerType)
{
dd(OfferType::find($offerType));
return view('offertype.edit', compact('offerType'));
}
= Correct - filled eloquent object:
OfferType {#239 ▼ #fillable: array:1 [▼ 0 => "name" ] #connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
**#attributes: array:4 [▼
"id" => 1
"created_at" => "2016-03-27 16:08:28"
"updated_at" => "2016-03-27 16:08:28"
"name" => "First Type"
] #original: array:4 [▼
"id" => 1
"created_at" => "2016-03-27 16:08:28"
"updated_at" => "2016-03-27 16:08:28"
"name" => "First Type"
]** #relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼ 0 => "*" ]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
OfferType.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OfferType extends Model
{
protected $fillable = [ 'name' ];
public function offers()
{
$this->hasMany(Offer::class);
}
}
Other things
Out of desperation, I have already done the following:
- php artisan clear-compiled
- php artisan optimize
- composer dump-autoload -o
- php artisan cache:clear
- php artisan view:clear
- php artisan route:clear
- restart the server
I don't have a plan where I can find the problem. Please, help me.*
Try changing the variable name to $offertype (note the lowercase 't', matching the resource route naming).
Laravel will automatically resolve type-hinted Eloquent models defined in routes or controller actions whose variable names match a route segment name.
Please or to participate in this conversation.