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

VETO87's avatar

Laravel and python

I know it's strange title but my issue is that I'm using Laravel to build my website but there's some form which I want to process the inputs using python library NLP. so is there way when user submit the form the data will process by python script then return the data to Laravel? without using API because it will take time.

0 likes
7 replies
shawnveltman's avatar
Level 3

You're not being super clear on what you mean by asking to process the data with python and then return it to laravel... Do you want to do it synchronously (ie within the controller / component, and use the returned data as part of your workflow)?

Or do you want to just trigger the processing to update something in your database, and don't need it available immediately?

EIther way, you can easily call python with the shell_exec route. Here's an example of how I do it in one of my projects:

public function run_python(Request $request)
    {
        $arguments    = $request->options_string;
        $full_path_to_python_script = '/your/full/path/to/file/my_awesome_python_file.py';
        $command      = "python3 $full_path_to_python_script \"{$arguments}\"";
        $output       = shell_exec($command);
        $return_value = ['data' => $output];

        return response()->json($return_value);
    }
3 likes
VETO87's avatar

@shawnveltman This is good actually. I have two questions first how do u handle $arguments in python? I mean how to define it in python. second is there way to cache the imported model in the python script? because every time I trigger the script it takes a lot of time to load the python models.

Temoran's avatar

@shawnveltman

Subject: Seeking Advice on Integrating Python AI Libraries with Laravel

Dear @shawnveltman ,

I hope this message finds you well. As a new developer learning Laravel, I am eager to create my first application. However, I have some concerns regarding the integration of Python AI libraries in the future.

My initial plan is to become proficient in full-stack Laravel development with Vue and utilize third-party APIs, such as OpenAI, for any AI-related tasks. After gaining approximately a year of experience, I intend to learn Python and Flask in order to leverage the powerful AI libraries available within the Python ecosystem. This would involve creating a microservice that acts as my own custom API, which can be called by my Laravel application to perform specific AI functions and return results.

I would greatly appreciate your insight on this approach. Do you think this is a viable strategy? Are there alternative methods that might be more efficient or effective? My primary concern lies in ensuring that I can make the most out of both PHP/Laravel and Python's data and AI capabilities.

Thank you for taking the time to read my message and consider my questions. Your expertise and guidance are truly invaluable as I navigate this exciting journey into web development.

Best regards, Alex

Temoran's avatar

@benjamin1509 I wrote the original message myself, but it was a type-o riddled mess. I had ChatGPT 'clean' it up, but clearly it just made me sound even worse. 🫣😬 lol

Bottom line, I want to learn Laravel since Django looks so sad, but I don't want to miss out on the Python AI goodness down the line.

For example, the Python Library Langchain is looking very promising to help making LLM automation chains.

shawnveltman's avatar

@Temoran Hey Alex, sorry for the late reply here. My advice is likely similar to what you've heard before, but it's still pretty far from your "plan", so it may be worth hearing/reading again :)

My advice -Don't spend a year learning Vue + API, then learn python & flask... Instead, pick a SMALL problem that you can work on over the next week, and learn just enough to solve that problem with a single toolset (preferably Laravel since you're here).

Once you've solved that, you'll likely think of other things that you can add on. EG: You created a "chatbot" that takes in user input & hits the OpenAI API, then spits back the results. Awesome! But now, make it look better (learn enough Tailwind to do what you want).

Then, you realize that you want to have the cool "typing in real-time" effect from the AI response - so learn a bit about the Laravel HTTP streaming stuff (honestly I don't know much about that yet, but for the purposes of this example let's assume I do, haha), and how to use Laravel / Livewire / Alpine to display the cool streaming message in "real" time.

Long & short, my advice is don't try to learn technologies / languages / frameworks. Instead, learn how to solve specific problems, and then increase the scope of problems you can solve. My preference is to learn to solve problems with as few tools as possible (I haven't wanted to learn Flask/Rails because Laravel solves the same sorts of problems, for example... But Laravel isn't the right choice for things like Machine Learning, or interfacing with certain services that require python SDKs, etc..).

My advice is super old fashioned, but it's essentially this - Learn to solve problems with the tools you're comfortable with, and learn what problems those tools can't solve...

That's not to say that you shouldn't learn about other things (Vue, React, Flask, React, Flutter/Dart, Whatever) - but as you're looking at them, consider them through the lens of "How would this tool make it easier to solve [problem] than anything else?" OR "What kinds of problems are better solved with this, and is it worth using it in my workflow just to solve that specific problem?"

shawnveltman's avatar

The arguments is just a string, you handle those in python. Mine uses sys.argv, but there are certainly other options.

re: Caching your model in the python script... So far as I can tell, you don't get to use Laravel models / eloquent / etc in your python... So I'm not sure how you'd pass cached data between Laravel & python.

Please or to participate in this conversation.