The controller is not getting the data from the FORM. I realise that the Form has by default a Post method, while the Route is using a Get, but if I change that, then the form will not display the form fields. Validation fails as the "required" does not get any values, so it returns to the same page. If I remove the validation filter, then it does go to the results page, but all it does is show ALL of the content of the table, since it is getting no parameters (where) from the Form. The weird thing is that in the past, it worked, but I must have messed up with some part of the code and now it doesn't. To save space here I have left out many fields which dont play a role in the problem.
The Form has three interdependent Fields Country, Region and Town, which are filled up alright.
FORM:
<form action = "{{URL::route('sacapropiedades')}} "class="form-horizontal" id="my_form" name="my_form">
<div class="form-group">
<div class="col-sm-3">
<label for="country">Pays</label>
<select name ="country" {{ (Input::old('country')) ?' value ="' . e(Input::old('country')). '"' : '' }} id = "country" class="form-control">
#foreach($countries as $country)
<option value="{{$country->country}}">{{$country->country}}</option>
#endforeach
</select>
</div>
<div class="col-sm-3">
<label for="town">Ville</label>
<select name ="town" {{ (Input::old('town')) ?' value ="' . e(Input::old('town')). '"' : '' }}id = "town" class="form-control">
</select>
</div>
</div><!-- END OF THIRD FORMGROUP -->
<div class="form-group">
<div class="col-sm-4">
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-success">Enviar</button>
<button type="reset" class="btn btn-danger">Borrar</button>
</div>
</div>
</form>
ROUTES
Route::get('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
CONTROLLER
public function findproperty(){
/*IT REPEATS THE COUNTRY QUERY ABOVE BECAUSE IT IS GOING TO USE IT
*ON THE RESULTS PAGE AND IT GIVES THE USER TO SELECT AGAIN OTHER COUNTRIES
*WITHOUT HAVING TO RETURN TO THE FIRST PAST PAGE*/
$countries = DB::table('properties')
->select('country')
->distinct()
->get();
/*FIRST VALIDATE INPUT DATA*/
$validator = Validator::make(Input::all(),
array(
'country' =>'required',
'regions' =>'required',
'transaction' =>'required',
'town' =>'required'
));
if($validator->fails()){
return Redirect::route('showrealestate')
->withErrors($validator)
->withInput();
}
else{
$country = Input::get('country');
$region = Input::get('regions');
$town = Input::get('town');
$transaction = Input::get('transaction');
$pricefrom = Input::get('pricefrom');
$priceto = Input::get('priceto');
$roomsfrom = Input::get('roomsfrom');
$roomsto = Input::get('roomsto');
$builtyear = Input::get('builtyear');
$swimming = Input::get('swimming');
$garden = Input::get('garden');
$garage = Input::get('garage');
$message = Input::get('message');
}
$country = DB::table('countries')->where('id_pais', $country)->pluck('nombre_pais');
$region = DB::table('regions')->where('id_region', $region)->pluck('nombre_region');
$town = DB::table('cities')->where('id_localidad', $town)->pluck('nombre_localidad');
$users = DB::table('users')
->join('properties', 'users.id', '=', 'properties.id_user_fk')
->select('users.email', 'properties.id_user_fk', 'properties.country', 'properties.region', 'properties.town',
'properties.price', 'properties.rooms','properties.m2','properties.swimming',
'properties.garden','properties.garage','properties.builtyear','properties.message',
'properties.pic1',
'properties.pic2', 'properties.pic3','properties.pic4','properties.pic5','properties.pic6');
if (!empty($country)) {
$users = $users->where('country', '=', $country);
}
if (!empty($region)) {
$users = $users->where('region', '=', $region);
}
if (!empty($town)) {
$users = $users->where('town', '=', $town);
}
if (!empty($transaction)) {
$users = $users->where('transaction', '=', $transaction);
}
if (!empty($pricefrom)) {
$users = $users->where('price', '>', $pricefrom);
}
if (!empty($priceto)) {
$users = $users->where('price', '<', $priceto);
}
if (!empty($roomsfrom)) {
$users = $users->where('rooms', '>', $roomsfrom);
}
if (!empty($roomsto)) {
$users = $users->where('rooms', '<', $roomsto);
}
if (!empty($builtyear)) {
$users = $users->where('builtyear', '>', $builtyear);
}
if (!empty($swimming)) {
$users = $users->where('swimming', '=', $swimming);
}
if (!empty($garage)) {
$users = $users->where('garage', '=', $garage);
}
if (!empty($garden)) {
$users = $users->where('garden', '=', $garden);
}
if (!empty($message)) {
$users = $users->where('message', '=', $message);
}
$users = $users->get();
return View::make('realestate.externa.listproperty', compact('users','countries'));
}
A post method is mandatory, otherwise Laravel will not redirect it to the correct method with the correct data. How was it working before? By luck, probably. :)
Route::get('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
Route::post('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
or
Route::match(array('GET', 'POST'), 'realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
or
Route::any('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
Then you'll probably need to not validate on GET:
if (Request::getMethod() == 'POST')
{
$validator = Validator::...
}
EDIT:
Sorry I overlooked this problem:
Instead of writing your FORM tag manually, use Laravel's FormBuilder class:
<?php Form::open(array('route' => 'sacapropiedades', 'class' => 'form-horizontal', 'id' => 'my_form', 'name' => 'my_form')); ?>
The difference is that it will add the method for you and it will also add a csrf token to your form.
Related
I am creating an online bookstore in Laravel, and upon creating a new book, the administrator is able to define which warehouses that are able to stock this book, by checking the specific warehouses checkboxes.
To give insight in how it works, this is my create function:
public function create()
{
$authors = Author::all();
$selectedAuthor = Book::first()->author_id;
$publishers = Publisher::all();
$selectedPublisher = Book::first()->publisher_id;
$warehouses = Warehouse::all();
$selectedWarehouse = Book::first()->warehouse_id;
return view('books.create', compact(['authors', 'publishers', 'warehouses'],
['selectedAuthor', 'selectedPublisher', 'selectedWarehouse']
));
}
and my store method:
public function store(Request $request)
{
$request->validate([
'ISBN' => 'required',
'author_id' => 'required',
'publisher_id' => 'required',
'year' => 'required',
'title' => 'required',
'price' => 'required',
]);
try {
$book = Book::create($request->all());
foreach ($request->checked as $value){
$book->warehouses()->attach([$value]);
}
return redirect()->route('books.index')
->with('success','Book created successfully.');
} catch (\Illuminate\Database\QueryException $e) {
var_dump($e->errorInfo);
}
}
But when an administrator edits a book, the checkboxes that were checked upon creating the book, should be "checked", and the administrator should be able to attach more warehouses, and be able to "unselect" a warehouse, so if an already checked value gets unchecked and sumbitted, it should get detached from the many-to-many table.
This is what i currently have:
My edit method:
public function edit(Book $book)
{
$authors = Author::all();
$selectedAuthor = Book::first()->author_id;
$publishers = Publisher::all();
$selectedPublisher = Book::first()->publisher_id;
$warehouses = Warehouse::all();
$selectedWarehouse = Book::first()->warehouse_id;
return view('books.edit', compact(['book', 'authors', 'publishers', 'warehouses'],
['selectedAuthor', 'selectedPublisher', 'selectedWarehouse']));
}
And my update method:
public function update(Request $request, Book $book)
{
$request->validate([
'ISBN' => 'required',
'publisher_id' => 'required',
'author_id' => 'required',
'year' => 'required',
'title' => 'required',
'price' => 'required',
]);
try {
$book->update($request->all());
// TODO: Update warehouses
return redirect()->route('books.index')
->with('success','Book updated successfully.');
} catch (\Illuminate\Database\QueryException $e) {
var_dump($e->errorInfo);
}
}
And the checkboxes in my edit.blade view:
#foreach($warehouses as $warehouse)
<input type="checkbox" name="checked[]" value="{{ $warehouse->id }}">
{{ $warehouse->address }}
<br/>
#endforeach
My Book model:
public function warehouses()
{
return $this->belongsToMany(Warehouse::class);
}
And my warehouse model:
public function books()
{
return $this->belongsToMany(Book::class);
}
Any help on being able to attach / detach upon editing an existing book, would be highly appreciated!
Try this on create and update method for storing
// Your method
foreach ($request->checked as $value){
$book->warehouses()->attach([$value]);
}
// Try This
$book->warehouses()->sync($request->checked); // $request->checked must be an array
Update Blade
#foreach($warehouses as $warehouse)
<input #if($book->warehouses()->where('warehouse_id', $warehouse->id)->exists()) checked #endif type="checkbox" name="checked[]" value="{{ $warehouse->id }}">
{{ $warehouse->address }}
<br/>
#endforeach
I will left this example with a logic according your problem. In this case are roles:
public function edit(Role $role){
//get roles ids
$permission_role = [];
foreach($role->permissions as $permission){
$permission_role[] = $permission->id;
}
//get permissions
$permissions = Permission::all();
return view("role.edit", compact('role', 'permission_role', 'permissions'));
}
In the blade:
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label>Select the permissions for the current role</label>
#foreach ($permissions as $permission)
<div class="valid-feedback d-block" style="font-size: 15px !important;">
<input type="checkbox" value="{{ $permission->id }}" name="permissions[]"
#if(is_array(old('permissions')) && in_array("$permission->id", old('permissions')))
checked
#elseif(is_array($permission_role) && in_array("$permission->id", $permission_role))
checked
#endif>
<strong> {{ $permission->description }} </strong>
</div>
#endforeach
</div>
<div class="invalid-feedback d-block">
#foreach ($errors->get('permissions') as $error)
{{ $error }}
#endforeach
</div>
</div>
</div>
Of this way you can also keep the old checkboxes when nothing is select. You should validate it as required.
So i have a form with multiple fields like below
<ul class="list-group list-group-flush">
#foreach ($group as $perm)
<li class="list-group-item">{{$perm->name}}
<div class="float-right">
<select name="perms[{{$perm->id}}]" class="form-control">
<option value="1">Yes</option>
<option value="0" selected>No</option>
</select>
</div>
</li>
#endforeach
</ul>
My controller is like below
public function permission(int $id)
{
$permission = request()->perms;
foreach ($permission as $perm => $status)
{
if($status == 1)
{
//echo $perm . " " . $status;
$user_perm = User_perms::create([
'user_id' => $id,
'perm_id' => $perm,
]);
}
}
$user = Users::find($id);
return redirect($user->path());
}
This code does what I want but I have a test
public function permissions_applied_for_user()
{
$this->withoutExceptionHandling();
//create a user
$this->post('/users/add', $this->data());
$user = Users::first();
//first clear out all data from user_perm table for specific user
$response = $this->post('/users/permission/' . $user->id, [
'user_id' => $user->id,
'perm_id' => '1',
]);
$this->assertCount(1, User_perms::all());
$response->assertRedirect('/users/view/' . $user->id);
//$response->assertOk();
//second insert all new permissions into the table
}
which throws the exception invalid argument supplied for foreach any advice on what I'm doing wrong?
I have some hard time with many to many relationship when I try to update the pivot columns.
Here is my database 'order_product' table
order_product_table
First, I am trying to update the products of that order.
Order update form
Here is the HTML:
#if($products)
<select class="form-control kt-select2 products" name="products[]" required>
<option selected disabled>Select a product</option>
#foreach($products as $product)
<option value="{{ $product->id }}" data-price="{{ $product->selling_price }}" {{ $product->id === $order_product->id ? 'selected' : '' }}>{{ $product->name }}</option>
#endforeach
</select>
#endif
My spaghetti code...
public function update(Order $order)
{
$attributes = $this->validateOrder();
$order->update($attributes);
$products = \request('products');
$quantity = \request('quantity');
$price = \request('price');
$discount = \request('discount');
$total = 0;
if ($products) {
$order->products()->sync([$order->id => ['product_id' => $products]]);
}
$this->flashMessage('success', 'Your order was updated with success!');
return redirect()->back();
}
Try a lot of things, but it does not work ...
Edit your code like this:
public function update(Order $order)
{
$attributes = $this->validateOrder();
$order->update($attributes);
//should be an array of products ID
$products = \request('products');
$quantity = \request('quantity');
$price = \request('price');
$discount = \request('discount');
$total = 0;
foreach($products as $key => $productId) {
//normalize attributes
$attributes = [
'price' => $price[$key],
'quantity' => $quantity[$key],
'discount' => $discount[$key],
];
$order->products()->updateExistingPivot($productId, $attributes);
}
$this->flashMessage('success', 'Your order was updated with success!');
return redirect()->back();
}
For more information about updateExistingPivot() method, check Laravel documention
For the sync() method in many to many relation, you need to provide the relation ID as index of the array.
$syncable = [];
foreach ($products as $key => $productId) {
$syncable[$productId] = [
'price' => $attributes['price'][$key],
'quantity' => $attributes['quantity'][$key],
'discount' => $attributes['discount'][$key]
]
}
if ($syncable) {
$order->products()->sync($syncable);
}
I am using Laravel 5.4
my create form is multi feild for multi language. name of field in my form is variable:
{!! Form::open(['route' => 'pages.store','files'=>true]) !!}
#if( isset($languages) && $languages->count() > 0 )
#foreach($languages as $language)
<div class="form-group">
{!! Form::label('subject_'.$language->code, 'subject in '.$language->name) !!}
<div class="form-line">
{!! Form::text('subject_'.$language->code,old('subject'),['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
<!-- TinyMCE -->
{!! Form::textarea('content_'.$language->code,'',['class'=>'tinymce']) !!}
<!-- #END# TinyMCE -->
</div>
#if (!$loop->last)
<hr class="style18">
#endif
#endforeach
<div class="form-group">
{!! Form::submit('save change',['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
and my control code:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'subject' => 'required|max:255',
'content' => 'required|max:255',
]);
$languages = Languages::all();
$page = new Page();
$page->save(); // Eloquent
foreach ($languages as $language) {
$pageTrans = new PageTrans();
$pageTrans['page_id']= $page->id;
$pageTrans['locale'] = $language->locale;
$pageTrans['subject'] = $request->input('subject_' . $language->code);
$pageTrans['content'] = $request->input('content_' . $language->code);
$pageTrans->save(); // Eloquent
}
return redirect(route('pages.index'));
}
but problem with validat and not detect feild name. Do you know of a solution or a better way to do this?
You can change your controller same this. I didn't test. Please let me know if have any problem on that.
$languages = Languages::all();
if( $languages->count() ){
$data = [];
foreach ($languages as $language) {
$data['subject_'.$language->code] = 'required|max:255';
$data['content_'.$language->code] = 'required';
}
$validator = Validator::make($request->all(), $data);
if ($validator->fails()) {
return redirect(route('pages.store'))
->withErrors($validator)
->withInput();
}else{
$page = new Page();
$page->save(); // Eloquent
foreach ($languages as $language) {
$pageTrans = new PageTrans();
$pageTrans['page_id']= $page->id;
$pageTrans['locale'] = $language->locale;
$pageTrans['subject'] = $request->input('subject_' . $language->code);
$pageTrans['content'] = $request->input('content_' . $language->code);
$pageTrans->save(); // Eloquent
}
return redirect(route('pages.index'));
}
}
Thank you!
I made form in which i have created fields for days of week, which i stored in database as serialize. When I try to edit the form (i.e. trying to change days data of form) that edited data does not get stored in database, instead it comes to an error as
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
What can i do to save values to database?
here is my code:
Controller:
public function store(EventRequest $request)
{
$checkbox = Input::get('days_of_week');
$input = Request::all();
$input['days_of_week'] = serialize(Input::get('days_of_week'));
Event::create($input);
return redirect('event');
}
public function edit($id)
{
// get the event
$event = Event::findOrFail($id);
$s = Category::all()->where('parent_id','=','0');
$days = array(
'Monday' => 'Monday',
'Tuesday' => 'Tuesday',
'Wednesday' => 'Wednesday',
'Thursday' => 'Thursday',
'Friday' => 'Friday',
'Saturday' => 'Saturday',
'Sunday' => 'Sunday',
);
$daysOfWeek = unserialize(Event::find($id)->days_of_week);
// show the edit form and pass the event
return view('event.edit')->with('event', $event)->with('s',$s)->with('days',$days)->with('daysOfWeek',$daysOfWeek);
}
public function update(EventRequest $request, $id)
{
$event = Event::findOrFail($id);
$input['days_of_week'] = serialize(Input::get('days_of_week'));
$event->update($request->all());
return redirect('event');
}
Edit.blade.php:
<div class="dropdown">
<a href="#">
<input type="text" name="fname" class="hida" placeholder="select number of days"/>
<p class="multiSel"></p>
</a>
<div class="mutliSelect">
#foreach($days as $day)
<ul>
<li>
{!! Form::checkbox("days_of_week[]", $day, null) , $day !!}
</li>
</ul>
#endforeach
</div>
</div>
Try updating your "Update Method" with following one,
public function update(EventRequest $request, $id)
{
$event = Event::findOrFail($id);
$input = Request::all();
$input['days_of_week'] = serialize(Input::get('days_of_week'));
$event->update($input);
return redirect('event');
}