Laravel is great that it auto populates the fields for you on an edit form. However i seem to be having bother getting this to work for checkboxes.
I have a list of disciplines that get stored as a json array in the database like so ["FULL_CONTACT","K1"]
How do I get these to display as checked in the form?
{!! Form::model($official, array('method' => 'put', 'route' => ['officials.update', $official->id], 'class' => 'form-horizontal')) !!}
<div class="form-group">
<label for="" class="col-lg-3 control-label">First Name:</label>
<div class="col-lg-9 controls">
{!! Form::text('first_name', null, array('class' => 'form-control', 'max-length' => '50', 'required')) !!}
</div>
</div>
<label class="checkbox">
{!! Form::checkbox('disciplines[]', null) !!} Full Contact
</label>
<label class="checkbox">
{!! Form::checkbox('disciplines[]', null) !!} Low Kick
</label>
<label class="checkbox">
{!! Form::checkbox('disciplines[]', null) !!} K1
</label>
{!! Form::close() !!}
{{ Form::checkbox('disciplines[]', 'Low Kick', true) }}
this will generate following html
<input checked="checked" name="disciplines[]" type="checkbox" value="Low Kick">
Form::checkbox's arguments are
Form::checkbox($name, $value, $checked, $options)
You can use #foreach loop to display all the checkboxs, for example:
#foreach($disciplines as $discipline)
{!! Form::checkbox('disciplines[]', $discipline, null) !!} label
#endforeach`
Related
I have a filtration page to get the posts according to user choices, I want to give the user the ability to choose multiple ways to filter, such as the price, post date, category, ...etc.
therefore I should append the user's previous choices.
for example ex: example.com?category=1 and now the user wants to filter with price, it should be. ex: example.com?category=1&price=200
it works for me while trying <a></a> tag,
ex price 200.
so I need to do the same with form GET, here's how I tries to solve it:
{!! Form::open(
[
'route' => [Request::route()->getName(), request()->input()],
'method' => 'GET'
]) !!}
#foreach ($specialisms as $specialism)
<div class="custom-control custom-checkbox">
{!! Form::checkbox('specialism', $specialism->id, request('specialism') == $specialism->id, ['class' => 'custom-control-input', 'id' => Str::snake($specialism->title['en']), 'onchange' => '$(this).parent().parent().submit()']) !!}
<label class="custom-control-label" for="{{ Str::snake($specialism->title['en']) }}">{{ $specialism->lang_title }}</label>
</div>
#endforeach
{!! Form::close() !!}
but it doesn't work, it always removes all parameters and append-only the parameter for the form input
you should modify:
checkbox name :specialism[] and condition: in_array($specialism->id, request('specialism'))
to be :
{!! Form::open([
'route' => [Request::route()->getName(), request()->input()],
'method' => 'GET',
]) !!}
#foreach ($specialisms as $specialism)
<div class="custom-control custom-checkbox">
{!! Form::checkbox('specialism[]', $specialism->id, in_array($specialism->id, request('specialism')), ['class'
=> 'custom-control-input', 'id' => Str::snake($specialism->title['en']), 'onchange' =>
'$(this).parent().parent().submit()']) !!}
<label class="custom-control-label"
for="{{ Str::snake($specialism->title['en']) }}">{{ $specialism->lang_title }}</label>
</div>
#endforeach
{!! Form::close() !!}
inside EmployeeController in the edit function, i have this code
public function edit($id)
{
$employees = Employee::find($id);
$departmentlists = Department::pluck('id', 'name');
return view('employees.edit', compact('employees', 'departmentlists'));
}
and inside edit.blade.php to display the dropdown i have this code
{!! Form::open(['action' => ['EmployeeController#update', $employees->id], 'method' => 'POST', 'autocomplete' => 'off', 'class' => 'form-horizontal', 'enctype' => 'application/x-www-form-urlencoded']) !!}
<div class="card">
<div class="card-header card-header-primary">
<h4 {{ Form::label('', 'Change Employee Data', ['class' => 'card-title']) }}
</h4>
<p class="card-category"></p>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12 text-right">
<a href="{{ route('employees.index') }}" class="btn btn-sm btn-primary" style="font-size:12px">
<i class="material-icons">keyboard_backspace</i>
{{ __('kembali') }}
</a>
</div>
</div>
<div class="row">
{{ Form::label('Name', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{Form::text('name', $employees->name, ['class' => 'form-control', 'placeholder' => 'Name', 'required'])}}
<p style="color: red;">#error('name') {{ $message }} #enderror</p>
</div>
</div>
<div class="row">
{{ Form::label('Department', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="col-md-7">
<div class="form=group">
#foreach($departmentlists as $dl)
<option value="{{ $dl->id }}" #if($dl->id==$employees->department_id) selected='selected' #endif >{{ $employees->name }}</option>
#endforeach
<p style="color: red;">#error('id') {{ $message }} #enderror</p>
</div>
</div>
</div>
<div class="row">
{{ Form::label('Keterangan', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{ Form::textarea('information', $employees->information, ['class' => 'form-control', 'placeholder' => 'Keterangan', 'rows' => '6', 'cols' => '50']) }}
</div>
</div>
</div>
<div class="card-footer ml-auto mr-auto">
{{ Form::hidden('_method','PUT') }}
{{ Form::submit('Ubah', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
This is the employees table
and this is departments table
Now, the goal is i want the dropdown on edit page to display department name of the employee belongs to, while the dropdown still have all of the department name.
so i change can it, but when i run this code it gives me this error.
Trying to get property 'id' of non-object (View:
C:\xampp\htdocs\ims-it-laravel7\resources\views\employees\edit.blade.php)
i have read other threads here but those code still doesn't solve the problem
$departmentlists = Department::pluck('id', 'name');
Later You use
#foreach($departmentlists as $dl) and $dl->id
$dl is NOT an object, it is an array because of the pluck() function.
More precisely it looks like this
[
"abc" => 1
"xyz" => 2
"foo" => 3
]
Note: To see it Yourself try using dd($departmentlists) or dump($departmentlists)
Please see Laravel Eloquent Pluck
In this case You might want to use Department::select(['id', 'name'])->get() as it will return collection of objects with specified properties.
How can I insert Multiple Row of Cart:: content() in Laravel 5.
I'm using Crinsane/LaravelShoppingcart for my cart, however, when you click on proceed to payment, I want the user to fill Customer details like (Terminal ID, sale date, and customer name. But I'm having issue with inserting Cart::(Content) into my database (I dont want to insert instance. I like to break the cart down to my database column. example, if someone pick like 2-5 items, I want it to be inserted to my db, with each item in each row. I've break the Cart::content down in my view, but I need to be able to use Laravel to insert multiple row based on the number of Items selected.
PLEASE NOTE: IF I ONLY ADD ONLY ONE ITEM TO CART, I'M ABLE TO INSERT TO DATABASE, IT'S ONLY WHEN THE ITEM IN CART IS MORE THAN ONE THAT I HAVE ISSUE WITH.
Below is my proceed.blade.php
<div class="panel-heading">Customer Detail</div>
<div class="panel-body">
#if (sizeof(Cart::content()) > 0)
{!! Form::open(['method'=>'POST', 'action'=> 'SalesController#store','files'=>true]) !!}
{!! Form::text('counter', $counter=Cart::content()->groupBy('id')->count(), ['class'=>'form-control'])!!}
#foreach (Cart::content() as $item)
<div class="form-group">
{!! Form::label('product_name', 'Product Name:') !!}
{!! Form::text('product_name', $item->name, ['class'=>'form-control'])!!} </div>
<div class="form-group">
{!! Form::label('quantity', 'Quantity:') !!}
{!! Form::text('quantity', $item->qty, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('unit_price', 'Unit Price:') !!}
{!! Form::text('unit_price', $item->price, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('rowId', 'Row ID:') !!}
{!! Form::text('rowId', $item->rowId, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('tax', 'Tax:') !!}
{!! Form::text('tax', $item->tax, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('total_price', 'Sub Total:') !!}
{!! Form::text('total_price', $item->subtotal, ['class'=>'form-control'])!!}
</div>
#endforeach
#endif
<div class="form-group">
{!! Form::label('terminal_id', 'Terminal ID:') !!}
{!! Form::text('terminal_id', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('customer_name', 'Customer Name:') !!}
{!! Form::text('customer_name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('sale_date', 'Sale Date:') !!}
{!! Form::text('sale_date', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Check Out', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
`
MY CONTROLLER
$counter= $request['counter'];
for ($i = 0; $i < count($counter); $i++) {
Sale::create([
'terminal_id' => $request['terminal_id'],
'customer_name' => $request['customer_name'],
'unit_price' => $request['unit_price'],
'total_price' => $request['total_price'],
'tax' => $request['tax'],
'quantity' => $request['quantity'],
'product_name' => $request['product_name'],
'sale_date' => $request['sale_date'],
'rowId' => $request['rowId']
]);
}
return redirect('shop');
}
First your fields are not arrays so with each #foreach in your view, the old value overrides.
Make your inputs to Array inputs by changing your view this way:
<div class="panel-heading">Customer Detail</div>
<div class="panel-body">
#if (sizeof(Cart::content()) > 0)
{!! Form::open(['method'=>'POST', 'action'=> 'SalesController#store','files'=>true]) !!}
{!! Form::text('counter', $counter=Cart::content()->groupBy('id')->count(), ['class'=>'form-control'])!!}
<?php $idx = 0; ?>
#foreach (Cart::content() as $item)
<div class="form-group">
{!! Form::label("product_name[$idx]", 'Product Name:') !!}
{!! Form::text("product_name[$idx]", $item->name, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("quantity[$idx]", 'Quantity:') !!}
{!! Form::text("quantity[$idx]", $item->qty, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("unit_price[idx]", 'Unit Price:') !!}
{!! Form::text("unit_price[idx]", $item->price, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("rowId[$idx]", 'Row ID:') !!}
{!! Form::text("rowId[$idx]", $item->rowId, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("tax[$idx]", 'Tax:') !!}
{!! Form::text("tax[$idx]", $item->tax, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("total_price[$idx]", 'Sub Total:') !!}
{!! Form::text("total_price[$idx]", $item->subtotal, ['class'=>'form-control'])!!}
</div>
<?php $idx++; ?>
#endforeach
#endif
<div class="form-group">
{!! Form::label('terminal_id', 'Terminal ID:') !!}
{!! Form::text('terminal_id', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('customer_name', 'Customer Name:') !!}
{!! Form::text('customer_name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('sale_date', 'Sale Date:') !!}
{!! Form::text('sale_date', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Check Out', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
I suggest you also putting your for loop in a transaction in order to improve it's safety.
Like this:
public function store(SalesCreateRequest $request) {
$counter = Input::get('counter');
\DB::transaction(function() use ($counter){
for ($i=0; $i < $counter; $i++){
Sale::create([
'terminal_id' => Input::get("terminal_id")[$i],
'customer_name'=> Input::get("customer_name")[$i],
'unit_price'=> Input::get("unit_price")[$i],
'total_price'=> Input::get("total_price")[$i],
'tax'=> Input::get("tax")[$i],
'quantity'=> Input::get("quantity")[$i],
'product_name'=> Input::get("product_name")[$i],
'sale_date'=> Input::get("sale_date")[$i],
'rowId'=> Input::get("rowId")[$i]
]);
}
});
return "working";
}
Note that you could simply add a [] suffix instead of [$idx] in your input names to make your inputs arrays, but usually indexing your inputs explicitly is safer.
Edit the $idx key from Cart::content() was returning a weird number, that was why simple numerical numbers threw Out of range exception.
Change your view code as I did above, I hope fixing it this way.
Hope it helps
Just modify your controller
$counter = $request['counter'];
\DB::transaction(function() use ($counter, $request) {
for ($i = 0; $i < count($counter); $i++) {
Sale::create([
'terminal_id' => $request["terminal_id"],
'customer_name' => $request["customer_name"],
'unit_price' => $request["unit_price"][$i],
'total_price' => $request["total_price"][$i],
'tax' => $request["tax"][$i],
'quantity' => $request["quantity"][$i],
'product_name' => $request["product_name"][$i],
'sale_date' => $request["sale_date"],
'rowId' => $request["rowId"][$i]
]);
}
});
NOTE
Not all input fields are array (terminal_id, sale_date...)
I am trying to validate a form in Laravel 5.3. The form has checkboxes. I need at least one checkbox to be selected for the form to be vald. This is my form
Here is my form
<div class="form-group {{ $errors->has('gender') ? 'has-error' : ''}}">
<div class="col-md-2"></div>
<div class="col-md-10">
<label for="gender_" class="checkbox-inline">
{!! Form::checkbox('gender', '', null, ['id' => 'gender_']) !!}
{{ trans('blogs.gender_') }}
</label>
<label for="gender_1" class="checkbox-inline">
{!! Form::checkbox('gender', '1', null, ['id' => 'gender_1']) !!}
{{ trans('blogs.gender_1') }}
</label>
<label for="gender_2" class="checkbox-inline">
{!! Form::checkbox('gender', '2', null, ['id' => 'gender_2']) !!}
Female
</label>
{!! $errors->first('gender', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
{!! Form::label('name',trans('blogs.name'),['class' => 'col-md-2 control-label']) !!}
<div class="col-md-10">
{!! Form::text('name',null, ['class' => 'form-control']) !!}
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
I am using FormRequest object. Here is my rules() method
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'gender' => 'required'
];
}
However, the validation fails unless I select every checkbox!
How can I correctly ensure that the validation only fails if no checkboxes is selected?
Your rules() looks correct so no require change for it. Try below:
<div class="form-group {{ $errors->has('gender') ? 'has-error' : ''}}">
<div class="col-md-2"></div>
<div class="col-md-10">
<label for="gender_0" class="checkbox-inline">
{!! Form::checkbox('gender[]', 0, null, ['id' => 'gender_0']) !!}
{{ trans('blogs.gender_') }}
</label>
<label for="gender_1" class="checkbox-inline">
{!! Form::checkbox('gender[]', 1, null, ['id' => 'gender_1']) !!}
{{ trans('blogs.gender_1') }}
</label>
<label for="gender_2" class="checkbox-inline">
{!! Form::checkbox('gender[]', 2, null, ['id' => 'gender_2']) !!}
Female
</label>
{!! $errors->first('gender', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
{!! Form::label('name',trans('blogs.name'),['class' => 'col-md-2 control-label']) !!}
<div class="col-md-10">
{!! Form::text('name',null, ['class' => 'form-control']) !!}
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
Hope this could help you!
I have a form in which user should at least select one file to be uploaded. I have three file input fields like this:
<div class="form-group col-lg-4">
{!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
{!! Form::file('files[]', ['id'=>'file1']) !!}
</div>
<div class="form-group col-lg-4">
{!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
{!! Form::file('files[]', ['id'=>'file2']) !!}
</div>
<div class="form-group col-lg-4">
{!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
{!! Form::file('files[]', ['id'=>'file3']) !!}
</div>
I should validate the presence of at least one file and the mime types in a form request. Then in the store method of the related form controller, the original file names should be stored in the three corresponding database fields(namely file1, file2, file3).
How can I implement this?
After some searching around I finally came up with a solution. First of all I modified the view to look like this:
<div class="form-group col-lg-4">
{!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
{!! Form::file('file1', ['id'=>'file1']) !!}
</div>
<div class="form-group col-lg-4">
{!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
{!! Form::file('file2', ['id'=>'file2']) !!}
</div>
<div class="form-group col-lg-4">
{!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
{!! Form::file('file3', ['id'=>'file3']) !!}
</div>
Then in the controller I used your suggested code:
$files =[];
if ($request->file('file1')) $files[] = $request->file('file1');
if ($request->file('file2')) $files[] = $request->file('file2');
if ($request->file('file3')) $files[] = $request->file('file3');
foreach ($files as $file)
{
if(!empty($file)){
$filename=$file->getClientOriginalName();
$file->move(
base_path().'/public/uploads/', $filename
);
}
}