Forum
/
Topics

independence implicite

Publié le 30 mars 2023 par g-you

Bonjour j'ai un problème avec l'injection implicite, quelqu'un peut-il m'orienter vers le problème merci

1<?php
2 
3namespace App\Models;
4use Illuminate\Database\Eloquent\Factories\HasFactory;
5use Illuminate\Database\Eloquent\Model;
6 
7class cheval extends Model
8{
9 use HasFactory;
10 protected $table = 'cheval';
11 protected $primaryKey = 'Id_Cheval';
12 protected $connection = 'mysql';
13 protected $fillable = [
14 'numero',
15 'nom',
16 'pere',
17 'race',
18 'couleur',
19 'sexe',
20 'lieu_naissance',
21 'date_naissance',
22 'Id_Mere',
23 ];
24 
25 public function chevals()
26 {
27 return cheval::class;
28 }
29 
30 public function cheval()
31 {
32 return $this->belongsTo(cheval::class);
33 }
34}
35 
36 
37use App\Http\Controllers\Controller;
38use App\Models\cheval;
39class chevauxController extends Controller
40{
41 public function edit($id_cheval)
42 {
43 $cheval = cheval::find($id_cheval);
44 dd($cheval);
45 return view('chevaux.edit', compact('cheval'));
46 } // return un model objet cheval
47 
48 public function edit(cheval $cheval) // ne fonctionne pas !!!!!!!
49 {
50 dd($cheval);
51 return view('chevaux.edit', compact('cheval'));
52 } // return un model objet cheval
53}

Avec edit(id_cheval) tout fonction parfaitement

1App\Models\cheval {#1272 ▼ // resources\views/chevaux/edit.blade.php
2 #table: "cheval"
3 #primaryKey: "Id_Cheval"
4 #connection: "mysql"
5 #fillable: array:9 [▶]
6 #keyType: "int"
7 +incrementing: true
8 #with: []
9 #withCount: []
10 +preventsLazyLoading: false
11 #perPage: 15
12 +exists: true
13 +wasRecentlyCreated: false
14 #escapeWhenCastingToString: false
15 #attributes: array:12 [▶]
16 #original: array:12 [▶]
17 #changes: []
18 #casts: []
19 #classCastCache: []
20 #attributeCastCache: []
21 #dates: []
22 #dateFormat: null
23 #appends: []
24 #dispatchesEvents: []
25 #observables: []
26 #relations: []
27 #touches: []
28 +timestamps: true
29 #hidden: []
30 #visible: []
31 #guarded: array:1 [▶]
32}

mais avec edit(cheval $cheval) j'ai rien

1App\Models\cheval {#1264 ▼ // resources\views/chevaux/edit.blade.php
2 #table: "cheval"
3 #primaryKey: "Id_Cheval"
4 #connection: "mysql"
5 #fillable: array:9 [▶]
6 #keyType: "int"
7 +incrementing: true
8 #with: []
9 #withCount: []
10 +preventsLazyLoading: false
11 #perPage: 15
12 +exists: false
13 +wasRecentlyCreated: false
14 #escapeWhenCastingToString: false
15 #attributes: []
16 #original: []
17 #changes: []
18 #casts: []
19 #classCastCache: []
20 #attributeCastCache: []
21 #dates: []
22 #dateFormat: null
23 #appends: []
24 #dispatchesEvents: []
25 #observables: []
26 #relations: []
27 #touches: []
28 +timestamps: true
29 #hidden: []
30 #visible: []
31 #guarded: array:1 [▶]
32}
1 réponse
William Suppo, Il y a 1 an

Bonjour g-you,

Il y a un peut-être un problème avec le nommage de tes classes, essaie en respectant la psr 4 => https://www.php-fig.org/psr/psr-4/

C'est à dire en mettant une majuscule à tes classes : nom dans le fichier et nom du fichier aussi.

Ciao