I want to save image name to database but it always save to C:\xampp\tmp\phpAB3A.tmp. btw im not using xampp, im using laragon.How to change the path? i want to save to storage/app/public
'name'=> 'required',
'email' => 'required',
//'logo' => 'required',
$imageName = time().'.'.request()->logo->getClientOriginalExtension();
request()->logo->move(storage_path('app/public'), $imageName);
Company::create($request->all());
return redirect()->route('company.dashboard')->with('Success');
In your Company::create you have to define the path... Something like:Company::create([
'path' => torage_path('app/public',$imageName),....]);
I'm using laravel 5.8 and this code worked for me.
In controller
<?php
public function store()
{
// I did like this, because storeLogo method is reusable
$data = request()->validate([ /* ... */ ]);
$company = Company::create($data);
$this->storeLogo($company);
// but you can do like this
$data = request()->validate([ /* ... */ ]);
if (request()->has('logo')) {
$data['logo'] = request()->logo->store('', 'public');
}
$company = Company::create($data);
}
private function storeLogo(Company $company)
{
if (request()->has('logo')) {
$company->update([
'logo' => request()->logo->store('', 'public'),
// logo file is stored at /storage/app/public/
]);
}
}
$input = $request->all();
$fileName = '';
if ($request->hasFile('logo')) {
$destinationPath = storage_path('app/public');
$file = $request->logo;
$fileName = time() . '.'.$file->clientExtension();
$file->move($destinationPath, $fileName);
}
$input['logo'] = $fileName;
Company::create($input);
Related
I am a Laravel beginner and I want to build an API with Laravel 8.
I have posts and images and I want to store and update them.
My store method works and the images are saved in the database and public path in images folders, but in update method I can't save it in folder.
These are my codes:
PostController
public function store(Request $request )
{
$data = $request->all();
//validationg posts and images fields
$validator = Validator::make($data, [
'user_id' => 'required',
'category_id' => 'required',
'title' => 'required|max:150|unique:posts',
'body' => 'required',
'study_time' => 'required',
'tags' => 'nullable|string',
'image' => 'required',
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors(), 'خطا در اعتبار سنجی']);
}
//separate tags
$tags = explode(",", $request->tags);
if ($request->hasfile('image')) {
//getting post images from request
$files = $request->file('image');
//saving name and path of images
foreach ($files as $file) {
$imageName = time().rand(1,10000).'.'.$file->extension();
$postTitle = $request->title; //post title for folder name and the images inside it
$imagePath = public_path(). '/images/posts/'.$postTitle;
$file->move($imagePath, $imageName);
$image = new Image;
$image->image = $imageName;
$image->path = $imagePath;
$images[] = $image; // make an array of uploaded images
}
}
$post = Post::create($data);
$post->images()->saveMany($images);//save imageas in image table
$post->tag($tags);//save tags in tags table
return response()->json([
'success' => true,
'message' => 'با موفقیت ثبت گردید ',
'data' => $post
]);
}
public function update(Request $request, $id)
{
$post_failed = Post::find($id);
if (is_null($post_failed)) {
return response()->json('پست مورد نظر یافت نشد ', 404);
}
$data = $request->all();
//validation posts and images fields
$validator = Validator::make($data, [
'user_id' => 'required',
'category_id' => 'required',
'title' => 'required|max:150|unique:posts',
'body' => 'required',
'study_time' => 'required',
'tags' => 'nullable|string',
'image' => 'required',
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors(), 'خطا در اعتبار سنجی ']);
}
$tags = explode(",", $request->tags);
if ($request->hasfile('image')) {
$postTitle = $request->title; //post title for folder name and the images inside it
//delete last Images from database for updating images
Image::where('imageable_type', 'App\Models\Post')->where('imageable_id' , $id)->delete();
//delete last images images folder
File::delete(public_path('/images/posts/'.$postTitle));
$files = $request->file('image');
foreach ($files as $file) {
$imageName = time().rand(1,10000).'.'.$file->extension();
$imagePath = public_path(). '/images/posts/'.$postTitle;
$image = new Image();
$image->image = $imageName;
$image->path = $imagePath;
$images[] = $image;
}
}
$post = Post::find($id);
$post->user_id = $data['user_id'];
$post->category_id = $data['category_id'];
$post->title = $data['title'];
$post->body = $data['body'];
$post->study_time = $data['study_time'];
$post->tags = $data['tags'];
$post->save();
$post->images()->saveMany($images);
$post->tag($tags);
return response()->json([
'success' => true,
'message' => 'با موفقیت ویرایش گردید ',
'data' => $post
]);
}
The relation between posts and images is polymorphic one to many and I tested it with postman.
Postman
Database
And the path:
Please, help.
In store() method you saved images on disk by using
$imagePath = public_path(). '/images/posts/'.$postTitle;
$file->move($imagePath, $imageName);
In update() you deleted them
File::delete(public_path('/images/posts/'.$postTitle));
and determined path for new files
$imagePath = public_path(). '/images/posts/'.$postTitle;
but nothing happens after this. In whole update() method there is no code that could do something in storage, so of course nothing appears in folder ;)
So again use $file->move() or Storage facade to save files.
TIP
Also this is bad practice to repeat long code logic like that. It would be better to extract this and share between store/update.
I am trying to upload an image into the database. The image is not uploading properly, how can I fix it?
The image path is uploading into database like this: {"image":"phpV3IZnF.png"}
it should be like this: phpV3IZnF.png
Controller
public function servicesaction(Request $request)
{
$this->validate($request,[
'name' => 'required',
'description'=>'required',
'image' =>'required']);
$name=$request->get('name');
$description = $request->get('description');
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
Storage::disk('cms')->put($image->getFilename().'.'.$extension,
File::get($image));
$content = new Services;
$content->image = $image->getFilename().'.'.$extension;;
check = Services::where('id', $content->id)->select('image')-
>create(['service_name'=>$name,'description'=>
$description,'image'=>$content ])->get();
return back()->with('success', 'Success Successfully Inserted')-
>with('path', $check);
}
you can get file name like below
$fileName = md5($image->getClientOriginalName() . time()) . "." . $image->getClientOriginalExtension();
then save the name into database
Replace servicesaction function with below code
public function servicesaction(Request $request)
{
$this->validate($request,[
'name' => 'required',
'description'=>'required',
'image' =>'required']);
$name=$request->get('name');
$description = $request->get('description');
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
Storage::disk('cms')->put($image->getFilename().'.'.$extension,
File::get($image));
$content = new Services;
$content->image =$image->getClientOriginalName().'.'.$extension;;
check = Services::where('id', $content->id)->select('image')-
>create(['service_name'=>$name,'description'=>
$description,'image'=>$content ])->get();
return back()->with('success', 'Success Successfully Inserted')-
>with('path', $check);
}
change getFilename() to getClientOriginalName()
Like this
$extension = $image->getClientOriginalExtension()
$content->image = $image->getClientOriginalName().'.'.$extension;
Just Change 'image'=>$content to 'image'=>$content->image in the your create function
check = Services::where('id', $content->id)->select('image')
->create(['service_name'=>$name,'description'=> $description, 'image'=>$content->image ]) //use $content->image here
->get();
I want to upload pdf file on page but its not working as done with image and its working
I have made a column filepath for pdf/word then code in postcontroller but not working
public function store(Request $request)
{
$this->validate($request, [
'title' =>'required',
'featured'=>'required|image',
'content'=>'required',
'category_id'=>'required'
]);
$featured= $request->featured;
$featured_new_name=time().$featured->getClientOriginalName();
$featured->move('uploads/posts', $featured_new_name);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'featured'=>'uploads/posts/'. $featured_new_name,
'category_id'=>$request->category_id,
'slug'=>str_slug($request->title)
and when I trying to add "filepath column name in data base" for pdf/ word then using in public function store(Request $request)
{
$this->validate($request, [
'title' =>'required',
'featured'=>'required|image',
'content'=>'required',
'category_id'=>'required',
'file' => 'required',
]);
$featured= $request->featured;
$featured_new_name=time().$featured->getClientOriginalName();
$featured->move('uploads/posts', $featured_new_name);
$file=$request->file;
$file=time().$file->getClientOriginalName();
$extension = Input::file('file')->getClientOriginalExtension();
$filename = rand(11111111, 99999999). '.' . $extension;
$fullPath = $filename;
$request->file('file')->move(base_path() . '/uploads/pdf/', $filename);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'featured'=>'uploads/posts/'. $featured_new_name,
'file'=>'uploads/pdf' .$filename,
'category_id'=>$request->category_id,
'slug'=>str_slug($request->title)
]);
Session::flash('success', 'New Blog has been Published on Website for Particular Menu');
return redirect()->back();
}
There is an issue in your code, you are saving the image in your base path whereas you want to save it into your public path so use
$request->file('file')->move(public_path() . '/uploads/pdf/', $filename);
instead of
$request->file('file')->move(base_path() . '/uploads/pdf/', $filename);
To save file update your code
validation of file type
'file' => 'required|mimes:pdf,doc|max:10000',
save file
if($request->hasfile('file'))
{
$file = $request->file;
$input = rand(11111111, 99999999).'.'.$file->getClientOriginalExtension();
$destinationPath = public_path('/uploads/pdf/100');
$file->move($destinationPath, $input);
}
I have error while i am uploading Post Image I can save picture on my computer but in my database filename is distorted.
Here is my PostsController
public function store(Request $request, User $user, Image $image)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'body' => 'required'
]);
if( $request->hasFile('image') ) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->save( public_path('uploads/images/' . $filename ) );
}
$image = $filename;
auth()->user()->publish(
new Post(request(['body', 'image'])));
return redirect('/');
}
The problem is that you try to insert the wrong variable into your database table. So, improve your code like this:
auth()->user()->publish(
new Post(['photo' => request('body'), 'image' => $image])
);
I am trying to insert image in database using laravel 5.4. It works. But when I search that directory then it does not appear there. I also want to validate only images should be upload and size.
Thanks for advance.
My Controller function is here
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Hash;
use App\User;
class regController extends Controller
{
public function create(Request $request)
{
$this-> validate(request(),[
'name' => 'required',
'email'=> 'required',
'type' => 'required',
'image' => 'required',
'password' => 'required|confirmed|min:6',
]);
$request['password'] = bcrypt($request -> password);
$User = new User($request->input()) ;
if($file = $request->hasFile('image')) {
$file = $request->file('image') ;
$fileName = $file->getClientOriginalName() ;
return Storages::putFile('public/images',$request->file('image'));
$destinationPath = public_path().'images/' ;
$file->move($destinationPath,$fileName);
$User->image = $fileName ;
}
$user =
User::create(request(['name','email','type','image','password']));
return redirect('login');
}
try this :
// Validation
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,bmp,png|max:10240' // size in kelobytes this equals 10 mb
]);
$User = new User() ;
$User->password = bcrypt($request->password);
$User->name = $request->name;
$User->type = $request->type;
$User->email = $request->email;
$filename = $this->getFileName($request->image);
$request->image->move(base_path('public/images'), $filename);
$User->image = $fileName;
$$User->save();
return back()->with('success','Image Upload successful');
I just forgot to show you how to implement getFileName function to generate a unique filename sorry about that, in your controller you can define it as protected method like so:
protected function getFileName($file)
{
return str_random(32) . '.' . $file->extension();
}
I think you are tryin to do smth like this:
public function fileUpload(Request $request)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$image = $request->file('image');
$input['imagename'] = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/images');
$image->move($destinationPath, $input['imagename']);
$this->postImage->add($input);
return back()->with('success','Image Upload successful');
}