Reputation: 195
I am making CRUD operation in laravel 5.4. I am saving an image in database and storage. I did it, but it is not doing with random name. Means If image name is logo.jpg, I need some other random name of that image save in database and in folder also.
I tried it. But if I save two different image with same name then it replace with new one.
My web.php is:
Route::resource('todo','todocontroller');
My todocontroller.php is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\todo;
class todocontroller extends Controller
{
public function create()
{
return view('todo.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$todo = new todo;
$this->validate($request,[
'title' => 'required|unique:todos',
'file' => 'image|mimes:jpeg,png,jpg,gif',
'body' => 'required',
],
[
'title.required' => 'Title can not be empty',
'unique' => 'Already inserted. Please try another.',
'file.image' => 'Upload only JPEG OR PNG',
'body.required' => 'Body can not be empty',
]);
if ($request->hasFile('file')) {
$images = $request->file->getClientOriginalName();
$request->file->storeAs('public/images',$images);
$todo->image = $images;
}
$todo->title = $request->title;
$todo->body = $request->body;
$todo->save();
session()->flash('message','Inserted Successfully');
return redirect('todo');
}
}
My create.blade.php is:
@extends('layout.app')
@section('body')
<br>
<a href="../todo" class="btn btn-info">Back</a>
<h1>Create new item</h1><br>
<form action="../todo" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
<input type="text" name="title" value="{{old('title')}}" placeholder="title">{{$errors->first('title',':message')}}
<br>
<input type="file" name="file" placeholder="Image">{{$errors->first('file',':message')}}
<br>
<textarea rows="5" name="body" placeholder="body">{{old('body')}}</textarea>{{$errors->first('body',':message')}}
<button type="submit">Submit</button>
</form>
</div>
@endsection
Upvotes: 1
Views: 4963
Reputation: 158
Very vague question but if I understood it properly, then you can try to append a random string hash
to your image name.
Maybe try to do it like this:
$images = md5(microtime()).'_'.$request->file->getClientOriginalName();
This will append a microtime
has to your image name and then save it to the database.
Upvotes: 1
Reputation: 1509
If you want to save image with different name please use this method: storeAs()
From Laravel docs:
https://laravel.com/docs/5.7/filesystem#storing-files Specifying A File Name
Upvotes: 1
Reputation: 303
Try the below code.
if ($request->hasFile('file')) {
$images = $request->file->getClientOriginalName();
$images = time().'_'.$images; // Add current time before image name
$request->file->storeAs('public/images',$images);
$todo->image = $images;
}
Upvotes: 3
Reputation: 9373
Please try this code update code. the time()
get current timestamp
.
if ($request->hasFile('file')) {
$file = $request->file('file');
$extension = $file->getClientOriginalExtension(); // getting image extension
$filename = time().'.'.$extension;
$file->move('public/images/', $filename);
$todo->image = $filename;
}
Upvotes: 2