How to request data from database in Laravel? - javascript

I've built a frontend in my vue3/laravel8 project and now want to get into the backend. I've been good at learning Vue3 but my knowledge base of Laravel is bare minimum.
I need to fetch an image link from a database and render it in a blade file.
In Vue3 I would use axios.get to send a request to a controller through an url and receive the image link. How do I send a request to my controller in Laravel? Is that even the right way to do it? I thought that maybe Laravel has some backend tricks up its sleeve, that Vue does not.
Edit:
I know that I can get data like this:
$data = DB::select('select * from db_name');
Does it have to be sent through an http response or can I just import the variable in the blade file?

Let's decompose your question to 2 sub-questions:
1- We need to fetch data when call axios request:
you have to update routes\api.php to link the coming request to the proper method located inside your controller
here's an example
2- We need to access our data in front-end:
Vue
If you using Vue components you can parse the response data then use it as you prefer
Laravel Blade System
You to call return view('your-blade-location', payload), payload => is your fetched data
here's a docs link

Related

How to connect laravel api with pure html website

Since I am new to laravel api, I don't know hot to connect laravel api to html endpoint. My laravel api is working well and html web pages also completely finish. I just want to connect them together... Please explain how to connect these two.
Thank you
I would suggest two ways to go about this but it all depends on what your API does. If you are looking to serve the HTML with Laravel and have some parts of the application loaded by Laravels view() method, you'd basically need to break your HTML into blade files in resources/view folder and call the blade files via view() in controller to load the desired page.
However if you are looking for a separation of view and API where API is called by the view only for some information, you'd need to utilize AJAX via JavaScript to make a call to the API endpoint and retrieve the data (JSON) for use in your HTML site.
I use axios a lot and here is a sample call:
axios.get(url).then(response => { // do whatever here with the response data });
I'm not sure I understand you. But it reads like you developed your Laravel Application (PHP) and HTML separately. Laravel uses Blade (see: https://laravel.com/docs/7.x/blade) as a template engine into which you can inject PHP objects. Basically, the call of a web page (more or less) works like this:
The user is calling the url
URL goes to the routes/web.php
in this file you can call e.g. a controller
the controller called via (e.g.)
return view('my.site.nice-site', [ key => value]);
blade starts and displays the page to the user with the given key as variable.
I hope this helps you a bit. Otherwise I recommend, just to get started, the documentation from Laravel or YouTube.

How to connect Laravel 5.4 backend with Angular 5 Frontend

I am working on a project where I need to develop frontend in angular 5 and backend business logic in laravel 5.4 with mySql Database. I am a newbie to this technology and dont know how to develop data flow connection between these two framworks.
It will be helpful if you tell me the easiest and most generic way to solve this out.
As with any other technology, you'll need to work with an API.
In your Laravel project:
Your Laravel project should have models, controllers and a file for the route paths for each method in your controllers, right? If you don't know where the routes are, there's a dedicated folder called "routes" at the root of your project.
Inside this folder, you'll have two important files: api.php and web.php. These two work exactly the same way, but the api.php will automatically add '/api' to the beginning of your route.
A route looks like a link, such as: "yourdomain.com/api/person/name/jondoe". Every route has to be accessed through an HTTP method, usually GET, POST, PUT or DELETE.
Example: let's say you need your user to see a list of people. You'll probably have a Person model and a PersonController containing some function called displayAllPeople(), and this function will select the people from your database. In your api.php file you'll add something like:
Route::get('/people', 'PersonController#displayAllPeople');
You can test that in your browser. Try to access http://yourdomain.com/api/people. You should see a JSON output of your function's return data. In this example, a list of people. Now all we have to do is make your Angular project call this API route.
Source: https://laravel.com/docs/5.7/controllers
In your Angular project:
Remember that Person model you created on your backend? Angular will access that model too, so you'll have to create a Person.ts model here. While that's not required to work, it's a good practice to create this model with the same attributes as the backend model.
Now you'll need a service. The service is a file that will access that Laravel route for you. If you're using Angular CLI, just run the command:
ng generate service person
That will create a person.service.ts file for you. Inside this file, you'll write all the functions to select people, update, save, delete people, and so on. Your function will look something like this:
findAll(): Observable<Person[]> {
return this.http.get<Person[]>('yourdomain.com/api/people');
}
Whenever you call the findAll() function, you'll access the Laravel route, which will return the People from your database. You can do that with any operation you want. Just make sure you follow the HTTP standards.
That also works if you want to send some data from your frontend to the backend, like if you want to save some new Person in your database. You'll need a POST call sending your new Person model which will be received by your Laravel's Controller function, and then persist it in the database.
Source: https://angular.io/tutorial/toh-pt4

Is JSON not serialise-able over http or Django doesn't know how to read it?

I have created a couple of projects with Django.
Every time I try to send an ajax POST request from frontend to django, I have to use JSON.stringify() function, pass it inside the body of POST request and parse the JSON manually inside Django (using ujson, because of its faster).
My question is that why do I need to do this manually?

Angular 2 with CodeIgniter

I searched a lot but unable to find the solution for this.
In angular 1.x, I used to include js in php pages and load the pages from server side code like CodeIgniter controller.
In angular 2.x, I unable to figure out that how I use server side features like $_SESSION?
e.g. I want to insert the data to mysql table and I will send data using POST service of Angular 2. How can I achieve that which user is logged in and created that record??
$city = $this->input->post("city");
$specialty= $this->input->post("specialty");
$data = array("city"=>, "specialty"=>$specialty, "createdBy"=>$this->session->userdata("userid"));
$this->db->insert("citywise", $data);
How to get this $this->session->userdata("userid"); because application is loading at client and everything is handling like REST services.
Using localStorage I can get login data on client browser but what about server?
Use JWT(Json Web Token) for the same purpose
refer this link

send multiple file through $http angularjs

I have a javascript array of multiple files like
filelist=[{'name':file1name,file:file1},{'name':file2name,file:file2}]
I need to send this file to backend views.py using $http request.
How do i send this in angularjs?
Please modify this link plunker

Categories

Resources