I have a form in my blade template when I click submit button nothing is happing it is not going to my controller.
this is the form
{{Form::open(['action' => ['HomeController#addcity'], 'method' => 'POST']) }}
{!! Form::select('city_add', array("CAM" => "CAM","KL" => "KL","IPOH" => "IPOH"), 'S',['style'=>'
}'],['class' => 'form-control','placeholder'=>'hotel_name']); !!}
{{Form::text('city_add',"CAM") }}
{{Form::submit('Submit',['class'=>'btn btn-danger'])}}
{!! Form::close() !!}
this is my route
Route::post('/home', 'HomeController#addcity');
My route controller
Your form select element like below:
You should not use same name for the select tag and input tag.
{{Form::open(['action' => ['HomeController#addcity'], 'method' => 'POST']) }}
{!! Form::select('city_option', array("CAM" => "CAM","KL" => "KL","IPOH" => "IPOH"),['class' => 'form-control','placeholder'=>'hotel_name']); !!}
{{Form::text('city_add',"CAM") }}
{{Form::submit('Submit',['class'=>'btn btn-danger'])}}
{!! Form::close() !!}
And In controller( for the test )
public function addcity(Request $request )
{
echo '<pre>';
print_r($request->all());
exit();
}
if you would like to add style in form element use below code:
{{Form::open(['action' => ['HomeController#addcity'], 'method' => 'POST']) }}
{!! Form::select('city_option', array("CAM" => "CAM","KL" => "KL","IPOH" => "IPOH"),'S',['style'=>'color:red'],['class' => 'form-control','placeholder'=>'hotel_name']); !!}
{{Form::text('city_add',"CAM") }}
{{Form::submit('Submit',['class'=>'btn btn-danger'])}}
{!! Form::close() !!}
Related
Im trying to use a form to submit reviews for products but I believe the submit button uses the incorrect controller store method. I have a controller for products and one for reviews. The products store works correctly and I can see the database being populated once submitted however when I go to submit a review for a product it will throw the custom error messages from the product store form. If I change the reviews form::open to the products form::open it will throw an error: The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Products form (works properly)
{!! Form::open(['action' => 'App\Http\Controllers\ProductsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
... labels and text ...
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
Reviews form
<div>
<p>Write a review</p>
<!-- submit review form -->
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
<div class="form-group">
{{ Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Write your message']) }}
</div>
<div class="form-group">
{{ Form::label('rating', 'Rating') }}
{{ Form::select('rating', ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'], '1') }}
</div>
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
</div>
ReviewsController store
public function store(Request $request, $id)
{
$this->validate($request, ['description' => 'nullable',
'rating' => 'nullable',
]);
$review = new Review;
$review->rating = $request->input('rating');
$review->reviewerid = auth()->user()->id;
$review->productid = $id;
$review->description = $request->input('description');
$review->save();
return redirect('/products/$id')->with('success', 'Review submitted');
}
Web.php file
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#abouts');
Route::get('/cart', 'App\Http\Controllers\PagesController#cart');
Route::get('/checkout', 'App\Http\Controllers\PagesController#checkout');
Route::get('/dashboard', 'App\Http\Controllers\PagesController#services');
Route::get('/categories/{Category}', 'App\Http\Controllers\PagesController#category');
Route::resource('reviews', 'App\Http\Controllers\ReviewsController');
Route::resource('products', 'App\Http\Controllers\ProductsController');
Auth::routes();
The error is because of this line:
Form::hidden('_method', 'PUT')
You're telling laravel to use put method. Delete it and it will work fine.
For your form action, I think you have type in this line:
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
Change it to :
{!! Form::open(['action' => 'App\Http\Controllers\ReviewsController#store']) !!}
I define a component for my forms :
/resources/views/components/form.blade.php
{!! Form::model($model, $form) !!}
{{ $slot }}
{!! Form::close() !!}
and in the edit blade file I write some codes like this :
#component('components.form',[
'model' => $user,
'form' => [
'route' => ['users.update', $user],
'method' => 'put',
'class' => 'col-sm-12'
]
])
{!! Form::text('username', null, ['class' => 'form-control']) !!}
#endcomponent
Unfortunately, the fields cannot fill with previous values.
But when I {{ dump($model) }} in /resources/views/components/form.blade.php the variable have a Object value.
What is the problem?
I use Laravel Collective for my forms,
I using a combobox for show list of roles.
this is my code:
{!! Form::model($user, ['route' => ['admin.users', $user->id], 'method' => 'put', 'class' => 'form-horizontal', 'enctype' => 'multipart/form-data']) !!}
{!! Form::select('roles[]', $r_list, null, ['class' => 'form-control', 'multiple' => true]) !!}
{!! Form::close() !!}
When the page load for first time, all values will select witouht any problem, but after returning to form with validation no items selected in list.
what should I do?
Why am I getting this error?
Routes:
Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
Route::get('/',['uses'=>'Admin\IndexController#index','as'=>'adminIndex']);
Route::resource('/products','Admin\ProductController');
});
Form:
{!! Form::open(['url' => route('admin.products.edit',['products'=>$product->id]),'class'=>'form-horizontal','method'=>'POST']) !!}
{{ method_field('EDIT')}}
{!! Form::button('Edit', ['id'=>'submit','type'=>'submit']) !!}
{!! Form::close() !!}
Also, when I'm trying to get list of routes by typing php artisan route:list, I'm getting error:
[Symfony\Component\HttpKernel\Exception\HttpException]
What's the problem?
I think your resource under admin prefix does not take the admin prefix. So you still have to use it without admin prefix. Besides, you are using 'url' instead of 'route'.
{!! Form::open(['url' => '/admin/products/'.$product->id.'/edit'),'class'=>'form-horizontal','method'=>'POST']) !!}
If you want to keep using route
{!! Form::open(['route' => ['products.edit', 'products'=>$product->id], 'class'=>'form-horizontal','method'=>'POST']) !!}
If you are tryint to update instead
{!! Form::model($product, ['method' => 'PATCH', 'route' => ['products.update', $product->id], 'class' => 'form-horizontal' ]) !!}
Since edit method uses GET, change your code to:
{!! Form::open(['route' => ['admin.products.edit', $product->id], 'class' => 'form-horizontal', 'method' => 'GET']) !!}
{!! Form::submit('Edit', ['id' => 'submit']) !!}
{!! Form::close() !!}
Also, since it's GET, you can use simple link:
<a href="{{ route('admin.products.edit', $product->id) }}">
<button class="btn" id="submit">Edit</button>
</a>
look down should be use route 'as'=>'admin.products.edit' and get id also redirect to controller function edit products
Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
Route::get('/edit/{id}/product',
[
'uses'=>'Admin\IndexController#index',
'as'=>'admin.products.edit'
]);
Route::resource('/products','Admin\ProductController');
});
Am new in php laravel and am getting the following error when displaying a page that should be having a form
ErrorException in FormBuilder.php line 525: Undefined offset: 1 (View: E:\mysite\mysite\resources\views\predictions\create.blade.php)
Here is the form code:
#extends('layouts.master')
#section('content')
<h2>Create Predictions</h2>
{!! Form::open(array('route' => 'predictions.store')) !!}
<div class="form-group">
{!! Form::label('title')!!}
{!! Form::text('title',null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('body')!!}
{!! Form::textarea('body',null, array('class' => 'form-control', 'size' => '50*3')) !!}
</div>
{!! Form::token() !!}
{!! Form::submit(null, array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
Here is the formbuilder code with the exception:
protected function setQuickTextAreaSize($options)
{
$segments = explode('x', $options['size']);
return array_merge($options, ['cols' => $segments[0], 'rows' =>
$segments[1]]);
}
Thanks in advance
There is no size attribute for textarea input tag. Remove 'size' => '50*3' from below code
{!! Form::textarea('body',null, array('class' => 'form-control', 'size' => '50*3')) !!}
Use rows and cols attribute instead. Since I guess you're trying to set 3 rows and 50 columns as your textarea box size your code may like this:
{!! Form::textarea('body',null, array('class' => 'form-control', 'rows' => '3', 'cols' => '50')) !!}