this is my first time using Laravel 9 and I want to ask some Question.
So I try to upload image and save it to database, then I want to show the image in my view. But the problem is in phpmyadmin it only shows the location where the uploaded file is stored as .tmp. The image that I uploaded is with the file name that matches what I want in the folder public/image, but why in database the name changes become D:\xampp\tmp\php5819.tmp
, can anyone help me please?
Here's my controller:
public function create()
{
$student = Student::all();
return view('create', ['addstudent' => $student]);
}
public function save(Request $request)
{
$newName = '';
if ($request->file('image')) {
$extension = $request->file('image')->getClientOriginalExtension();
$newName = $request->name . '-' . now()->timestamp . '.' . $extension;
$request->file('image')->storeAs('image', $newName, 'public');
}
$request['image'] = $newName;
$student = Student::create($request->all());
if ($student) {
session()->flash('success', 'Data berhasil ditambahkan');
session()->flash('pesan', 'Data berhasil ditambahkan');
}
return redirect('/about');
// dd($request->all());
}
This is my View that I want to display the image:
@extends('layouts.templates')
@section('title', 'Detail')
@section('content')
<div class="container">
<div class="row">
<h1>Student Detail</h1>
<div class="my-3">
<img src="{{asset('image'.$student->image)}}" alt="{{ $student->name}}">
</div>
<h3>Nama : {{$student->name}}</h3>
<h3>Gender :
@if ($student->gender == 'P')
Perempuan
@else
Laki - laki
@endif</h3>
<h3>NIM : {{$student->NIM}}</h3>
<a href="/about">Back</a>
</div>
</div>
@endsection
This is my Form
@extends('layouts.templates')
@section('title', 'Add Student')
@section('content')
<div class="container">
<div class="row">
<div class="col-8 m-auto">
<h2 class="my-3">Form Add Student</h2>
<form action="save" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="name">Nama </label>
<input name="name" type="text" class="form-control" id="name" aria-describedby="emailHelp"
placeholder="Masukkan Nama" required>
</div>
<div class="form-group">
<label for="gender">Gender</label>
<select name="gender" id="gender" class="form-control" required>
<option value="">Pilih</option>
<option value="L">L</option>
<option value="P">P</option>
</select>
</div>
<div class="form-group mb-3">
<label for="NIM">NIM</label>
<input name="NIM" type="text" class="form-control" id="NIM" placeholder="Masukkan NIM">
</div>
<label for="NIM">Image</label>
<div class="input-group mb-3">
<input type="file" class="form-control" id="image" name="image"
aria-describedby="inputGroupFileAddon04" aria-label="Upload">
</div>
<div class="form-group row">
<div class="col-sm-10 mb-5">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
<a href="/about">Back</a>
</div>
</div>
</div>
@endsection
Here's the error In database
In laravel folder Image name in laravel folder
Database column name Column name
You are storing the filename from request which is temp filename instead of Laravel stored.
The method storeAs()
will return the path Laravel stored, try below.
public function save(Request $request)
{
$data = $request->all();
if ($request->file('image')) {
$extension = $request->file('image')->getClientOriginalExtension();
// $newName = $request->name . '-' . now()->timestamp . '.' . $extension; No need for this.
$path = $request->file('image')->storeAs('image', $newName, 'public'); // Catch the path Laravel stored.
}
$data['image'] = basename($path); // Suggest save filename.ext only, if you need origin path just remove basename function.
$student = Student::create($data);
if ($student) {
session()->flash('success', 'Data berhasil ditambahkan');
session()->flash('pesan', 'Data berhasil ditambahkan');
}
return redirect('/about');
// dd($request->all());
}