#Implicit Bindind
48 messages · Page 1 of 1 (latest)
I want to use in the edit() function the implicit Bindind but it does not work I have a return that is empty. ```PHP
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class cheval extends Model
{
use HasFactory;
protected $table = 'cheval';
protected $primaryKey = 'Id_Cheval';
protected $connection = 'mysql';
protected $fillable = [
'numero',
'nom',
'pere',
'race',
'couleur',
'sexe',
'lieu_naissance',
'date_naissance',
'Id_Mere',
];
public function chevals()
{
return cheval::class;
}
public function cheval()
{
return $this->belongsTo(cheval::class);
}
}
?>
use App\Http\Controllers\Controller;
use App\Models\cheval;
class chevauxController extends Controller
{
// this mehode 1 it works
public function edit($id_cheval)
{
$cheval = cheval::find($id_cheval);
dd($cheval);
return view('chevaux.edit', compact('cheval'));
}
// I want to replace the methode 1 and use implicit Bindind here but it doesn't work
public function edit(cheval $chevaux)
{
dd($cheval);
return view('chevaux.edit', compact('chevaux'));
}
}
```
with edit(id_cheval) it's ok , i have my #attributes: array:12 [▶]
App\Models\cheval {#1272 ▼ // resources\views/chevaux/edit.blade.php
#table: "cheval"
#primaryKey: "Id_Cheval"
#connection: "mysql"
#fillable: array:9 [▶]
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
result with edit(cheval $cheval) doesn't return anything
App\Models\cheval {#1264 ▼ // resources\views/chevaux/edit.blade.php
#table: "cheval"
#primaryKey: "Id_Cheval"
#connection: "mysql"
#fillable: array:9 [▶]
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
Syntax highlighting, please.
```php
// PHP code here..
```
Is there any particular reason you’re not following any of Laravel’s naming conventions?
• Models (and all PHP classes) should be named using StudlyCase (i.e. Cheval, not all-lowercase).
• There’s no need to change the primary key name from the default of id (and especially not with mixed class like you have with “Id_Cheval”).
Je suis débutant
Et je ne suis pas français.
sorry
I am a beginner
I've only been on laravel for 20 days
I modified id because I have a database where id was named id_horse
I didn't quite understand what you meant by that "and especially not with mixed class like you have with “Id_Cheval”"
Well columns (and tables) tend to be named all-lowercase, like id_cheval
OK
I will search for writing conventions
do you have any idea where my problem comes from
Show your route definition.
POST chevaux ......................................................... chevaux.store › chevauxController@store
GET|HEAD chevaux/create ................................................ chevaux.create › chevauxController@create
GET|HEAD chevaux/{chevaux} .............................................. chevaux.show › chevauxController@show
PUT|PATCH chevaux/{chevaux} ............................................ chevaux.update › chevauxController@update
DELETE chevaux/{chevaux} ............................................. chevaux.destroy › chevauxController@destroy
GET|HEAD chevaux/{chevaux}/edit .......................................... chevaux.edit › chevauxController@edit
GET|HEAD contact ........................................................... contact.create › contactController@create
POST contact ....................................... contact.store › entraineurController@edit
use App\Http\Controllers\chevauxController;
Route::resource('chevaux', chevauxController::class);
If the binded route parameter is {chevaux} you have to do edit(cheval $chevaux)
As an advice, try to use PHP conventions. Class names must be UpperCamelCase class Cheval and class ChevauxController
I will follow your advice for the convention.
but the problem is still there
public function edit(cheval $chevaux) // ne fonctionne pas !!!!!!!
{
return view('chevaux.edit', compact('chevaux'));
} // return un model objet cheval
can you show your model and table?
could be the primary key being in a different format causing it to not work
if you have cheval.ChevalID for example you have to put protected $primaryKey = 'ChevalID'; inside your model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class cheval extends Model
{
use HasFactory;
protected $table = 'cheval';
protected $primaryKey = 'Id_Cheval';
protected $connection = 'mysql';
protected $fillable = [
'numero',
'nom',
'pere',
'race',
'couleur',
'sexe',
'lieu_naissance',
'date_naissance',
'Id_Mere',
];
public function chevals()
{
return cheval::class;
}
public function cheval()
{
return $this->belongsTo(cheval::class);
}
}
?>
``````PHP
use App\Http\Controllers\Controller;
use App\Models\cheval;
class chevauxController extends Controller
{
// this mehode 1 it works
public function edit($id_cheval)
{
$cheval = cheval::find($id_cheval);
dd($cheval);
return view('chevaux.edit', compact('cheval'));
}
// I want to replace the methode 1 and use implicit Bindind here but it doesn't work
public function edit(cheval $chevaux)
{
dd($cheval);
return view('chevaux.edit', compact('chevaux'));
}
}
with edit(id_cheval) it's ok , i have my #attributes: array:12 [▶]
App\Models\cheval {#1272 ▼ // resources\views/chevaux/edit.blade.php
#table: "cheval"
#primaryKey: "Id_Cheval"
#connection: "mysql"
#fillable: array:9 [▶]
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
result with edit(cheval $chevaux) doesn't return anything
App\Models\cheval {#1264 ▼ // resources\views/chevaux/edit.blade.php
#table: "cheval"
#primaryKey: "Id_Cheval"
#connection: "mysql"
#fillable: array:9 [▶]
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
Implicit Bindind
try public function getRouteKeyName(): string
{
return 'Id_Cheval';
}
in your model
I added it to the model, but
I'm sorry since I'm a beginner I don't know how to try it to give you a result
So, you're pretty much changing every single thing from Laravel, which makes things a lot more difficult, especially if you're new to Laravel. Like I mentioned earlier, you'd have to configure your resource route. Then, your model names are really weird (cheval instead of Cheval), which might also cause issues. So there's something that's causing the binding to not work at all, because if it would work you'd see a 404.
I'm sorry, I didn't know all this before. I'm looking at the writing conventions and rewriting everything correctly
If you’re a beginner then it would be better if you just stuck to Laravel’s conventions instead of naming things randomly.
yes, but I did not know that this could cause problems 😅
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Cheval extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $table = 'cheval';
protected $primaryKey = 'Id_Cheval';
protected $connection = 'mysql';
protected $fillable = [
'numero',
'nom',
'pere',
'race',
'couleur',
'sexe',
'lieu_naissance',
'date_naissance',
'Id_Mere',
];
public function chevals()
{
return $this->hasMany(Cheval::class);
}
public function cheval()
{
return $this->belongsTo(Cheval::class);
}
}
use App\Http\Controllers\Controller;
use App\Models\cheval;
class ChevauxController extends Controller
{
// this mehode 1 it works
public function edit($id_cheval)
{
$cheval = Cheval::find($id_cheval);
dd($cheval);
return view('chevaux.edit', compact('cheval'));
}
// I want to replace the methode 1 and use implicit Bindind here but it doesn't work
public function edit(Cheval $cheval)
{
dd($cheval);
return view('chevaux.edit', compact('cheval'));
}
}
use App\Http\Controllers\ChevauxController;
Route::resource('chevaux', ChevauxController::class)->parameters([
'chevaux' => 'cheval'
]);
I changed everything but the problem is still there
Why don’t you just use route model binding?
Did you, by any chance, comment out the SubstituteBindings middleware?
Otherwise, just go back to basics, create a plain simple other route and see if route model binding works there
That's the issue, apparently that's not working (which wasn't entirely clear initially for me either..)
🧐 I turned up late lol… seems there’s a lot of extra added stuff that’s unnecessary, I’d just delete it all and start again using a conventional approach. So much easier lol.
Hi, this is what I did. I started from scratch and now it works. I couldn't tell you why it didn't work with the first project, but thanks for your help, I learned a lot.