Cannot bind models in components - php

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?

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

My laravel collective form is not submitting when I click submit

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() !!}

No records selected in combobox after validation

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?

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.

Laravel methodNotAllowed on post

I'm not sure why it's not working...
I'm trying to update a user and I keep getting method not allowed error exception.
-- routes
Route::get('superadmin/users', ['as' => 'superadmin.users', 'uses' => 'SuperAdminController#usersIndex']);
Route::post('superadmin/users/{id}', ['as' => 'superadmin.editUser', 'uses' => 'SuperAdminController#editUser']);
-- controller
public function usersIndex()
{
$users = User::all();
return View::make('superadmin.users',compact('users'));
}
public function editUser($id)
{
$user = User::findOrFail($id);
$user->email = Input::get('email');
$user->save();
return Redirect::route('superadmin.users')->with('alertsuccess', 'User has been updated.');
}
-- view
{{ Form::model($user, ['method' => 'PATCH', 'route' => ['superadmin.editUser', $user->id], 'class' => 'form']) }}
<div class="form-group">
{{ Form::label('email', 'Email:', ['class' => 'placeholder-hidden']) }}
{{ Form::text('email', Input::old('email'), ['class' => 'form-control']) }}
</div>
{{ Form::submit('Update User', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}
This is most probably as you need to set up a resource controller in order to use the PATCH method. Try using POST instead.

Categories