No records selected in combobox after validation - php

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?

Related

laravel form submit uses wrong controller method

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']) !!}

Cannot bind models in components

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?

Two forms same page - one returns null the other works

When I run $value = $request->session()->all(); Controller one gives the value 'product' as null. The second gives the right product id. They are on the same page. Both are forms within bootstrap modals. Why are they giving different session data? Stumped.
the form is submitted from a product page -> the id i want isnt submitted from the form -> the id is from the product page
Controller One
{!! Form::open(['action' => 'ControllerOne#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
{{Form::label('time', 'Select a Time')}}
{{Form::time('time', '', ['class' => 'form-control', 'placeholder' => 'Time'])}}
{{Form::label('date', 'Select a Date')}}
{{Form::text('date', '', ['class' => 'form-control', 'placeholder' => 'Date'])}}
<div class="modal-footer">
{{Form::submit('Add', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
Controller Two
{!! Form::open(['action' => 'ControllerTwo#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
{{Form::label('listing', 'Price (NZD)')}}
{{Form::text('price', '', ['class' => 'form-control', 'placeholder' => 'Price'])}}
{{Form::label('listing', 'Name')}}
{{Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'Name'])}}
{{Form::label('listing', 'Phone Number')}}
{{Form::number('phone_number', '', ['class' => 'form-control', 'placeholder' => 'Phone Number'])}}
{{Form::label('listing', 'Comments/Conditions')}}
{{Form::textarea('conditions', '', ['class' => 'form-control', 'placeholder' => 'Comments/Conditions'])}}
{{Form::submit('Submit', ['class'=>'btn btn-success'])}}
{!! Form::close() !!}

populate select in Laravel form with value from model

I made form with select. Here is code:
{!! Form::model($customerAccount, ['route' => ['admin.customerAccount.update',
$customerAccount->id_customer_account], 'method' => 'PUT', 'class' => 'form-horizontal']) !!}
...
<div class="#if ($errors->has('cacct_active'))has-error has-feedback #endif form-group">
{!! Form::label('cacct_active', 'Status: *',['class' => 'col-md-4 control-label']) !!}
<div class="col-md-8">
{!! Form::select('cacct_active',$customerAccount->getStatusList(),null,['class'=>
'form-control', 'required' => 'required', 'autofocus' => 'none']) !!}
</div>
</div>
I have options loaded by $customerAccount->getStatusList() and want default selected option to be value from my object. But it does not happen, selected is just the first one from options.
EDIT
the problem was I was using 'true, false' values in db and then '0, 1' values in 'statusList' parameter - the easiest way to solve this was to use mutator 'casts' in model for this field

Laravel 5 Dynamic clause WHERE from Form array

y have this Controller with GET vars:
localhost/ordersys/public/admin/orders?provid=220001&price=500
{!! Form::open(array('action' => array('Admin\OrdersController#filter'), 'role'=>'search', 'method' => 'GET')) !!}
{!! Form::text('provid', null, array('class' => 'typeahead form-group form-control', 'placeholder' => 'Search by Provid here...')) !!}
{!! Form::text('price', null, array('class' => 'typeahead form-group form-control', 'placeholder' => 'Price max ...')) !!}
{!! Form::submit('Search', array('class' => 'btn btn-default search-bar-btn')) !!}
{!! Form::close() !!}
$varprovid = Input::get('provid');
$varprice = Input::get('price');
$collection = DB::table('orders')
->where('cod_prov', $varprovid)
->where('price', '<', $varprice)
->paginate(15);
This work, but how can catch the Inputs Input::get('provid'), Input::get('price') from Form and filter Collection using Where clauses dynamically. I can build array and use foreach loop? Any idea please, thanks.

Categories