Multiple files upload Laravel 5.8 - laravel

I want to upload multiple files in Laravel but it's not working I tried in my controller
Edit: I means page refresh but no error or registred data in the db
I followed https://www.youtube.com/watch?v=wk5usyevYj0
public function storescan(Request $request){
$this->validate($request,[
'file' => 'required|file|mimes:gif,jpg,png,jpge,jpeg|max:102048'
]);
$files=$request->file('file');
foreach($files as $file){
Document::create([
'doc'=>$file->getClientOriginalName(),
'path'=>$file->store('public/storage'),
'support'=>$request->input('support'),
'ecole'=>$request->input('ecole'),
'jour'=>$request->input('jour'),
'cycle'=>$request->input('cycle'),
'type'=>'Image',
'niveau'=>$request->input('niveau'),
'matiere'=>$request->input('matiere'),
'user_id'=>Auth::user()->id
]);
}
return redirect()->route('prof.document.create')->withMessage('✅ Image(s) ajoutée(s) avec succès!');
}
And here is my route and formular
Route::post('/professeur/myDoccy/scan/create','DocumentController#storescan')->name('prof.document.createscan');
Formular everything is correct I minimized the code and left only the input file
<form enctype="multipart/form-data" class="form" action="{{ route('prof.document.createscan') }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{{ csrf_field() }}
<label class="purple" >Charger l’image </label><br/>
<input required type="file" multiple name="file[]" accept='image/*' class="form-control-file border-or m-1" >
</div>
</form>

You should check $errors variable (read more https://laravel.com/docs/5.8/validation).
You have problem with validation. You have rule
'file' => 'required|file|mimes:gif,jpg,png,jpge,jpeg|max:102048'
But it will work for single file. You have multiple of them, so it should be:
'file.*' => 'required|file|mimes:gif,jpg,png,jpge,jpeg|max:102048'
Also, you can use "image" rule instead of file with all mimes.

I solved the issue by deleting the validate part
And adding to all fillable attributes in my model but still no validation possibility
class Document extends Model
{
use SoftDeletes;
public function user(){
return $this->belongsTo('App\User');
}
protected $fillable = [
'doc','support','ecole',
'path',
'jour',
'cycle',
'type',
'niveau',
'matiere',
'user_id'
];
}
function
public function storescan(Request $request){
$files=$request->file('file');
foreach($files as $file){
Document::create([
'doc'=>$file->getClientOriginalName(),
'path'=>$file->store('public/storage'),
'support'=>$request->input('support'),
'ecole'=>$request->input('ecole'),
'jour'=>$request->input('jour'),
'cycle'=>$request->input('cycle'),
'type'=>'Image',
'niveau'=>$request->input('niveau'),
'matiere'=>$request->input('matiere'),
'user_id'=>Auth::user()->id
]);
}
return redirect()->route('prof.document.create')->withMessage('✅ Image(s) ajoutée(s) avec succès!');
}

Related

Laravel 9 how to pass parameter in url from one controller to another controller?

I was facing this problem of missing parameter when trying to pass a parameter from one controller to another controller. The parameter is $id whereby the data is originally from post method in details blade.php into function postCreateStepOne However, I want to pass the data into a new view and I return
redirect()->route('details.tenant.step.two')->with( ['id' => $id]
);}
And this is where error occur. However, it works fine if I skip it into a new route and directly return into a view with the compact parameter. For Example,
return view('document.details-step-two', compact('id', 'property'));
However, I would prefer a new url as I was doing multistep form using Laravel.
Error
web.php
Route::get('/document/details/viewing/{id}', 'ViewDetails')->name('details.tenant');
Route::post('/document/details/viewing/{id}', 'postCreateStepOne')->name('post.step-one');
Route::get('/document/details/viewing/step-2/{id}', 'ViewDetailsStep2')->name('details.tenant.step.two');
TenanatController.php
public function viewDetails($id){
$view = Properties::findOrFail($id);
return view('document.details', compact('view'));
}
public function ViewDetailsStep2(Request $request, $id){
$view = Properties::findOrFail($id);
$property = $request->session()->get('property');
return view('document.details-step-two', compact('view', 'property'));
}
public function postCreateStepOne($id, Request $request)
{
$validatedData = $request->validate([
'property-name' => 'required',
]);
if(empty($request->session()->get('property'))){
$property = new Tenancy();
$property->fill($validatedData);
$request->session()->put('property', $property);
}else{
$property = $request->session()->get('property');
$property->fill($validatedData);
$request->session()->put('property', $property);
}
return redirect()->route('details.tenant.step.two')->with( ['id' => $id] );
}
details.blade.php
<form action="{{ route('post.step-one', $view->id) }}" method="POST">
#csrf
<div class="card-body">
<div class="form-group">
<label for="title">Property Name:</label>
<input type="text" value="" class="form-control" id="property-name" name="property-name">
</div>
</div>
<div class="card-footer text-right">
<button type="submit" class="btn btn-primary">Next</button>
</div>
</form>
When you use with on a redirect the parameter is passed through the session. If you want to redirect to a route with a given route parameter you should pass that parameter in the route function itself like e.g.
return redirect()->route('details.tenant.step.two', ['id' => $id]);

Update data in laravel 6

I try to create crud in laravel 6. Create, Read and Delete process is running well. But when Update process, the data in table not change. Could anyone help me to find the problem ? The following my code.
Route
Route::get('/blog', 'BlogController#index');
Route::get('/blog/add','BlogController#add');
Route::post('/blog/store','BlogController#store');
Route::get('/blog/edit/{id}','BlogController#edit');
Route::post('/blog/update','BlogController#update');
Controller
public function index()
{
$blog = DB::table('blog')->get();
return view('blog',['blog' => $blog]);
}
public function edit($id)
{
$blog = DB::table('blog')->where('blog_id', $id)->get();
return view('edit', ['blog'=>$blog]);
}
public function update(Request $request)
{
DB::table('blog')->where('blog_id',$request->blog_id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
View
#foreach ($blog as $n)
<form method="post" action="/blog/update" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
You must provide id in your route
Route::post('/blog/update/{id}','BlogController#update');
In update method add parameter id and then find product against id
public function update(Request $request, $id)
{
DB::table('blog')->where('blog_id',$id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
#foreach ($blog as $n)
<form method="post" action="{{ route('your route name'), ['id' => $$n->id] }}" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
try separating the update into two statements like so
$blog = DB::table('blog')->where('blog_id',$id)->first();
$blog->update([
'blog_title' => $request->title,
'author' => $request->author]);
Also you might want to use models in the future so you can do it like
$blog = Blog::where('blog_id',$id)->first();
Doesn't really shorten your code but it improves the readibility.
Do your update like this:
public function update(Request $request)
{
$post = DB::table('blog')->where('blog_id',$request->blog_id)->first();
$post->blog_title = $request->title;
$post->author = $request->author;
$post->update();
return redirect('/blog');
}

Update Comment in Laravel

I am new at Laravel and I want to use if statement inside Laravel Controller.
So I have a form tag where I can add comments. I want to edit comments in the same form.
Here is my form tag:
<form method="post" action="/hotels/{{$id}}" name="review_hotel">
#csrf
<div class="form-group">
<textarea name="comment" id="comment" class="form-control" style="height:100px"
placeholder="Write your review"></textarea>
#error('comment')
<p class="text-danger">{{$message}}</p>
#enderror
</div>
<input type="submit" value="Submit" class="btn_1" id="submit-review">
</form>
I have a script, with the help of this script I can add a comment that I want to change to the textarea.
After this I have no idea how to update it in my controller.
This is CommentController:
public function comment(CommentRequest $request, $id)
{
$comment = new Comment();
$comment->object_id = $id;
$comment->user_id = Auth::id();
$comment->comment = $request['comment'];
$comment->save();
return redirect('/hotels/'.$id);
}
I think I need to write "if" statement in my controller, but I don't know how.
maybe there is a simpler way to update comment?
On your model Comment.php mark form variables as fillable
public $fillable = ['object_id', 'user_id', 'comment'];
then use this Model function (Eloquent)
Comment::updateOrCreate(
[
'object_id' => ...,
'user_id' => ...,
],
[
'object_id' => ...,
'user_id' => ...,
'comment' => ...,
]
);
https://laravel.com/docs/5.8/eloquent#other-creation-methods

POST method not supported for route in Laravel 6

I am building a discussion form in Laravel 6. The route I used is a POST method and I checked it in route:list. I get the following error, why?
The POST method is not supported for this route. Supported methods:
GET, HEAD, PUT, PATCH, DELETE
View
<form action="{{ route('replies.store', $discussion->slug) }}" method="post">
#csrf
<input type="hidden" name="contents" id="contents">
<trix-editor input="contents"></trix-editor>
<button type="submit" class="btn btn-success btn-sm my-2">
Add Reply
</button>
</form>
Route
Route::resource('discussions/{discussion}/replies', 'RepliesController');
Controller
public function store(CreateReplyRequest $request, Discussion $discussion)
{
auth()->user()->replies()->create([
'contents' => $request->contents,
'discussion_id' => $discussion->id
]);
session()->flash('success', 'Reply Added.');
return redirect()->back();
}
You passed a disccussion object as parameter in order to store user_id within an array.
I think this is not a good practice to store data.
You might notice that your routes/web.php and your html action are fine and use post but you received:
"POST method not supported for route in Laravel 6". This is runtime error. This probably happens when your logic does not make sense for the compiler.
The steps below might help you to accomplish what you want:
1. Eloquent Model(App\Discussion)
protected $fillable = ['contents'];
public function user(){
return $this->belongsTo('App\User');
}
2. Eloquent Model(App\User)
public function discussions(){
return $this->hasMany('App\Discussion');
}
3. Controller
use App\Discussion;
public function store(Request $request){
//validate data
$this->validate($request, [
'contents' => 'required'
]);
//get mass assignable data from the request
$discussion = $request->all();
//store discussion data using discussion object.
Discussion::create($discussion);
session()->flash('success', 'Reply Added.');
return redirect()->back();
}
4. Route(routes/web.php)
Route::post('/replies/store', 'RepliesController#store')->name('replies.store');
5. View
<form action="{{ route('replies.store') }}" method="post">
#csrf
<input type="hidden" name="contents" id="contents">
<trix-editor input="contents"></trix-editor>
<button type="submit" class="btn btn-success btn-sm my-2">
Add Reply
</button>
</form>

post form action to controller in laravel

First of all I want to say that my problem is very similar to this question, only the answer is not working in my case.
I am trying to connect my form action to my UserController. it's an update avatar function. Here's what it look like.
in my profile/show.blade
<div class="col-md-12 justify-content-center">
<form action="{{ action('UsersController#update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
</div>
UsersController
public function avatar()
{
$user = Auth::User();
return view('dashboard.profile'.$user->id,compact('user',$user));
}
public function update_avatar(Request $request){
$request->validate([
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user = Auth::User();
$folder = 'avatars';
Storage::delete($folder.'/'.$user->avatar);
$avatarName = $user->id.'_avatar'.'.'.request()->avatar->getClientOriginalExtension();
$request->avatar->storeAs('avatars',$avatarName);
$user->avatar = $avatarName;
$user->save();
return back()
->with('success','You have successfully upload image.');
}
routes/web.php
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
here's the error I am receiving
Thank you so much in advance!
Change these routes:
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
To these:
Route::get('dashboard/profile/{{profile}}', 'UsersController#avatar')->name('user.avatar');
Route::post('dashboard/profile/{{profile}}', 'UsersController#update_avatar')->name('user.update_avatar');
And in your form use route instead of action
<form action="{{ route('user.update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
You can try do something like this:
Route::post('dashboard/profile','Dashboard\UsersController#update_avatar')->name(profile.update);
and use it in the form like this:
<form action="{{ route('profile.update') }}">

Resources