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>
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'm just trying to insert title and body using eloquent on database and MethodNotAllowedHttpException appears wont let me do it cause of that error
Here's the controller
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
Here's my view create.blade.php
<form action="post.store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
i am assuming your are using resource route.
i think this <form action="post.store" method="POST"> should be <form action="{{ route('post.store') }}" method="POST"> and use this too {{ csrf_field() }} inside form otherwise you will get tokenmismatch error.
<form action="{{ route('posts.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
You may use command php artisan route:list and in column Name you will see route name and you will be able to call in view by {{ route('routename') }}
Example:
In route list is shown posts.store, so you will call in view {{ route('posts.store') }}
I think you may call incorrect route by above route view is posts.show but when insert you called post.store maybe posts.store
use in form action {{ route('posts.store') }}
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
am try to send request by post form from but error TokenMismatchException
here is my controller code
public function postSaveedit(Request $request) {
$var1 = $request->input('title'); //name on the form
$var2 = $request->input('meaning'); //name on the form
$words = User::where("title", $var1)->update(array("meaning" => $var2));
return view('dict.watch', compact('words'));
}
here is view code.
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>
thnak you for your help
From https://laravel.com/docs/master/routing:
Laravel automatically generates a CSRF "token" for each active user
session managed by the application. This token is used to verify that
the authenticated user is the one actually making the requests to the
application.
Anytime you define a HTML form in your application, you should include
a hidden CSRF token field in the form so that the CSRF protection
middleware will be able to validate the request. To generate a hidden
input field _token containing the CSRF token, you may use the
csrf_field helper function
Just add this line inside your form
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<input type="hidden" name="_token" value="{{ csrf_token(); }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>
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');
}