Laravel 5 Image upload - laravel

I use Laravel 5.1 and want to upload a image within my form:
blade:
<form method="POST" action="{{ url('upload') }}" enctype="multipart/form-data">
<input name="picture" type="file" class="uploader" id="picture" value="Upload" />
<button type="submit">Add</button>
</form>
My route:
Route::post('upload', 'MyController#upload');
My controller:
public function upload(Request $request){
if ($request->hasFile('picture'){}
}
The problem is that the if statement always return false. Any ideas why? Thank you

Related

a form with the PUT method is sent with the GET method

I have 2 controller with resource clients and sumsubs.
being on the clients.show route I created a form that I would like to send to the sumsubs.update endpoint but it does not work; the form still contacts sumsubs.show
here are the routes that I have defined
here is the form
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="PUT" action="{{ route('sumsubs.update', $client->id) }}">
#csrf
<div class="form-group row">
<div class="col-sm-10">
<input type="number" class="form-control" id="candidat" name="candidat" value="{{ $client->id }}" hidden required>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success float-right"> {{ __('Recheck') }} </button>
</div>
</form>
the following is my sumbsubs controller
public function show($id)
{
return 'not ok';
}
public function update(Request $request, $id)
{
return 'ok ';
}
this is what i get when i submit the form
I would like to know where is my mistake
I read the laravel documentation for form submission and tried with url and route methods but I still get the same result
You have to use method spoofing.
Change your method in form tag to Post:
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="POST" action="{{ route('sumsubs.update', $client->id) }}">
and add this after #csrf:
#method('PUT')

The GET method is not supported for this route. Supported methods: POST in laravel ...is there something i'm missing?

blade file :
<form action="{{URL::to('/admin/sender')}}"method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Controller:
public function notificationSender(Request $request)
{
$text= request()->text;
print_r($request->input());
event(new OrderComplete($text));
return view('admin.sender');
}
Route:
Route::post('/sender','HomeController#notificationSender');
the route is a subroute for a group..is there something i'm missing?
Give space between method and route parameters here
if your laravel version is 5 then use this
<form action="{{URL::to('/admin/sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Or else you can also name the route to make it easy to pass in action parameter of form like this.
Route::post('/sender','HomeController#notificationSender')->name('sender');
Then you can pass it in form like this
<form action="{{route('sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
For GET Method (Laravel 6)
blade file
<form method="GET" action="{{ route('sender') }}" enctype="multipart/form-data" >
#csrf
<input type="text" name="text">
<input type="submit">
</form>
Write Controller
public function notificationSender(Request $request)
{
$text= $request->get('text');
echo "<pre>"; print_r($text);
event(new OrderComplete($text));
return view('admin.sender');
}
Route
Route::get('sender','HomeController#notificationSender');

The POST method is not supported for this route. Supported methods: GET, HEAD

Iam trying to update a specific data using post method. After submitting the form it shows an error : The POST method is not supported for this route. Supported methods: GET, HEAD.
editpage.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<h3>Update Book</h3>
<br>
<form action="update" method="post" >
{{csrf_field()}}
#foreach($array as $fetch)
<div><input type="hidden" name="id" value="{{$fetch->id}}"></div>
<div><input type="text" name="name" class="form-control " placeholder="Bookname" value="{{$fetch->name}}" ></div><br>
<div><textarea name="content" class="form-control" rows="5" placeholder="Description" >{{$fetch->content}}</textarea></div><br>
<div><input type="text" name="author" class="form-control" placeholder="Author" value="{{$fetch->author}}"></div> <br>
<div><input type="submit" name="submit" value="Update Book" class="btn btn-success" ></div>
#endforeach
</form>
</div>
#endsection
Web Route
Route::get('/', function () {
return view('welcome');
});
Route::get('/addbook',function () {
return view('AddBook');
});
Route::post('/insert',['uses'=>'BookController#insert']);
Route::get('/delete/{id}',['uses'=>'BookController#delete']);
Route::get('/edit/{id}',['uses'=>'BookController#edit']);
``````
Route::post('/update',['uses'=>'BookController#update']);
```````
Route::get('/home',['uses'=>'BookController#index']);
Auth::routes();
There are action like update which requires the method submitted to the server url to be either PUT/PATCH (to modify the resource)
Try with this,
<form action="{{ route('book.update') }}" method="post" >
{{csrf_field()}}
{{ method_field('PUT') }}
#foreach($array as $fetch)
// ...
#endforeach
</form>
Your Route,
Route::put('update',['uses'=>'BookController#update', 'as' => 'book.update']);
Your Controller
public function update(Request $request)
{
// ...
}
Hope this helps :)

submit form can not access method in controller

When I submit form in view, I can't access that in controller.
View create.blade.php
#extends('layouts.app')
#section('content')
<form method="POST" action="posts">
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
Controller PostController
public function store(Request $request)
{
return $request->all();
}
I want to access the form data, in PostController in the store method and once I click submit, it not giving the result.
I have route: Route::resource('posts', 'PostController') and I am posting to this route/url, as action="posts". In Route I have used resource and in method="post"
Does it matter? What can I do?
Looks like you are missing the action
Route::post('post', 'PostController#store');
I'm not sure but may be it's because of url and not included csrf_field(). Try changing it to:
<form method="POST" action="{{ url('posts') }}">
{{ csrf_field() }}
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
or you may use route('posts.store ') instead of url('posts')

Call to a member function store() on null - laravel 5.4

I'm trying to upload an image though everytime I submit it's returning that the store() on null error. I've set the form to enctype="multipart/form-data" which hasn't helped.
Can anyone point me in the right direction?
Thanks.
Function inside the controller
public function store(Request $request){
$file = $request->file('imgUpload1')->store('images');
return back();
}
Form below:
<form action="/imgupload" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="imgUpload1">File input</label>
<input type="file" id="imgUpload1">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
I had the same issue what I did to fix is in the opening form tag add enctype="multipart/form-data" that should fix it. Without it laravel would not understand the file.
like:
<form method="POST" enctype="multipart/form-data" name="formName">
The data is always fetched with name attribute which is missing in your form input
Change
<input type="file" id="imgUpload1">
to
<input type="file" id="imgUpload1" name = "imgUpload1">
and do some validation in the controller side like this
$val = Validator:make($request->all, [
'imgUpload1' => 'required',
]);
if($val->fails()) {
return redirect()->back()->with(['message' => 'No file received']);
}
else {
$file = $request->file('imgUpload1')->store('images');
return redirect()->back();
}
you need add this code in your controller
if ($request->file('imgUpload1') == null) {
$file = "";
}else{
$file = $request->file('imgUpload1')->store('images');
}
you are getting error because your store function is not seeing the file from your request from the input tag so to fix this set the "name" just I have done below
<form action="/imgupload" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="imgUpload1">File input</label>
<input type="file" id="imgUpload1" name="imgUpload1">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

Resources