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">
<input type="text" name="title" value="" placeholder="title">
<br>
<input type="file" name="file" placeholder="Image">
<br>
<textarea rows="5" name="body" placeholder="body"></textarea>
<button type="submit">Submit</button>
</form>
</div>
@endsection
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2MtUKkF
via IFTTT
Aucun commentaire:
Enregistrer un commentaire