Home > Blockchain >  Execute python file in Laravel and save data into DB
Execute python file in Laravel and save data into DB

Time:02-02

I have a python script that I want to execute in my Laravel application through the pressure of a button and then save in a DB some data that returns me the script. I use symfony/process but I don't get the desired result. Someone can help me?

My view in Laravel is this:

<form action="{{ url('admin/scan') }}" method="POST">
    @csrf
    <button type="submit"  id="scan">Scan Device</button><br>
</form>

The controller is this:

public function scan()
    {
        $process = new Process(['python', 'C:\Simone\Università\Smart IoT Devices\Lab_Raspy\Bluetooth\prova.py']);
        $process->run();
        $process->getOutput();
    }

And the Route is:

Route::post('scan', [DataFromRaspController::class, 'scan'])->name('scan');

CodePudding user response:

To troubleshoot your problem, please check the exception. By default, it doesn't show any underlying errors.

if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

Don't forget to include,

 use Symfony\Component\Process\Exception\ProcessFailedException;
  •  Tags:  
  • Related