Search code examples
phplaravelcontrollercreate-table

How get id from page with a controller to another page with different controller


I have 3 tables: courriersEntrees, traitements, users

I have to create a traitement when the user clicks button 'affecter traitement' in index page that presents a list of courriers.

How can I get the id of the courrier clicked to the create function?

my index.blade.php of courriersEntreeController:

  <table class="table">
        <head>
            <tr>
                <th>emetteur</th>
                <th>sujet</th>
                <th>date recu</th>
                <th>type</th>
                

            </tr>
        </head>
        <body>
            @foreach($courriers as $courrier)
            <tr>
                <td>{{ $courrier->emetteur}}</td>
                <td>{{$courrier->sujet}}</td>
                
                <td>{{$courrier->date_recus}}</td>
                <td>{{$courrier->type}}</td>
                <td>
                   


                    <form action="{{url('courriers/'.$courrier->id) }}" method="post">

                        {{csrf_field()}}
                        {{method_field('DELETE')}}

                         <a href="" class="btn btn-primary"> detail </a>
                    <a href="{{url('courriers/'.$courrier->id.'/edit')}}" class="btn btn-primary"> editer </a>
                     <a href="{{url('traitements/create/')}}" class="btn btn-primary"> Affecter Traitement</a>
                    <button type="submit" class="btn btn-primary"> supprimer </button>
            </form>  
             </td>
            </tr>
            @endforeach
        </body>
    </table>

My traitementController

 public function create()
    {    $courrier= courriersEntree::find($id);
        return view('traitement.create');
    }

My traitement table: enter image description here


Solution

  • you have to pass the id from your view to the controller :

    <a href="{{ route('your route name',$courrier->id)}}" class="btn btn-primary"> Affecter Traitement</a>
    

    Or with url like :

    <a href="{{url('traitements/create/'.$courrier->id)}}" class="btn btn-primary"> Affecter Traitement</a>
    

    your controller seems to be :

    public function create($courrier_id)
        {    $courrier= courriersEntree::find($courrier_id);
            return view('traitement.create');
        }