I'm new to Laravel8 and i'm trying to insert the data with image so when I try to insert the data it's just shows me inserted data and image path only.Does anybody have any idea how to solve this?
file:-RestaurantController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Restaurant;
class RestaurantController extends Controller
{
function add(Request $request)
{
$resto=new Restaurant;
$resto->name=$request->input('name');
$resto->email=$request->input('email');
$resto->address=$request->input('address');
$resto->create_at=$request->input('create_at');
$resto->uploaded_at=$request->input('uploaded_at');
$resto->image=$request->input('image');
// $resto->image=$request->file('image')->store('images');
if($request->hasFile('image')){
$image = $request->file('image');
$image_name = $image->getClientOriginalName();
$image->move(public_path('/images'),$image_name);
$image_path = "/images/" . $image_name;
}
$resto->save();
$request->session()->flash('status','Client entered successfully');
return redirect('list');
}
}
file:-add.blade.php
@extends('layout')
@section('content')
<div >
<form method="POST" action="" enctype="multipart/form-data">
@csrf
<div >
<label for="name">Name</label>
<input type="text" name="name" id="name" placeholder="Enter Your Name">
</div>
<div >
<label for="email">Email</label>
<input type="email" name="email" id="email"
placeholder="[email protected]">
</div>
<div >
<label for="address">Address</label>
<textarea name="address" id="address" rows="3"></textarea>
</div>
<div >
<label for="create_at">Create At Date</label>
<input type="date" name="create_at" id="create_at">
</div>
<div >
<label for="uploaded_at">Uploaded At Date</label>
<input type="date" name="uploaded_at" id="uploaded_at">
</div>
<div >
<label for="image">Image</label>
<input type="file" name="image" id="image">
</div>
<button type="Submit" clas="btn btn-primary">Submit</button>
</form>
</div>
@stop
web.php
Route::get('/',[RestaurantController::class,'index']);
Route::view('/add','add');
Route::post('/add',[RestaurantController::class,'add'])->name('add');
I got `<img src=http://127.0.0.1:8000/storage/images alt="image" height="50" width="50" >` and what i want is http://127.0.0.1:8000/storage/images/`my_image_name`
When I perform return $request->all(); it shows me {"_token":"ExiC1hv4sX3qrz6ZcQJJNfIL6bjblw938hfRkG8J","name":"test","email":"[email protected]","address":"testing address","create_at":"2022-05-30","uploaded_at":"2022-06-01","image":{}}
as a output
CodePudding user response:
you can use this code
if($file = $request->hasFile('image')) {
$file = $request->file('image') ;
$fileName = $file->getClientOriginalName() ;
$destinationPath = public_path().'/images' ;
$file->move($destinationPath,$fileName);
}
CodePudding user response:
You can simply use store() method .
...
if ($request->hasFile('image')){
$image = $request->image->store('image');
}
$restro->image = $image;
$restro->save();
and you can retrieved the stored file in blade as
<img src= {{ asset('storage/'. $restro->image) }} />
Make sure you have created the symlink of your storage folder in public folder.If you don`t you can create it by artisan command
php artisan storage:link