Api Pagination Call to undefined method stdClass::links() on laravel 8 - laravel

hello can you help me? so i am paginating on server side, but when i try to display on client side i get error like this...
i can say I can't use firstItem(), Thanks for your response, I hope this gets the answer..
In Controller
$response = Http::withHeaders([
'X-Authorization' => 'NkKeAs7Og8O6WcNste9OsAjkui2pSZnjZr6WOcDf1EjU1UDPuz88CATNqFI2rkSN'
])->get('http://www.Api.net/api/get-produk', [
'type_product' => $filter,
'limit' => $request->length,
'offset' => $request->start,
]);
$data = json_decode($response);
In Rest API
$produk = Daftarproduk::select('cat_number', 'brand', 'nama_produk', 'host', 'reactivity', 'clone_type', 'application', 'pack_size', 'type_product', 'pack_category', 'price')
->where('type_product', $request->get('type_product'))
->offset($request->offset)
->limit($request->limit)
->paginate();
In View
<tbody>
#foreach ($produk->data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->cat_number }}</td>
<td>{{ $item->brand }}</td>
<td>{{ $item->nama_produk }}</td>
<td>{{ $item->host }}</td>
<td>{{ $item->reactivity }}</td>
<td>{{ $item->clone_type }}</td>
<td>{{ $item->application }}</td>
<td>{{ $item->pack_size }}</td>
<td>{{ $item->type_product }}</td>
<td>{{ $item->price }}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="d-flex justify-content-start">
Showing
{{ $produk->firstItem() }}
to
{{ $produk->lastItem() }}
of
{{ $produk->total() }}
Entries
</div>
<div class="d-flex justify-content-end">
{{ $produk->links() }}
</div>

Related

Laravel - syntax error, unexpected ')', expecting '['

I am using Laravel 5.8 to create an App. When I wanted to display the view, it generated an error as shown below:
syntax error, unexpected ')', expecting '['
Controller
public function serviceOverview(Request $request)
{
$data['title'] = 'Subscription Overview';
$serviceoverviews = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
$render=[];
if(isset($request->package_name))
{
$serviceoverviews=$serviceoverviews->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
$render['package_name']=$request->package_name;
}
if(isset($request->service_id))
{
$serviceoverviews=$serviceoverviews->where('service_id',$request->service_id);
$render['service_id']=$request->service_id;
}
$serviceoverviews= $serviceoverviews->orderBy('created_at','DESC');
$serviceoverviews= $serviceoverviews->paginate(15);
$serviceoverviews= $serviceoverviews->appends($render);
$data['serviceoverviews'] = $serviceoverviews;
return view('report.serviceOverview',$data);
}
In the Controller, I tried to do some filtering. Also, I did raw query.
View
<div class="box box-primary">
<div class="box-header with-border">
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
#if(count($detailsubscriptions))
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->links() }}
</td>
</tr>
</tbody>
</table>
#else
<div class="row text-center">
<h2>No Service Overview to show</h2>
</div>
#endif
</div>
</div>
I tried to check the code, but found nothing. How do I resolve this issue
You have missed a $ from the $serviceoverview variable in your blade #foreach. Change:
#foreach($serviceoverviews as $key => serviceoverview)
To
#foreach($serviceoverviews as $key => $serviceoverview)
Here is the full code updated with changes. Please refer to this
Controller.php
public function serviceOverview(Request $request)
{
$builder = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
if($request->filled('package_name'))) {
$builder = $builder->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
}
if(isset($request->service_id))
{
$builder=$builder->where('service_id',$request->service_id);
}
$serviceoverviews = $builder->orderBy('created_at','DESC')->paginate(15);
$data = [
'title' => 'Subscription Overview',
'serviceoverviews' => $serviceoverviews
];
return view('report.serviceOverview', $data);
}
Now change your view code like this.
view.blade.php
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => $serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->appends(\Request::all())->links() }}
</td>
</tr>
</tbody>
</table>
You are using serviceoverview in place of $serviceoverview in view file. That would be an issue.
You have made some typo and one logical error.
Change
#foreach($serviceoverviews as $key => serviceoverview) to #foreach($serviceoverviews as $key => $serviceoverview)
<td>{{ serviceoverview->service_name }}</td> to <td>{{ $serviceoverview->service_name }}</td>
You have put {{ $serviceoverview->links() }} out side of the foreach loop

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?

Laravel 4 pagination - Can't get past page 1

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()

Resources