Laravel 4 pagination - Can't get past page 1 - laravel

I have created somewhat of a search feature, where a user can choose a type, pool, date and time. I can't seem to make the paginator to work, as when I click on page 2 the results disappear.
I have tried to append the $query->links() function, but none of my tries have yet succeeded.
The URL looks like this: http://localhost:8080/nih/public/bassengweb/data?time_maling=1&pool_id=1&fraDato=14%2F04%2F2014&tilDato=22%2F04%2F2014&fraTid=00%3A01&tilTid=23%3A59
The code for the view file:
#extends('default')
#section('content')
<h1>ADMIN SØK</h1>
{{ Form::open(array('url' => 'bassengweb/data', 'method' => 'GET')) }}
<table>
<tr>
<td>{{ Form::label('time_maling', 'Timemåling') }}</td>
<td>{{ Form::checkbox('time_maling', 1) }}</td>
<td>{{ Form::label('3_time_maling', '3. Timemåling') }}</td>
<td>{{ Form::checkbox('3_time_maling', 1) }}</td>
<td>{{ Form::label('oppgaver', 'Oppgaver') }}</td>
<td>{{ Form::checkbox('oppgaver', 1) }}</td>
<td>{{ Form::label('pool_id', 'Basseng') }}</td>
<td>{{ Form::select('pool_id', $pools, Input::old('pool_id')) }}</td>
</tr>
<tr>
<td>{{ Form::label('dagsoppg', 'Dagvakt') }}</td>
<td>{{ Form::checkbox('dagsoppg', 1) }}</td>
<td>{{ Form::label('kveldsoppg', 'Kveldsvakt') }}</td>
<td>{{ Form::checkbox('kveldsoppg', 1) }}</td>
<td>{{ Form::label('helgoppg', 'Helgevakt') }}</td>
<td>{{ Form::checkbox('helgsoppg', 1) }}</td>
</tr>
<tr>
<td>{{ Form::label('kontcm', 'Kontroll CM') }}</td>
<td>{{ Form::checkbox('kontcm', 1) }}</td>
<td>{{ Form::label('arduino', 'Arduino') }}</td>
<td>{{ Form::checkbox('arduino', 1) }}</td>
<td><hr/></td>
</tr>
<tr>
<td>{{ Form::label('fraDato', 'Fra dato: ') }}</td>
<td>{{ Form::text('fraDato', Input::old('fraDato'), array('placeholder' => $date)) }}</td>
<td>{{ Form::label('tilDato', 'Til dato: ') }}</td>
<td>{{ Form::text('tilDato', Input::old('tilDato'), array('placeholder' => $date)) }}</td>
</tr>
<tr>
<td>{{ Form::label('fraTid', 'Fra klokka: ') }}</td>
<td>{{ Form::text('fraTid', Input::old('fraTid'), array('placeholder' => '10:55:31')) }}</td>
<td>{{ Form::label('tilTid', 'Til klokka: ') }}</td>
<td>{{ Form::text('tilTid', Input::old('tilTid'), array('placeholder' => '10:55:31')) }}</td>
<td>{{ Form::submit('Søk') }}<td>
</tr>
</table>
{{ Form::close() }}
#if(isset($data))
<table class='table table-hover'>
<tr>
<td>ID</td>
<td>Tittel</td>
<td>Verdi</td>
<td>Ansatt ID</td>
<td>Dato</td>
<td>Tid</td>
#if($type === 'measurement')
<td>Basseng</td>
</tr>
#foreach($data as $dataItem)
<tr>
<td>{{ $dataItem->id }}</td>
<td>{{ $dataItem->measurements[0]->title }}</td>
<td>{{ $dataItem->value }}</td>
<td>{{ $dataItem->emps->user_name }}</td>
<td>{{ date('d/m/Y', strtotime($dataItem->date)) }}</td>
<td>{{ $dataItem->time }}</td>
#foreach($dataItem->measurements as $measurement)
<td>{{ $pools[$measurement->pivot->pool_id] }}</td>
#endforeach
<td>{{ HTML::linkRoute('edit_data', 'Rediger', $dataItem->id) }}</td>
<td>{{ HTML::linkRoute('delete_confirm', 'Slett', $dataItem->id) }}
</td>
</tr>
#endforeach
</table>
{{ $data->appends(array($type => 1, Input::except('page')))->links() }}
#elseif($type === 'task')
</tr>
#foreach($data as $dataItem)
<tr>
<td>{{ $dataItem->id }}</td>
<td>{{ $dataItem->tasks[0]->title }}</td>
<td>{{ $dataItem->value }}</td>
<td>{{ $dataItem->emps->user_name }}</td>
<td>{{ date('d/m/Y', strtotime($dataItem->date)) }}</td>
<td>{{ $dataItem->time }}</td>
<td>{{ HTML::linkRoute('edit_data', 'Rediger', $dataItem->id) }}</td>
<td>{{ HTML::linkRoute('delete_confirm', 'Slett', $dataItem->id) }}</td>
</tr>
#endforeach
#endif
#endif
#stop
Thanks.

You nested array withing appends method:
$data->appends(array($type => 1, Input::except('page')))->links()
// returns
array(
$type => 1,
array(
... // Input/Request params are ignored as a nested array
)
)
Change it to:
$data->appends(array_merge(array($type => 1), Input::except('page')))->links()

Related

Laravel 5.8 - Invalid argument supplied for foreach()?

I tried to display data inside a table for admin.
Here is my admin.blade.php:
<form method="post" action="post_namaadmin">
<input type="text" name="txtnamaadmin" id="txtnamadmin" value="1">
<input type="submit" name="btnnamadmin" id="btnamaadmin">
</form>
<div class="card-body">
<table border="1">
#foreach($datalama as $rows)
<tr>
<td>ID : </td>
<td>{{ $rows->userid }}</td>
<td>Nama : </td>
<td>{{ $rows->nama }}</td>
</tr>
<tr>
<td>Section</td>
<td>IM</td>
<td>CO</td>
<td>SH</td>
<td>PL</td>
<td>RI</td>
<td>ME</td>
<td>TB</td>
<td>CF</td>
</tr>
<tr>
<!-- //1 -->
<td>{{ $rows->a1 }}</td>
<td>{{ $rows->b1 }}</td>
<td>{{ $rows->c1 }}</td>
<td>{{ $rows->d1 }}</td>
<td>{{ $rows->f1 }}</td>
<td>{{ $rows->g1 }}</td>
<td>{{ $rows->h1 }}</td>
<!-- //1 -->
<td>{{ $rows->a2 }}</td>
<td>{{ $rows->b2 }}</td>
<td>{{ $rows->c2 }}</td>
<td>{{ $rows->d2 }}</td>
<td>{{ $rows->f2 }}</td>
<td>{{ $rows->g2 }}</td>
<td>{{ $rows->h2 }}</td>
<!-- //3 -->
<td>{{ $rows->a3 }}</td>
<td>{{ $rows->b3 }}</td>
<td>{{ $rows->c3 }}</td>
<td>{{ $rows->d3 }}</td>
<td>{{ $rows->f3 }}</td>
<td>{{ $rows->g3 }}</td>
<td>{{ $rows->h3 }}</td>
<!-- //4 -->
<td>{{ $rows->a4 }}</td>
<td>{{ $rows->b4 }}</td>
<td>{{ $rows->c4 }}</td>
<td>{{ $rows->d4 }}</td>
<td>{{ $rows->f4 }}</td>
<td>{{ $rows->g4 }}</td>
<td>{{ $rows->h4 }}</td>
<!-- //5 -->
<td>{{ $rows->a5 }}</td>
<td>{{ $rows->b5 }}</td>
<td>{{ $rows->c5 }}</td>
<td>{{ $rows->d5 }}</td>
<td>{{ $rows->f5 }}</td>
<td>{{ $rows->g5 }}</td>
<td>{{ $rows->h5 }}</td>
<!-- //6 -->
<td>{{ $rows->a6 }}</td>
<td>{{ $rows->b6 }}</td>
<td>{{ $rows->c6 }}</td>
<td>{{ $rows->d6 }}</td>
<td>{{ $rows->f6 }}</td>
<td>{{ $rows->g6 }}</td>
<td>{{ $rows->h6 }}</td>
<!-- //7 -->
<td>{{ $rows->a7 }}</td>
<td>{{ $rows->b7 }}</td>
<td>{{ $rows->c7 }}</td>
<td>{{ $rows->d7 }}</td>
<td>{{ $rows->f7 }}</td>
<td>{{ $rows->g7 }}</td>
<td>{{ $rows->h7 }}</td>
</tr>
#endforeach
</table>
</div>
The code is not yet completed, but it should give a rough picture of the table.
Here is my route in web.php:
Route::post('/post_namaadmin', 'DataController#admin' ) ->name ('postnamaadmin');
Function in DataController.php :
public function admin(Request $request){
$mk = new data();
$param['datalama'] = $mk->ambilData($request->txtnamaadmin);
return view('admin',$param);
}
Function in data class :
public function ambilData($nama){
$query = data::orderby('userid')->where('nama','=' ,'$nama')->first();
return $query;
}
Here is the error:
> ErrorException {#230 ▼ #message: "Invalid argument supplied for
> foreach()" #code: 0 #file:
> "C:\xampp\htdocs\kuesioner1\storage\framework\views\5c1b0932d9bc5a402e110419f0ce7e166c7a2109.php"
> #line: 22 #severity: E_WARNING } 1
I'm new in Laravel. For why I use 5.8, my superior demand me so. All help and suggestion is highly appreciated.
When you try to dd($param['datalama']), you will get null, because your query found nothing, using ->where('nama','=' ,'$nama').
You need to change data class ambilData method
public function ambilData($nama){
return data::orderby('userid')
->where('nama', $nama)
->get();
}
I think you should put $param['datalama'] in the foreach instead
and change the view function to view('admin',compact('param'))
and in the form tag you need to change the action to
<form action="{{route('postnamaadmin')}}">

How to calculate total column value without query by Laravel or php?

Here above I want to Calculate Buying Total and selling Total in the current month . Suppose in November I want to calculate sell row total , buying row total and buying-selling total count. How Can I do that . Please help...
**<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Product Name</th>
<th>Quantity (Stock IN)</th>
<th>Buying Total</th>
<th>Stock</th>
<th>Selling Total</th>
<th>Profit (%)</th>
</tr>
</thead>
<tbody>
#foreach($product_all as $row)
<tr>
<td>{{ $row->buying_date}}</td>
<td>{{ $row->product_name}}</td>
<td>{{ $row->stock_in }}</td>
<td>{{ $row->buying_price*$row->stock_in }}</td> *********
<td>{{ $row->total_stock }}</td>
<td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
<td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>
</tr>
#endforeach
</tbody>
</table>**
<h4 style="color: #ff3939; text-align: center;">November Total Invest : {{ #### }} <span> Sell : {{ #### }}</span></h4>
There are 2 ways to do:
First: setting parameters
**<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
<?php
$yoursum1 = 0;
$yoursum2 = 0;
foreach($product_all as $row)
echo "<tr>
<td>{{ $row->buying_date}}</td>
<td>{{ $row->product_name}}</td>
<td>{{ $row->stock_in }}</td>
<td>{{ $row->buying_price*$row->stock_in }}</td> *********
<td>{{ $row->total_stock }}</td>
<td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
<td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>
</tr>"
$yoursum1 += $yourvalue1;
$yoursum2 += $yourvalue2;
endforeach
echo "<tr>
<td></td>
<td>Summary</td>
<td>{{ $yoursum1}}</td>
<td>{{ $yoursum2}}</td> *********
<td>{{ $yoursum3}}</td>
<td>{{ $yoursum4}}</td> ********
<td>{{ $yoursum5 }}</td>
</tr>"
?>
</tbody>
</table>**
<h4 style="color: #ff3939; text-align: center;">November Total Invest : {{ #### }} <span> Sell : {{ #### }}</span></h4>
Second: using query to calculate and insert one row below
#foreach($product_all as $row)
<tr>
<td>{{ $row->buying_date}}</td>
<td>{{ $row->product_name}}</td>
<td>{{ $row->stock_in }}</td>
<td>{{ $row->buying_price*$row->stock_in }}</td> *********
<td>{{ $row->total_stock }}</td>
<td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
<td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>
</tr>
#endforeach
<tr>
<td></td>
<td>Summary</td>
<td>{{ $value1 }}</td>
<td>{{ $value2 }}</td> *********
<td>{{ $value3 }}</td>
<td>{{ $value4 }}</td> ********
<td>{{ ($value5 }}</td>
</tr>
IT is advisable to keep calculations and processing out of the views. Controllers are meant to be the files where all calculations and data manipulations should be done.
//Say this is inside the controller method from which the view with data is returned as response
public function index(Request $request)
{
$product_all = Product::all();
$groupedByMonth = $product_all->groupBy(function($product) {
return \Illuminate\Support\Carbon::parse($product->date)->format('m');
});
$totals = (object) [
'buying' => $groupedByMonth->map(function($month){
return $month->sum('buying_total');
}),
'selling' => $groupedByMonth->map(function($month){
return $month->sum('selling_total');
}),
];
//Pass $totals to the view where it can be accessed as $totals->buying
}
As such the calculation/manipulation can be done in blade view between #php #endphp

Laravel Foreach Looping

I need to display the previous value of the iterated array, here is code
<tr>
<th></th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Delivery Address</th>
<th>Contact No.</th>
<th>Zip Code</th>
<th>Payment Terms</th>
<th>Credit Limit</th>
</tr>
<?php $previousValue = null; ?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue=$value->customer_name }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
#endforeach
I need to display the previous name of Customer in the iteration?
Any idea on how to do this?
Set value at the end , I set blank for first
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue or ""}}</td>
.....
.....
</tr>
<?php $previousValue = $value->customer_name ?>
#endforeach
$customer_count[$key - 1]
Of course you'll need to check if that exists or use the null-coalesce operator or something, so:
#php
$previous = $customer_count[$key - 1] ?? false;
#endphp
Then use:
#if( $previous )
... some stuff ...
#endif
wherever you need to inject.
please try this
<td>{{ (!$loop->first) ? $customer_count[$key - 1]->customer_name : '' }}</td>
You can read more about foreach loop here.
At very first set the previous value as blank and then at the bottom set value in $previousValue
<?php $previousValue = '';?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{ $previousValue }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
<?php $previousValue = $value->customer_name; ?>
#endforeach

Laravel Resource controller not getting delete route on amchor click

In laravel, I have resource controller :
Route::resource('/student','StudentController');
here is my view listing code :
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
Link
</td>
</tr>
conytroller function i have :
public function destroy($id)
{
echo "ok";
}
My problem is that when I click on anchor button(delete ) it not
found the resource route for the delete what I am doing wrong here can
anyone please help me elated this.
when I click on the delete anchor it calls the show function of resource but I want the destroy function to call on click.
destroy function use DELETE method
in your blade view
#foreach($students as $model)
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
DELETE
<form id="delete_form" style="display:none" action="{{ route('student.destroy',$model->id)}}" method="POST" >
{{ csrf_field() }}
<input type="hidden" name="_method" value="delete" />
</form>
</td>
</tr>
#endforeach

Laravel Blade echo and variables

I'm trying to understand Laravel, but i'm stuck
When I try this code:
#foreach($nerds as $key =>$value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
<td>{{ $value->nerd_level }}</td>
</tr>
#endforeach
it displays litterally "{{ $value->id }}" etc in a list on the site.
Anyone?

Resources