Home > Net >  How to request data from database in Laravel?
How to request data from database in Laravel?

Time:11-04

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?

CodePudding user response:

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