Laravel 5 retrieve checkbox array value - laravel

I am creating project with laravel 5, i did store checkbox array value into table using below's code
$permission_id = Input::get('permission_id');
if(is_array($permission_id))
{
$permission_id = implode(',', $permission_id);
}
$userpermissionlist->permission_id = $permission_id ;
it's stored value like 2,3,4 now i need to explode this value and selected checkbox value will be checked.. how can i do that..My view code is
{!! Form::checkbox('permission_id[]', $userpermission->id) !!}

Setting the third parameter to true in checkbox() method will mark the checkbox as checked. Assuming you have $checked_permission_ids which is an array of all the checked ids, you could do this:
#foreach( $user_permissions as $userpermission )
{!! Form::checkbox('permission_id[]', $userpermission->id, in_array( $userpermission->id, $checked_permission_ids ) ) !!}
#endforeach

Related

Laravel select dropdown using ajax

I have data in an array that contains hotels' names with their id. I want to place them in my select where I will display only the city an grab the id to do ajax and display rooms name. The problem is it is displaying both the name of the hotel and the id array in my dropdown.
this is my controller.
foreach ($hotel as $nam ){
$hotel_nam[]=$nam->id;
$hotel_nam[]=$nam->hotel_name;
}
return $hotel_nam;
this is my drop down
{!! Form::select('hotel_name',$hotel_nam,'S', array('style'=>' Border:none; ', 'id' => 'hotel_id', 'onchange'=>"showRooms(this.value)"));!!}
$hotelLists = [''=>'--Select Hotel--'];
foreach ($hotel as $nam) {
$hotelLists[$nam->id] = $nam->hotel_name;
}
{!! Form::select('hotel_name',$hotelLists,'', array('style'=>' Border:none; ', 'id' => 'hotel_id', 'onchange'=>"showRooms(this.value)"));!!}

display fetch data in dropdown menu with "where" value selected in laravel

I am trying to display the currencies in the dropdown menu but I need the data with a value of default=1 to be selected. upon searching, i found a sample and tried to applied it to my controller, here's what I came up,
$currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');
it doesn't work. the error message said
Call to undefined method Illuminate\Database\Query\Builder::lists()
also I read a comment that the list() is already obsolete in laravel.
how can I achieve this?
here's from my create function in controller
public function create()
{
$currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');
return view ('orders.create')->with('currencies', $currencies);
}
here's from create blade
{{ Form::select('currency_id', $currencies, Input::old('currency_id'),null, ['class' => 'form-control input-lg','required']) }}
thank you so much in advance!
Try using ->pluck(),
$currencies = \DB::table('currencies')->pluck('currency_name','id');
// In blade
{{ Form::select('currency_id', $currencies, null, ['class' => 'form-control input-lg','required']) }}
Read more about pluck here.

Laravel - Display values with same value / number on the page

I have a page here which get all the value that has the same employee_no. here is my page.
the page id defines which employee's name , lastname and number to define. so for example I hve 10 users and i need to set those 10 users with their time in and outs so i need to have a dependent data for this page. that is why I need text and its value to get all the data which that user has.
here in my page, see the textbox? that is the data that im fetching and that will be my basis when I fetch all the records that has the same number.
so when i display that employee number, then all the data must be displayed. but on my case, no data is being displayed here take a look.
whereas, the final output should be this.
here is my view
{!! Form::open(['action' => 'Admin\EmployeeFilemController#insertSchedule', 'method' => 'POST']) !!}
{{Form::text('txtEmployeeNo', $employee->employee_no,['class' => 'form-control'])}}
{!! Form::close() !!}
here is my contoller
public function createSchedule(Request $request, $id)
{
$employee = Employeefm::find($id);
$employNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where(['employee_no'=>$employNum])->get();
// dd($request->all);
return view('admin.employeemaintenance.createSchedule',compact('employee','employeeSched'));
}
my route
Route::get('/admin/employeemaintenance/{id}/createSchedule', 'Admin\EmployeeFilemController#createSchedule');
P.S I have displayed the data because i change the code $employNum
$employeeSched = Schedule::where(['employee_no'=>$employNum])->get();
to this a fixed number 54232
$employeeSched = Schedule::where(['employee_no'=>`54232'])->get();
and here is how i fetched it
#foreach ($employeeSched as $setTime)
<tr>
<td> {{Form::label('date_today', $setTime->date_today)}}</td>
<td> {{Form::label('time_in', $setTime->time_in)}}</td>
<td> {{Form::label('time_out', $setTime->time_out)}}</td>
</tr>
#endforeach

Laravel/formbuilder select option read out value with array

I'm writing a few cods around Laravel's form builder.
Here is my Controller:
public function create()
{
$departments = Department::all('name');
return view('door.project.create') ->with('departments',$departments);
}
On the create.blade.php, I get stuck.
The selectbox's option would show out with JSON array,
like:
{"name": "Sale"}
And the select box codes would be down here:
<div class="form-group">
{!! Form::Label('deparment_name', 'Department:') !!}
{!! Form::select('deparment_name', $departments, null, ['mutiple' => 'multiple']) !!}
</div>
May I ask how do I fix it?
Instead of:
$departments = Department::all('name');
you should use:
$departments = Department::pluck('name','id');
to get in select valid list.

Laravel 5 | Pagination Issue on setting ID field

I Use laravel "paginate()" function to create pagination.
I set the Id field of the record table with "$i++".
But each page starts with 1, 2, 3...
How could i set Id as continue of previous page.
Actually my item table id is not properly, thats why i use "$i++" to create Id.
My Controller:
public function show()
{
$items = \Auth::user()->Items()->paginate(5);
return view('cart.admin.show',compact('items'));
}
My View:
<div class="pagination pagination-centered">
{!! $items->render() !!}
</div>
What is the Solution for this issue...?
For this you can create a helper function in App/Http/Helpers/helper.php
function serial_number_pagination($current_page, $per_page, $i){
return (($current_page * $per_page) + $i)-5;
}
And call the helper function in your blade file like (in place of displacing $i value),
<td>{{ serial_number_pagination($items->currentPage(), $items->perPage(), $i) }}</td>
At blade page, use this before foreach loop:
$sn_count = (isset($_GET['page']) && $_GET['page'] > 1) ? (($_GET['page']-1) * 5)+1 : 1;

Resources