0

I need to pass user_id from blade to routes, and then use the variable into a laravel command. How can I do?

lista_lavori.blade.php

<div class="box-tools">
     <a href="/stampasingoloreport/{{$utente->id}}" class="btn btn-box-tool" role="button"><i class="fa fa-print"></i></a>
</div>

web.php - route

Route::get('/stampasingoloreport/{id}', function ($id) {
    Artisan::call('StampaSingoloReport:stampasingoloreport');
    return back();
});

StampaSingoloReport.php - Commands

public function handle()
{

    // static id i need to change this dinamically
    $id = 5;
    $utente = \App\User::where('id',$id)->get();

    //invio email per avvertire l'utente
    $data = array('utenti'=>$utente);
    Mail::send('mail.invioMailReportLavoriSingoli', $data, function($message) {
        $message->to('[email protected]', 'Admin Risorse Umane') ->subject('Email da piattaforma BancaStatoHR') ;
        $message->from('[email protected]') ;
    });

}

1 Answer 1

1

You can pass an array to call() method like

Route::get('/stampasingoloreport/{id}', function ($id) {
    Artisan::call('StampaSingoloReport:stampasingoloreport',[
      'id' => $id     
 ]);
  return back();
});

Now in your handle method, you can access these arguments like

protected $signature = 'StampaSingoloReport:stampasingoloreport { id } ' ;

function handle(){
  $this->argument('id');
 // your code
}

Hope this helps

Sign up to request clarification or add additional context in comments.

13 Comments

levaral I got this error The "id" argument does not exist.
@CarmineRub have you used the code which is provided in the answer?
levaral yes but don't work! Can you show me the href="" in the blade? please.
Artisan::call('StampaSingoloReport:stampasingoloreport',[ 'id' => $id ]); this code will run the command with arguments
function handle(){ $this->argument('id'); // your code } this function will recieve the argument
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.