I'm still new to working with APIs and laravel and vue. I have a Laravel backend server, you can create a "Drone" like that:
$droneModel = new DroneModel;
$data = $request->all();
foreach ($data as $key => $value) {
$droneModel->$key = $request->$key;
}
if ($request->hasFile('image')) {
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$fileName = time() . '.' . $extension;
$file->move('images/drones/', $fileName);
$droneModel->image = $fileName;
// $droneModel->image = imgUpload($request, 'drones');
}
$droneModel->save();
return response()->json([
'message' => 'Drone Model Added successfully',
'Drone' => $droneModel
], 201);
images are stored in the database by the name of the image, and its saved in the "/public/images/drones/" folder
in the vueJs project, I have this:
<script>
import axios from "axios";
export default {
name: "listDrones",
data() {
return {
drones: [],
};
},
created() {
this.getDrones();
},
methods: {
getDrones() {
axios.get("http://127.0.0.1:8000/api/drones").then((response) => {
this.drones = response.data;
});
},
},
};
</script>
<template>
<!-- <p>{{ drones }}</p> -->
<section style="background-color: #eee">
<div class="container py-5">
<div class="row">
<div class="col-md-12 col-lg-4 mb-4 mb-lg-0">
<div class="card text-black">
<img
:src="'../../../../../../sager/inventory/public/images/' + drones[0].image"
class="card-img-top"
alt="iPhone"
/>
</div>
</div>
</div>
</div>
</section>
</template>
all the drones in the database are stored in the "drones" variable and I want to display images but don't know how.
I know the src is not correct so please help.
inventory/public/images/directory part of the laravel project or the vue project?