Hi im trying to upload image into database when i do this all its gave error like this.
(1/1) BadMethodCallException
Method getClientOrignalName does not exist.
<form action="{{route('post.store')}}" method="post" enctype="multipart/form-data">**strong text**
public function store(Request $request)
{
$this->validate($request,[
'title' => 'required|max:255',
'content' => 'required',
'feature' => 'required|image',
'category_id' => 'required'
]);
// dd($request->all());
//exit;
$featured = $request->feature;
$featured_new_name=time().$featured->getClientOrignalName();
$featured->move('uploads/posts',$featured_new_name);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'feature'=>'uploads/posts/'. $featured_new_name,
'category_id'=>$request->category_id
]);
Session::flash('success','Post Created Successfully.');
}
You should use file() method for retrieve file information from request. Try this code,
public function store(Request $request) {
$this->validate($request,[
'title' => 'required|max:255',
'content' => 'required',
'feature' => 'required|image',
'category_id' => 'required'
]);
// use file() method for retrive file data
$featured = $request->file('feature');
$featured_new_name = time() . $featured->getClientOrignalName();
$featured->move('uploads/posts', $featured_new_name);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'feature'=>'uploads/posts/'. $featured_new_name,
'category_id'=>$request->category_id
]);
Session::flash('success','Post Created Successfully.');
}
Related
I am trying to store logged user's id but I am getting this error
ErrorException
array_map(): Argument #2 should be an array
This is the code in the controller
public function store(Request $request)
{
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$data = request()->validate([
'id' => $id = Auth::id(),
'content' => 'required',
'topic' => 'required',
'hashtag' => 'required'
]);
$check = Tweets::create($data);
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}
The error is in this line of code.
'id' => $id = Auth::id(),
I know that should be a string but to explain to you what I am trying to do, and I still have not found any solution.
Do it Like this.
public function store(Request $request)
{
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$request->validate([
'content' => 'required',
'topic' => 'required',
'hashtag' => 'required'
]);
$data = $request->all();
$data['id'] = Auth::id();
$check = Tweets::create($data);
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}
Delete this
'id' => $id = Auth::id(),
and add
$data['id'] = Auth::id();
before
$check = Tweets::create($data);
That should work
I'm using the Spatie MediaLibrary library in a Laravel application. I want to upload 0 or more photos to my app via a REST API.
I can get it to work when the photo attribute contains 1 file
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable|file'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
// stores the photo
if ($request->hasFile('photo')) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
return new ListingResource($listing);
}
The postman request looks as follows:
I know want to change the code so it can handle multiple photos in the request. I'm using the following code in the controller above to do so:
if ($request->hasFile('photo')) {
foreach ($request->input('photo', []) as $photo) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
}
and I have changed the attribute to photos[] instead of photo.
The code never goes into the foreach loop even.
Anyone has a hint on how to solve this?
Apparently the Spatie Medialibrary has a function called addMultipleMediaFromRequest. The full code is now
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
return new ListingResource($listing);
}
In Postman, I'm calling it as follows:
documentation reference
I managed to upload multiple files like this:
if($request->file('photos')) {
foreach ($request->file('photos') as $photo) {
$post->addMedia($photo)->toMediaCollection('post');
}
}
Check this out:
https://github.com/spatie/laravel-medialibrary/issues/227#issuecomment-220794240
This code is working for me.
View
<input type="file" name="photo[]" multiple />
ListingController
public function store(Request $request)
{
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
}
hi m trying to save data into db data saves but image isn't saving how to save it in db:
controller:
public function store(Request $request)
{
// dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $request->category_image,
]);
$path = $request->file('category_image');
$path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images');
return back();
}
Well... In your portion of code, there is no var.save(); so how will eloquent store any data without the given instruction?
Per example do something like this:
$userImage = new UserImage;
$UserImage->save();
public function store(Request $request)
{
// dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
$path = $request->file('category_image');
$nameImage = $path->getClientOriginalName();
$path->move(base_path('public/images/backend_images/category_images'), $nameImage);
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $nameImage,
]);
return redirect()->back();
}
code in my project and its work!
if ($request->hasFile('image')) {
$file = $request->file('image');
$filename = date('mdYHis') .str_random(5).'.'.$file->extension();
$request->image->move(base_path('public/images/rooms'), $filename);
$request->merge(array('image' => $filename));;
}
I think this would help. But you can browse for more answers.
Save to public directory
$path = 'images/backend_images/category_images';
$file = $request->file('category_image');
$filename = $file->getClientOriginalName();
$file->move(public_path($path), $filename);
And the, save to database
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $filename
]);
My form validation is not working in Laravel. How can I update my form with validation in Laravel?
You can check my code here-
public function update(Request $request, $id)
{
$id->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
$updateInfo= Info::findOrFail($id);
$updateInfo->user_id = $request->input('user_id');
$updateInfo->Name = $request->input('Name');
$updateInfo->UserName = $request->input('UserName');
$updateInfo->Password = $request->input('Password');
$updateInfo->save();
return redirect('/info');
}
You need to call validate on $request, like this-
$request->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
Here is the full code-
public function update(Request $request, $id)
{
$request->validate([
'Name'=>'required',
'UserName'=>'required',
'Password'=>'required|min:6',
'email'=>'required|email',
]);
if (!$validator->fails()) {
$updateInfo= Info::findOrFail($id);
$updateInfo->user_id = $request->input('user_id');
$updateInfo->Name = $request->input('Name');
$updateInfo->UserName = $request->input('UserName');
$updateInfo->Password = $request->input('Password');
$updateInfo->save();
} else {
\Session::flash('error', $validator->messages()->first());
return redirect()->back()->withInput();
}
return redirect('/info');
}
I have added one more condition in the code to handle the validation errors. If validation fails then it will redirect back with your inputs as well as the validation error messages. Make sure you have error session flash in your blade views to show the errors.
For me this is best way , i can keep on track on query and other exceptions by putting it in try catch block
public function update(Request $request, $id)
{
try{
$validator = Validator::make($request->all(), [
'name' => 'required',
'UserName' => 'required',
'Password' => 'required',
'email' => 'required|email',
]);
if($validator->fails()) {
return redirect()
->route('path_to_edit_form')
->withErrors($validator)
->withInput();
}
Info::where('id',$id)->update([
'user_id' => $request->get('user_id'),
'Name' => $request->get('Name'),
'UserName' => $request->get('UserName'),
'Password' => $request->get('Password'),
]);
return back()->with([
'alert_type' => 'success',
'message' => 'User info updated successfully.'
]);
}catch(\Exception $e){
return back()->with([
'alert_type' => 'danger',
'message' => $e->getMessage()
]);
}
}
I would like to create a validation for the update function
I would like it to exclude the title for that dedant id
public function update(Request $request, Post $post)
{
$this->validate($request,[
'title' => "required|unique:posts,title,".$id.'id',
'image' => 'image',
'categories' => 'required',
//'tags' => 'required',
'body' => 'required',
]);
I propose another example that works maybe it can come in handy
public function update(Request $request, $id)
{
$this->validate($request,[
'name' => "required|unique:categories,name,".$id.'id',
'image' => 'mimes:jpeg,bmp,png,jpg'
]);
This code will work,
public function update(Request $request, Post $post)
{
$this->validate($request,[
'title' => ["required,unique:posts,title,".$this->route('post')],
'image' => 'image',
'categories' => 'required',
//'tags' => 'required',
'body' => 'required',
]);
Let me know is it work for you.
You can try this:
public function update(Request $request, $id)
{
$rules = [
'title' => 'required|unique:posts,title,'.$id,
'image' => 'image',
'categories' => 'required',
'body' => 'required',
];
$this->ValidateForm($request->all(), $rules);
$post= Post::find($id);
if($post){
$post->title = $request->title;
$post->categories = $request->categories;
$post->body = $request->body;
if ($request->hasFile('image')){
Storage::delete($request->profile_photo);
$post->image = $request->file('image')->store('post');
}
$post->save();
}
}