I got this error when I access to router:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException No message
I tried to find out that but I couldn't fix it.
Router(web.php)
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
View(subdomains.account.pages.test.blade.php)
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Controller
namespace Ares\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
The problem is that code always use the GET method instead of POST.
How can I fix it?
EDIT: I just found cause this issue is because I don't use Laravel using php artisan serve, and I only have a virtual host in the public server, how I can solve this without using artisan serve?
FIX: (EDITED)
The issue is by forcing the trailing slash at the end of the URL.
Try This:
FormController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
subdomains/account/pages/test.blade.php
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
web.php
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
Output: When you use your route /test the test button will be displayed on the output page
When you click on the test button, it will be redirect to /testForm page and get the output test
You should try this:
{!! Form::open(['url' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Updated answer
Route::post('/testForm', 'FormController#store')->name('testform.store');
{!! Form::open(['route' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Try to use code below for subdomains.account.pages.test
{!! Form::open(['action' => [ '\Ares\Http\Controllers\FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
You should remove closer from your routes,
Route::get('/test', 'FromController#index');
Route::post('/testForm', 'FormController#store');
then run,
php artisan route:clear
Then submit your form
Related
I´ve this error: The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
But i´m not trying to delete something
My view
{!! BootForm::open(['id'=>'form', 'method' => 'POST','route'=>['declaracionenviar.enviarTodo']]); !!}
{!! BootForm::checkboxElement('ids[]', ' ', $declaracion->id, '', false, ["class"=>'check i-checks']); !!}
{!! Form::submit('Send something',['class' => 'btn btn-primary']); !!}
{!! BootForm::close() !!}
My route
Route::post('/declaracionenviar/enviarTodo','DeclaracionEnviarController#enviarTodo')->name('declaracionenviar.enviarTodo');
My controller
public function enviarTodo(Request $request)
{
$ids= $request->get('ids');
dd($ids);
}
I would appreciate some help!
I wish to make search query by datepicker and select field.
How could I get the requests values from below view file to controller?
Where could I modify in the code? thanks.
index.blade.php
<div class="form-group col-sm-6">
{!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/pdfs/job_finished_search'))) !!}
{!! Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) !!}
{!! Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) !!}
{!! Form::select('customer_name', $jobs->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
{!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!}
{!! Form::close() !!}
</div>
Controller.php
public function job_finished_search(Request $request, Job $jobs)
{
$jobs = Job::onlyTrashed()
->whereBetween('created_at', array(
(Carbon::parse($request->input('datepicker_from'))->startOfDay()),
(Carbon::parse($request->input('datepicker_to'))->endOfDay())))
->where('customer_name', 'like', '%'.$request->customer_name.'%')
->orderBy('deleted_at', 'desc')
->paginate(15);
if (empty($jobs)){
Flash::error('Search result not found');
}
return view('pdfs.index', ['jobs' => $jobs]);
}
There are multiple way to get the request data e.g to get datepicker_from value you can use any of the below
$request->datepicker_from
$request->input('datepicker_from')
$request->get('datepicker_from')
choose the one you like the most
refer to https://laravel.com/docs/5.5/requests
To get request values you can use the get method, try:
$customer = $request->get('customer_name','default_value');
To get request values, you can set an object like $input= $request->all(). Then you can make use of the object which is an array to get at specific fields, e.g to access your date picker, you can write $input['datepicker_from']. You need to place $input= $request->all() before you declare the $jobs object in your code.
I can't seem to find a way to put image as submit button in blade, is there a way to do this?
{!! Form::submit('Search', array('class'=>'btn')) !!}
Form::input should do the trick:
{!! Form::input('image', 'Search', array('class'=>'btn', 'src' => '...')) !!}
You can create the submit with pure html instead of laravelcollective like that
<input type="image" src="sourse for image" width="48" height="48" alt="Submit" />
For Laravel Collective version 6.2 the solution This small example shows the sending of a variable by means of a button with an image (src='../path/to/file) using the 'submit' passing as parameters the dimensions of the button (30,30).
Note: var_to_send can have a string like $ansver - > 'Hello World' or another value and it will pass it from the controller and it will print it.
// new.blade.php
{!! Form::open([ 'action'=> 'StorageController#save', 'method' => 'POST', 'files' => true]) !!}
{!! Form::hidden('var_to_send', $answer) !!}
{!! Form::image(url('../img/A.png'), 'submit', ['width' => '30', 'height' => '30']) !!}
{!! Form::close() !!}
// StorageController.php
class StorageController extends Controller
{
public function save(Request $request)
{
return $request->input('var_to_send');
}
}
// route wep.php
Route::post('storage/create', 'StorageController#save');
I want to set the placeholder value for my form element. Here is my controller:
class MyController extends Controller {
public function index(Request $request){
$start_time = $request->get('start_date');
return view('showChart',compact('start_time'));
}
}
The controller will send response to showChart.blade.php. Part of the showChart.blade.php is:
{!! Form::open(array('url' => '/', 'class' => 'form')) !!}
{!! Form::label('START DATE') !!}
{!! Form::text('start_date', null,
array('required',
'placeholder'=>{{start_time or 'default value'}}
))
!!}
{!! Form::close() !!}
It does not work as expected, it will output: , it seems that the code does not execute.
You can’t use template delimiters once inside an already-open set. You’ll have to do something like this instead:
{!! Form::text('start_date', null, ['placeholder' => empty($start_date) ? 'default value' : $start_date, 'required' ]) !!}
I'm not sure if i understand every thing but did you have a white screen ? That means a blade error.
{!! Form::text('start_date', null,
array('required',
'placeholder'=>{{start_time or 'default value'}}
))
!!}
Remove {{ }} and replace start_time by $start_time.
And about your default value, you can put it into $start_time in your controller or before in your view.
I had created a form like the following in my view
{!! Form::open(array('url' => 'user/posts', 'files'=> true)) !!}
<div class="form-group">
{!! Form::text('title',Input::old('title'),['class'=>'form-control', 'placeholder' => 'What is your title?']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Your Image') !!}
{!! Form::file('image') !!}
<p class="help-block">Hey! Please don't upload over 15MB images!</p>
</div>
<div class="form-group">
{!! Form::label('caption', 'Don\'t miss to caption!') !!}
{!! Form::text('caption',Input::old('caption'),['class'=>'form-control', 'placeholder' => 'Your caption']) !!}
</div>
{!! Form::submit('Post now!',['class'=>'btn btn-info']) !!}
{!!Form::close() !!}
And I try dd() for my image from my controller but it return nulll. The following one is my Controller
public function store(PostFormRequest $request)
{
dd($request->input('image'));
$post = new Post;
}
The following one is my PostFormRequest
<?php namespace App\Http\Requests;
use Response;
use Illuminate\Foundation\Http\FormRequest;
class PostFormRequest extends FormRequest {
public function rules()
{
return [
'title' => 'required',
'image' => 'mimes:jpeg,bmp,png'
];
}
public function authorize()
{
$image = $this->image;
if(empty($image))
return true;
else
return false;
}
public function forbiddenResponse()
{
return Response::make('Sorry!',403);
}
}
When I try dd($request->input('title')); it's return string(4) "test" what I was wrong?
For files you need to use:
dd($request->file('image'));
and not
dd($request->input('image'));
Reference
Faced with the same problem. One more little thing: if you use native HTML form submit you need also set 'enctype="multipart/form-data"'. Otherwise $request->file('INPUT_NAME') returns string instead the file.