Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hamzacreede's avatar

select in view a relationshap

hey everybody ! i need some help so , i have 3 tables (" vehicules ", "conducteurs") and ("affectation") this table present the relationshape between (" vehicules " AND "conducteurs") i set in this table the id of those tables the problem is when i want select in view some informations of those tables i use this in view

 @foreach ($Conducteurs as $categorie)

                       <tr>
                   
                               <td>{{$categorie->vehicules->test   }} </td>

                               <td>{{$categorie->conducteurs->test   }} </td>

                       </tr>

   @endforeach 

a try this to :

 @foreach ($Conducteurs as $categorie)

                       <tr>

                     @foreach ($Conducteurs as $categorie)

                               <td>{{$categorie->vehicules()->test   }} </td>

                    @endforeach 

                      @foreach ($Conducteurs as $categorie)

                               <td>{{$categorie->conducteurs()->test   }} </td>

                    @endforeach

                       </tr>

   @endforeach 

this is my controller :

public function ShowAffectation(){

          if (Auth::check()) {  // user is an admin

                
                $affectation=DB::table('affectations')
            
               ->join('conducteurs', 'affectations.NumeroConducteur', '=', 'conducteurs.NumeroConducteur')
               ->join('vehicules', 'affectations.NumeroVéhicule', '=', 'vehicules.NumeroVéhicule') 
               ->get();

                  

               $arr=Array( 'affectation'=>$affectation);


              return view('Operations/affectations',$arr) ;

          }

                return view('auth/login-admin');// user is a guest

}

the tables: //conducteurs public function up() { Schema::create('conducteurs', function (Blueprint $table) {

        $table->increments('NumeroConducteur');

        $table->integer('NumeroPermis');

        $table->string('Nom');
        $table->string('Prenom');
        $table->date('DateNaissance');
        $table->string('Adresse');
        $table->string('SituationFamiliale');
        $table->string('Genre');
        $table->date('AnneeObtention');
        $table->string('WilayaObtention');


        $table->timestamps();
    });
}

//affectations public function up() { Schema::create('affectations', function (Blueprint $table) {

         $table->increments('ID_Affectation');

         $table->integer('NumeroVéhicule')->unsigned();

         $table->foreign('NumeroVéhicule')->references('NumeroVéhicule')->on('vehicules');




         $table->integer('NumeroConducteur')->unsigned();

         $table->foreign('NumeroConducteur')->references('NumeroConducteur')->on('conducteurs');


        $table->timestamps();
    });
}

//véhicules public function up() { Schema::create('vehicules', function (Blueprint $table) {

         $table->increments('NumeroVéhicule');

        $table->integer('NumeroSequenceMatricule');

        $table->string('Wilaya');
        $table->date('AnneeCirculation');          
        $table->string('Marque');
        $table->string('Modele');
        $table->string('Couleur');


        $table->timestamps();
    });
}

AND finaly the models :

//conducteurs protected $table="conducteurs";

 public function vehicule(){

    return $this->belongsTo('App\Affectation');
 }

//véhicules protected $table="vehicules";

 public function vehicule(){

    return $this->belongsTo('App\Affectation');
 }

//affectations protected $table="affectations";

 public function conducteur(){

    return $this->hasMany('App\Conducteur','NumeroConducteur');
 }



 public function vehicule(){

    return $this->hasMany('App\Vehicule','NumeroVéhicule');
 }

ALL this doesn't Work ! :/

0 likes
6 replies
robrogers3's avatar

i'm not sure what your specific problem is but this

 @foreach ($Conducteurs as $categorie)

                       <tr>

                     @foreach ($Conducteurs as $categorie)

                               <td>{{$categorie->vehicules()->test   }} </td>

                    @endforeach 

                      @foreach ($Conducteurs as $categorie)

                               <td>{{$categorie->conducteurs()->test   }} </td>

                    @endforeach

                       </tr>

   @endforeach

is wrong. well, it's also confusing what does the 'test' property refer to?

either way you are looping 3 times when you only need one loop.

in the outer loop. you have access to the 'current' categorie

so the two inner foreachs don't require you to loop them.

maybe you want?

foreach ($categorie->vehicules()) ??

and foreach ($categorie->conducteurs()) where you print out the test property each time.

2 likes
Snapey's avatar

why bother creating eloquent models and relationships then just use DB joins ?

1 like
hamzacreede's avatar

thanks for your fast answer gys :D ! but @robrogers3 he sent me this message:

"Method vehicules does not exist. (View:......"

robrogers3's avatar

@hamzacreede this $categorie->vehicules()

should be this. $categorie->vehicules

probably.

what does '->test' refer too??????????? what propety.

Please or to participate in this conversation.