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

chagouani's avatar

get id on slect box

I have 2 interface the first to create a "technicien" the 2nd it's given him a "tarification" of hit "tache" so after finishing to create our technicien button validate us rederict to the interface tarifciation which takes as parameter id techncien_id automatically on à dynamic combobox

I managed to recover technicien_id but my problem is that I have relative combobox in the 2nd interface so I have to reselected the technician to take the technicien_id

tarificationcontroller.php

public function create($technicien_id)   
{
    $technicien = technicien::orderBy('id','desc')->get();
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->get();
    return view('tarification.create')->with('taches', $taches)->with('technicien', $technicien)- 
    >with('metiers', $metiers)->with('technicien_id', $technicien_id);
}

create.blade.php

@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
@if(count($errors))
 <div class="alert alert-danger" role="alert">
 <ul>
   @foreach($errors ->all() as $message)
    <li>{{$message}}</li>
    @endforeach
 </ul>
</div>
@endif
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> 
</script>
<script type="text/javascript">
var getMetiersByTechnicienUrl = "{{url('/metiersbytechnicien')}}";
var getTachesByMetierUrl = "{{url('/tachesbymetier')}}";
//console.log(getMetiersByTechnicienUrl,getTachesByMetierUrl 
,getTarificationsByTacheUrl);
function getMetiersByTechnicien(val) {
    if(val.length>0) {
        var technicien_id = val;
        $.get(getMetiersByTechnicienUrl+'/'+technicien_id,function(res) 

{ var html = '<option value=">-Select-"' ; $.each(res.metiers,function(index,item) { html+=''+item.libelle_metier+''; }); $('#metiers').html(html);

        });
    }
}




function getTachesByMetier(val) {
    if(val.length>0) {
        var metier_id = val;
        $.get(getTachesByMetierUrl+'/'+metier_id,function(res) {
            var html = '<option value="">-Select-</option>' ;
            $.each(res.taches,function(index,item) {
                html+='<option 
  value="'+item.id+'">'+item.libelle_tache+'</option>';
            });
            $('#taches').html(html);

        });
    }
}
</script>
<div class="container">
<div class="row"></div>
<div class="col-md-12">
   
   <div class="col-md-10">
        <h1>Tarification tache</h1>
    <form action=" {{url ('tarification')  }}" method="post">
     {{csrf_field()}}
     <div class="form-group">
        <label for="technicien">Technicien</label>
           <select onchange="getMetiersByTechnicien(this.value)" 
name="technicien_id" id="technicien_id" class="form-control">
                   <option value="">-Select-</option>
                   @foreach($technicien as $t)
                          <option value="{{$t->id }}" {{ $t->id == 
$technicien_id ? 'selected = "selected"' : '' }}>
                                {{$t->user->nom}}
                          </option>
                   @endforeach
            </select>
    </div>
    <div class="form-group">
        <div class="col-md-12">
                <div class="col-md-4">
        <label>Metier: </label>
        <select onchange="getTachesByMetier(this.value)" style="width: 
200px" class="productm form-control" id="metiers">
            <option value="">-Select-</option>
       </select>
                </div>
                <div class="col-md-4">                                      
                    <label>tache: </label>
                    <select style="width: 200px" class="productname 
 form-control" name="tache_id" id="taches">
                        <option value="">-Select-</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label>tarification: </label>
                    <input style="width: 200px" class="productname form- 
  control" type="text"  name ="Tarif" class="form-control" value=" 
  {{old('tarif')}}">
   
   </div>
     </div>
      </div>
       <div class="form-group">
       <input type="submit" value = "enregistrer" class="form-control 
       btn btn-primary">
        </div>
        </div>
            </div>






           </div>
<link 
 href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/    
    bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap- 
    datepicker/1.5.0/css/bootstrap-datepicker.css" rel="stylesheet">

    

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap- 
 datepicker/1.5.0/js/bootstrap-datepicker.js"></script>
@endsection

route.php

Route::get('tarification/create/{technicien_id?}', 
    'TarificationController@create');
0 likes
35 replies
mballaag's avatar

you can use hidden input

<input name="technicien_id" type="hidden" id="entry_ref" value="{{ $technicien_id}}" >

or session

session(['technicien_id' => $technicien_id]);
1 like
mballaag's avatar

@chagouani

on view you replace your select by input.

for session you add session on your controller function

you can retrieve value like this on any controller :

Session::get('technicien_id');
1 like
mballaag's avatar

i suppose you controller will be like

public function create($technicien_id)   
    {
        $technicien = technicien::find($technicien_id);
        $taches = Tache::orderBy('libelle_tache', 'asc')->get();
        $metiers = Metier::orderBy('libelle_metier', 'asc')->get();
        >with('technicien', $technicien)->with('metiers', $metiers);
    }

on your controller i replace this:

<div class="form-group">
        <label for="technicien">Technicien</label>
           <select onchange="getMetiersByTechnicien(this.value)" 
name="technicien_id" id="technicien_id" class="form-control">
                   <option value="">-Select-</option>
                   @foreach($technicien as $t)
                          <option value="{{$t->id }}" {{ $t->id == 
$technicien_id ? 'selected = "selected"' : '' }}>
                                {{$t->user->nom}}
                          </option>
                   @endforeach
            </select>
    </div>

by this:

<div class="form-group">
        <label for="technicien">Technicien : {{$technicien->user->nom}}</label>
          <input name="technicien_id" type="hidden" id="entry_ref" value="{{ $technicien->id}}" >
    </div>
1 like
chagouani's avatar

@mballaag the same think he token on technicien id the id automatically but the next select box I receive it empty

mballaag's avatar

@chagouani what is the content of function's controller whose return create.blade.php??

1 like
chagouani's avatar

@mballaag this is the function to return create.blade.php

 public function create($technicien_id)   
{
    $technicien = technicien::find($technicien_id);
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->get();
    return view('tarification.create')->with('technicien', $technicien)->with('metiers', 
   $metiers);
}
chagouani's avatar

@mballaag and i have this function on metier controller

 public function getMetiersByTechnicien($technicien_id)
{

    $t = technicien::find($technicien_id);
    return response()->json(['metiers' => $t->metier]);
}
mballaag's avatar

vous pouvez expliquer le problème en francais (on se comprendra mieux je pense :) )

1 like
chagouani's avatar

@mballaag j'ai 2 interface la 1er affiche la liste des technicien avec un bouton ajouter tarification car chaque technicien à plusieurs tarification quand on appui sur le bouton ajouter tarification pour un technicien sélectionné dans la 1er interface elle nous redirige vers une interface qui contient des Select box relative ('technicien'->'metier'->'tache') le technicien doit être sélectionné automatiquement Mon problème maintenant c'est que l'interface tarification affiche le technicien automatiquement mais le sélect box du 'metier' n'affiche rien et si je sélectionne manuellement un technicien il affiche ses metier

mballaag's avatar

@chagouani une fois que le technicien est choisi automatique depuis le backend et n'est plus rechoisi sur l'interface de tarfircation, l'on peut directement envoyer ces metiers sur l'interface et les faire afficher.

donc le controller serait ca :

 public function create($technicien_id)   
{
    $technicien = technicien::find($technicien_id);
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->where('technicien_id',$technicien_id)->get();
    return view('tarification.create')->with('technicien', $technicien)->with('metiers', 
   $metiers);
}

et la modification dans la vue :

<div class="form-group">
        <div class="col-md-12">
                <div class="col-md-4">
        <label>Metier: </label>
        <select onchange="getTachesByMetier(this.value)" style="width: 
200px" class="productm form-control" id="metiers">
            <option value="">-Select-</option>
        @foreach($metiers as $metier)
            <option value="{{$metier->id}}">{{$metier->libelle_metier}}</option>
        @endforeach
       </select>
                </div>

là vous avez les métiers du technicien sélectionné sur la page précédente. et à chaque changement de metier tu récupères juste les postes par ajax comme vous le faites déjà.

1 like
chagouani's avatar

@mballaag when i have change like this i have this erreur

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'technicien_id' in 
    'where clause' (SQL: select * from `metiers` where `technicien_id` = 2 and 
   `metiers`.`deleted_at` is null order by `libelle_metier` asc) 

this is my view

<div class="form-group">
            <div class="col-md-12">
                    <div class="form-group">
                        <div class="col-md-12">
                                <div class="col-md-4">
                        <label>Metier: </label>
                        <select onchange="getTachesByMetier(this.value)" style="width: 
                        200px" class="productm form-control" id="metiers">
                            <option value="">-Select-</option>
                        @foreach($metiers as $metier)
                            <option value="{{$metier->id}}">{{$metier->libelle_metier}}</option>
                        @endforeach
                       </select>
                    </div>
                    <div class="col-md-4">
                        <label>tache: </label>
                        <select style="width: 200px" class="productname form-control" 
       name="tache_id" id="taches">
                            <option value="">-Select-</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label>tarification: </label>
                        <input style="width: 200px" class="productname form-control" 
          type="text"  name ="Tarif" class="form-control" value="{{old('tarif')}}">
                           
                        
                    </div>


                </div>
                </div>
                <div class="form-group">
                <input type="submit" value = "enregistrer" class="form-control btn btn- 
      primary">
            </div>
            </div>
                </div>
mballaag's avatar

@chagouani votre table "metiers" n'est pas lié à des techniciens ou bien la colonne qui doit contenir l'id du technicien ne s'appelle pas technicien_id. vous pourrez corriger le code du controller facilement. la partie concernée par les metiers est la suivante:

    $metiers = Metier::orderBy('libelle_metier', 'asc')->where('technicien_id',$technicien_id)->get();
   

il faudrait changer technicien_id par la bonne colonne ou suprimer toute la clause where('technicien_id',$technicien_id)

chagouani's avatar

@mballaag le nom du champ est comme dans le code et quand je suprime where j'ai cette erreur

Trying to get property of non-object (View: 
   C:\xampp\htdocs\projet\resources\views\tarification\create.blade.php)"

et entre metier et technicien j'ai une relation many to many comme ceci

model metier

 public function techniciens()
{
    return $this- 
    >belongsToMany('App\technicien','technicien_zone','metier_id','technicien_id');

}

model technicien

public function metier()
{
    return $this- 
>belongsToMany('App\metier','technicien_metier','technicien_id','metier_id');

}
mballaag's avatar

@chagouani ok si c'est une relation manytomany le controller doit changer vers ca :

public function create($technicien_id)   
{
    $technicien = technicien::find($technicien_id);
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->where('techniciens', function($query, $technicien_id){$query->where('id',$technicien_id;})->get();
    return view('tarification.create')->with('technicien', $technicien)->with('metiers', 
   $metiers);
}

pour les problèmes lié à la vue fraudrait s'assurer que la table metier contient bien id je pense

chagouani's avatar
"Parse error: syntax error, unexpected ';', expecting ',' or ')'"
mballaag's avatar

@chagouani ok:

public function create($technicien_id)   
{
    $technicien = technicien::find($technicien_id);
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->where('techniciens', function($query, $technicien_id){
        $query->where('id',$technicien_id);
    })
    ->get();
    return view('tarification.create')->with('technicien', $technicien)->with('metiers', 
   $metiers);
}

petite correction pour la vue sinon tu vas te retrouver avec des strings dans le controller

@foreach($metiers as $metier)
      <option value={{$metier->id}}>{{$metier->libelle_metier}}</option>
@endforeach
                                
chagouani's avatar

@mballaag

"Missing argument 2 for 
    App\Http\Controllers\TarificationController::App\Http\Controllers\{closure}()"
mballaag's avatar

@chagouani try this:

public function create($technicien_id)   
{
    $technicien = technicien::find($technicien_id);
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->where('techniciens', function($query) use ($technicien_id){
        $query->where('id',$technicien_id);
    })
    ->get();
    return view('tarification.create')->with('technicien', $technicien)->with('metiers', 
   $metiers);
}
chagouani's avatar

@mballaag

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'techniciens' in 'where 
clause' (SQL: select * from `metiers` where `techniciens` = (select * where `id` = 2) 
and `metiers`.`deleted_at` is null order by `libelle_metier` asc) ◀"
mballaag's avatar

désolé pour le retard @chagouani :

public function create($technicien_id)   
{
    $technicien = technicien::find($technicien_id);
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->whereHas('techniciens', function($query) use ($technicien_id){
        $query->where('id',$technicien_id);
    })
    ->get();
    return view('tarification.create')->with('technicien', $technicien)->with('metiers', 
   $metiers);
}
chagouani's avatar

@mballaag

pas grave je vous remercie pour le temps que vous m'avais sacré

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 
'technicien_zone.metier_id' in 'where clause' (SQL: select * from `metiers` where 
exists (select * from `techniciens` inner join `technicien_zone` on `techniciens`.`id` 
= `technicien_zone`.`technicien_id` where `metiers`.`id` = 
`technicien_zone`.`metier_id` and `id` = 2 and `techniciens`.`deleted_at` is null) 
and `metiers`.`deleted_at` is null order by `libelle_metier` asc) ◀"    
mballaag's avatar

tu dois donc avoir un problème avec la déclaration de ton modèle.

si 'technicien-zone' c'est ta table intermediaire entre 'techniciens' et 'metiers' il doit contenir les colonnes 'technicien_id' et metier_id comme tu les a déclaré. or technicien_zone ne contient pas de colonne 'metier_id'

mballaag's avatar

@chagouani ton model devrait être alors :

metier.php

public function techniciens()
{
    return $this- 
    >belongsToMany('App\technicien','technicien_metier','metier_id','technicien_id');

}

technicien.php

public function metier()
{
    return $this- 
>belongsToMany('App\metier','technicien_metier','technicien_id','metier_id');

}

dans le premier vous avez rempli la table comme etant 'technicien_zone'

chagouani's avatar

@mballaag j'ai modifier le model et j'ai l'aisser la vue et le controller comme suite

<div class="col-md-4">
    <label>Metier: </label>
    <select onchange="getTachesByMetier(this.value)" style="width: 
    200px" class="productm form-control" id="metiers">
        <option value="">-Select-</option>
    @foreach($metiers as $metier)
          <option value={{$metier->id}}>{{$metier->libelle_metier}}</option>
    @endforeach
   </select>
            </div>

cotroller

 public function create($technicien_id)   
{
    $technicien = technicien::find($technicien_id);
    $taches = Tache::orderBy('libelle_tache', 'asc')->get();
    $metiers = Metier::orderBy('libelle_metier', 'asc')->whereHas('techniciens', 
    function($query) use ($technicien_id){
        $query->where('id',$technicien_id);
    })
    ->get();
    return view('tarification.create')->with('technicien', $technicien)->with('metiers', 
   $metiers);
}

j'ai u ce message d'erreur

"Trying to get property of non-object (View: 
   C:\xampp\htdocs\projet\resources\views\tarification\create.blade.php)"
mballaag's avatar

metier possède t'il bien les attributs 'id' et 'libelle_metier'?? si oui ajoute dans ton controller avant le return

var_dump( $metiers); die;

vous capturez et envoyer le résultat afin que je puisse le consulter.

chagouani's avatar

@mballag

       object(Illuminate\Database\Eloquent\Collection)#121 (1) { ["items":protected]=> 
 array(1) { [0]=> object(App\metier)#361 (27) { ["guarded":protected]=> array(0) { } 
 ["dates":protected]=> array(1) { [0]=> string(10) "deleted_at" } 
["connection":protected]=> string(5) "mysql" ["table":protected]=> NULL 
["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" 
["incrementing"]=> bool(true) ["with":protected]=> array(0) { } 
["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> 
bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(5) { 
["id"]=> int(3) ["libelle_metier"]=> string(8) "metier 2" ["deleted_at"]=> NULL 
["created_at"]=> string(19) "2018-06-08 11:41:36" ["updated_at"]=> string(19) "2018-06-08 
11:41:36" } ["original":protected]=> array(5) { ["id"]=> int(3) ["libelle_metier"]=> string(8) 
 "metier 2" ["deleted_at"]=> NULL ["created_at"]=> string(19) "2018-06-08 11:41:36" 
["updated_at"]=> string(19) "2018-06-08 11:41:36" } ["changes":protected]=> array(0) { } 
["casts":protected]=> array(0) { } ["dateFormat":protected]=> NULL 
["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } 
["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } 
["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> 
array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } 
["forceDeleting":protected]=> bool(false) } } }
mballaag's avatar

@chagouani $metier est correctement envoyé sur la vue. je voudrais plus d'informations sur l'erreur pour pouvoir la debug. pouvez vous capturer et afficher l'erreur la plus récente dans les log le chemin du fichier est certainement votre_fichier\storage\logs\laravel-2018-06-14.log

Next

Please or to participate in this conversation.