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;
Related
Its possibility of sending a variable to the blade view through the addColumn() function of datatable jquery?
I want to pass another variable to compare values of the result of the query.
this is the code:
if ($request->ajax()) {
$products = Product::with('categories', 'featureds')->orderBy('name', 'ASC');
$selecteds = [];
$products_selecteds = Product::all();
foreach($products_selecteds as $products_selected){
foreach($products_selected->featureds as $featured){
array_push($selecteds, $products_selected->id);
}
}
return Datatables::of($products)->addColumn('format_price', function($product) {
return $product->format_price;
})->addColumn('checkbox', function ($product, $selecteds) {
return view('pages.products.components.table.checkbox', ['product' => $product, 'selecteds' => $selecteds]);
});
}
the array $selecteds its i want to pass to the view checkbox:
<td>
<label>
<input type="checkbox" class="checkbox-item" {{ in_array($product->id, $selecteds) ? 'checked' : "" }} value=""/>
<span></span>
</label>
</td>
Yes, it's possible to render view on additional columns. But to achieve that, you have to make the column as a Raw Column like this:
Datatable::of($products)
->addColumn('selected', function($product) use($selecteds) {
return view('product.selected', compact('product', 'selecteds'));
})
->rawColumns(['selected'])
note:
you also need to declare use keyword since $selecteds is outside the closure
I am now working with my "Update function".
public function edit($id)
{
$product = Inventory::find($id);
return view('/update',compact('product')) ;
}
This is my view button in my blade
<a href="{{ route('edit',$prod->id) }}" class="btn btn-raised btn-primary btn-sm">
view
</a>
It works when I redirect it to my edit blade
but when I changed the $id to $prod_num
public function edit($prod_num)
{
$product = Inventory::find($prod_num);
return view('/update',compact('product')) ;
}
my $id is bigInt and primary key(AU) while $prod_num is a string.
how is gonna work if I want to look for the prod_id
Thank you
The find() method will looks in the primary key of your model. If you want to get a record by another attribute, you can use where() and first():
$product = Inventory::where('prod_num', $prod_num)->first();
I'm looking to fetch unique values from a table in my database and pass the company name into my view as options in a drop down list within a form.
The function in my controller looks like this:
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->select('company')->get()->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
And my view looks like this:
<div class="form-group">
#foreach ($companies as $company)
{{ Form::label('Select Company')}}
{{ Form::select('companies', $company->company, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
#endforeach
</div>
I'm getting the following error:
Invalid argument supplied for foreach()
If I try {{dd($companies}} I can see an array is being passed through to the view OK. This array looks like this:
array:353[
0 => {#274 ▼
+"company": "Example company name"
}......
]
Ditching the loop and reverting back to:
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
causes another error to be thrown which states:
htmlspecialchars() expects parameter 1 to be string, object given
Where am I going wrong here?
In your controller
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
Fllow as per below
your index function must be like below
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
your view must me like below
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
let me know if any
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
I'm building a backend for my website in Laravel. I need only few text boxes to populate a template. So far I've managed to do that, but every time I pass a value to a template I see also all previous values, not only the last one.
So for example if I'm editing slider title, I see also all previous slider titles - I want to see only last one.
My controler:
public function store()
{
$input = Input::all();
homepage::create([
'slider_title' => $input['slider_title'],
]);
return Redirect::to('/');
}
Part of view responsible for displaying slider title:
#foreach ($homepage as $home)
{{$home->slider_title}}
#endforeach
View for user input in admin:
{{ Form::open(['url' => '/admin']) }}
<div>
{{ Form::label('slider_title', 'Slider title:') }}
{{ Form::text('slider_title') }}
</div>
<div>
{{ Form::submit('Change a slider title') }}
</div>
{{ Form::close() }}
Route for admin:
Route::post('/admin', 'AdminController#store');
Route for main page:
Route::get('/', 'PageController#home');
PageController home method:
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
I tried changing PageControl with:
$homepage = Homepage::orderBy('id', 'desc')->first();
but I'm getting this error:
ErrorException: Trying to get property of non-object on my view page
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
You are seeing all slides because you are selecting all of them:
$homepage = Homepage::all();
This should give you the last one:
$homepage = Homepage::orderBy('id', 'desc')->first();
or
$homepage = Homepage::orderBy('created_at', 'desc')->first();
Remember that now you are getting only one result and you cannot use foreach anymore:
foreach ($homepage as $home)
You can refer to it directly:
$homepage->slider_title
If you still want to return an array to use foreach, this is an option:
$homepage = array( Homepage::orderBy('created_at', 'desc')->first() );