I'm working on a laravel project where I need file upload. I have successfully managed to get file upload for the create function working, but for the edit() and update() functions, it won't. The main problem is that the form won't submit. here is the import pits of my form:
<form action="{{route('ads.update', $ad->id)}}" class="form-horizontal" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}"><div class="form-group">
<label for="imageUpload" class="control-label col-md-4">#lang('image upload')</label>
<div class="col-md-6">
<input type="file" name="images[]" class="form-control" multiple="multiple">
</div>
</div><input type="submit" value="أضف التعديل" class="btn btn-success btn-block btn-default">
</form>
Whenever I delete this section of code, the form submits and the update() works flawlessly
<div class="form-group">
<label for="imageUpload" class="control-label col-md-4">#lang('image upload')</label>
<div class="col-md-6">
<input type="file" name="images[]" class="form-control" multiple="multiple">
</div>
Things I've tried:
Changing the enctype to put
Changing the method from PATCH to PUT
Thanks in advance for your help
Related
I am currently working on a 1 page crud for a laravel project. I had a lot of trouble to begin with, but now the last error i have is
Too few arguments to function 0 passed and 1 expected
this happens on loading the main page of the crud. this file gives the error:
edit.blade.php
<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
#method('PATCH')
#csrf
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
</div>
<div class="form-group">
<label for="email">email </label>
<input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
controller index:
public function index($id)
{
$users = User::all();
$userEdit = User::find($id);
return view('admin.user.index', compact('users', 'userEdit'));
}
yes i need $users and $userEdit, but i cant reach $userEdit on the first load of the page. normally there is also no $id in index() but i added it to try to fix it, this did not work
You need to pass the parameter in as an array with a key specifying the route's parameter name. See https://laravel.com/docs/9.x/routing#generating-urls-to-named-routes
<form method="post" action="{{ route('admin.user.update', ['id' => $userEdit->id]) }}">
you have a error in your form:
<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
</div>
<div class="form-group">
<label for="email">email </label>
<input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
method not it´s PATCH it´s PUT and firts send your CSRF
i have problem on my code, i cant pass uploaded file into the request this is my form
<form method="POST" action="{{ route('user.update', ['user' => Auth::user()]) }}" autocomplete="off" enctype="multipart/form-data">
#method('PUT')
#csrf
<div class="form-group focused">
<label for="avatar" class="form-control-label">Profile Pic
<span class="small text-danger">*</span></label>
<input type="file" class="form-control-file" name="avatar" id="avatar" accept=".jpeg,.jpg,.png">
</div></form>
i tried to dump my request variable in my update method in user controller but it return null
dd($request->file('avatar'));
please help me, ill be very helpful
btw sorry for my bad english
View File:
<form method="post" action="{{ route('user.update', ['user' => Auth::user()]) }}" enctype="multipart/form-data">
#method('PATCH')
#csrf
<div class="form-group focused">
<label for="avatar" class="form-control-label">
Profile Pic
<span class="small text-danger">*</span>
</label>
<input type="file" name="avatar" id="avatar">
</div>
</form>
I'm creating a laravel application that allows users to create a blog post.
I have created a PostsController as a resource with store function like this:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
return 123;
}
Also, I added a route in web.php
Route::resource('posts', 'PostsController');
If I list the routes with php artisan php artisan show:routes, POST method is listed:
The HTML form looks like this:
<form action="PostsController#store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
When I submit the form, I get MethodNotAllowedHttpException:
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I used to use laravel collective for forms before. Haven't done any work in laravel for a while and now it seems to be deprecated (https://laravelcollective.com/), so I resorted to classic HTML form. How do I work around this?
Your action is incorrect within your form - you need to point the action to the URL of the route, and then the route will select the method, in this case, the 'store' method. Also add #csrf for more information CSRF Protection
<form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
add the name in textbox and textarea
form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
route ::post()is not working fine on the other hand route::get() is working fine and other method is working, just post method is not working
Route::get('/', 'HomeController#index');
this route is working:
Route::post('/posts', 'Cdesignation#index');
but this route is not working
use same form:
<form action="/form/process" method="POST">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div> <input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<input type="submit" class="btn btn-default" value="Submit">
</form>
how to resolve it
if you notice that the route in the form is /form/process it should be /posts
My question is that how can I upload image if I already have a form?
I would like to upload with only one button click.
<form class="form-horizontal" role="form" method="post" action="/controller/method">
<div class="form-group">
<label for="test" class="col-xs-8 control-label">Test:</label>
<div class="col-xs-8">
<input type="text" class="form-control" id="test" name="test" placeholder="" value="">
</div>
</div>
<div class="form-group">
<label for="file" class="col-xs-8 control-label">File:</label>
<div class="col-xs-8">
<input type="file" id="file" name="file" class="file">
</div>
</div>
This is only post the selected file name. I have to validate every input and if there is a way I do not want to create new controller function.
Using this coding u can upload the file[view]
<div class="form-group">
<label for="exampleInputFile">Image Upload</label>
<input id="myfile" name="myFile" type="file" />
<label id="upload-error" style="color:red;display: none;">Choose File</label>
</div>
For controller
if($this->input->post('select'))
{
$image=array();
$image_title= $this->input->post('title');
$folder="./img/uploadimg/";
if(is_uploaded_file($_FILES['myFile']['tmp_name']))
{
if(copy($_FILES['myFile']['tmp_name'],$folder.$_FILES['myFile']['name']))
{
$source= $folder.$_FILES['myFile']['name'];
$source_image=$_FILES['myFile']['name'];
$img_path=explode(".", $source_image);
}
$image['filepath']=$source;
$image['filename']= $this->input->post('title');
}