Pagination for the view page is not working in laravel5 - php

I am trying to use pagination for my view pages but getting error as : Call to a member function links() on array
Limiting to display records using paginate in controller is fine,but issue is when passing to the view. Controller code is:
$referredProcedureBookings = Gateway::procedurebooking()->getReferredProcedureBooking()->paginate(5);
$new_records = [];
foreach ($referredProcedureBookings as $referredProcedureBooking)
{
$records['ProcedureId'] = $referredProcedureBooking->id;
$records['ProcedureName'] = $referredProcedureBooking->procedure_name;
$new_records[] = $records;
}
return view('procedurebookings',compact('new_records');
In my view its like this:
#foreach($new_records as $new_recordss)
<td><div class="col-md-2"><h5 class="panel-body">{{ $new_recordss['Name'] }}</h5></div></td>
<td><div class="col-md-2 col-md-pull-6"><h5 class="panel-body">{{ $new_recordss['Age'] }}</h5></div></td>
#endforeach
{{ $new_records->links() }}
Error i am getting is :Call to a member function links() on array.
Even if i use :{{ $new_records[links()] }}
Then i get :Call to undefined function links()
how should i use the links method?

links method available on $referredProcedureBookings
$referredProcedureBookings->links()
so pass $referredProcedureBookings to view first.

Related

How to use count number of rows and print value in view file laravel 5.4?

app/http/controller/FirstController.php
public function delhi_property()
{
$sql = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
return view('index',['results'=>$sql]);
}
resources/views/index.blade.php
<h3>({{ $results->count() }}) Properties</h3>
routes/web.php
Route::get('/','FirstController#delhi_property');
I am new in laravel 5.4 Here, what am I doing I simply run a query as I mention above in my Controller and want to print numbers of rows in my view file but when I check it shows an error i.e.
Undefined variable: result (View: D:\xampp\htdocs\real_estate\resources\views\index.blade.php)
So how can I solve this problem? Please help me.
You are returning the query result as "results", so in the view you need to access it as "$results".
In the error it says undefined variable result.
There is no "s" in it.
So refer to the code line that error returns and check whether variable naming is correct.
In controller:
public function delhi_property()
{
$data = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
return view('index', compact('data'));
}
In blade file:
<h3>( {{ $data->count() }} ) Properties</h3>
OR
<h3>( {{ count($data) }} ) Properties</h3>
To count all rows in a query is a function for example:
count($results);
I am modifying your query a bit, will achieve the same result efficiently
public function delhi_property()
{
$cityResults = DB::table('property')->whereIn('city', ['Delhi','New Delhi'])->get();
return view('index',compact('cityResults'));
}
In your view you can access the count as following:
<h3>{{ $cityResults->count() }} Properties</h3>
In case you are using this to calculate and display the total count on a Bootstrap badge, try the following directly in the sidebar view:
<span class="badge badge-info right">{{ DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->count() }}</span>
You can use larave's default function to count rows like example given below.
public function delhi_property()
{
$result = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
$total_result = count($result);
return view('index',compact(['result','total_result']));
}
In view you can print number of rows in variable $result.

Paginate Issue Laravel 5.6

Was wondering if someone could check this out and let me know what I'm doing wrong. I'm trying to use paginate on a database query and render it in my view.
Is my DB query incorrect? I was following the docs to create it. Also read that I needed to remove ->get() when using paginate.
This is what is giving the error on my view: {{$item->paginate(4)}}, same happens if I use {{$item->links(4)}}
Everything renders fine if I remove paginate from the query in the controller..?
Here is what I'm working with.
Controller:
public function index()
{
// $news = DB::table('news')->orderBy('id', 'DESC')->paginate(4);
$news = DB::table('news')->select('id','title','description','listing_image','created_at','updated_at')->orderBy('id', 'DESC')->paginate(4);
return view('news.index',compact('news'));
}
View: (Getting error: Call to undefined method stdClass::paginate())
#if ($news->count())
#foreach($news as $item)
...html
#if ($item->listing_image)
...more html
#else
#endif
... more html
#endforeach
{{$item->paginate(4)}}
#endif
change {{$item->paginate(4)}} to {{ $news->links() }}

laravel pagination and sort table

I'm using sort function and pagination but the problem when I use sort for any column works fine but when I click to the next page using pagination the sort change and have to click on the column again for sorting.
so how to keep sorting while using pagination?
<tr>
<th>
<div class="checkbox checkbox-replace">
<input type="checkbox" id="chk-3">
</div>
</th>
<th>#sortablelink('details','Details')</th>
<th>#sortablelink('postingdate','Date')</th>
<th>#sortablelink('description','Description')</th>
<th>#sortablelink('amount','Amount')</th>
<th>#sortablelink('type','Type')</th>
<th>#sortablelink('slip','Slip#')</th>
<th>#sortablelink('vendor_id','Vendor')</th>
<th>#sortablelink('category_id','Category')</th>
</tr>
public function index()
{
$checks = Checks::orderBy('id', 'asc')->get();
$checks= Checks::sortable()->paginate(5);
return view('home',compact('checks'));
}
You can append query string to your link by doing this :
{{ $users->appends(['sort' => 'votes'])->links() }}
replace sort by your sortable query, and votes by something like request()->sort
According to this page, you also can do your pagination like this:
//Controller
public function index()
{
$checks = Checks::orderBy('id')->paginate(5);
$links = $checks ->appends(['sort' => 'id'])->links();
return view('home', compact('checks', 'links'));
}
And in your view, you can just call your pagination links like this
{{ $links }}
Hope it helps. Cheers!
{{links()}} will still give a messed up sort when you paginate. This is the same as {!! link() !!} also and {{ $checks->links()}}
Rather, use the code below. It will correct the error.
//for the Controller
public function index()
{
$checks = Checks::sortable()->paginate(5);
return view('home', compact('checks'));
}
//for the blade template in laravel append this in div after the table
{!! $checks->appends(\Request::except('page'))->render() !!}][1]
you can use this line in your blade file
{!! $product->appends(\Request::except('page'))->render() !!}

Access Array on HTML VIEW Laravel

I've an error like Trying to get property of non-object, I use var_dump and realize that I've array not object but idk how to access
public function ptkp($id){
$halaman="tindaklayanan";
$keluhan = keluhan::findOrFail($id);
$tindak = DB::table('tindakans')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('tindakans.id, id_keluhan, perbaikan_sementara, revisi_dokumen, target_verifikasi, ttd_tanggung1,
ttd_tanggung2'))->get();
$analisa = DB::table('analisas')
->join('tindakans','tindakans.id','=','analisas.id_tindakan')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('id_tindakan, analisa, tindakan, pic, tanggal_pelaksanaan'))->get();
return view('Laporan.ptkp',compact('keluhan','tindak','analisa','halaman'));
//$pdf = \PDF::loadView('laporan.ptkp', compact('keluhan','tindak','analisa','halaman'));
//return $pdf->stream();
}
Look at $tindak when I use var_dump the result is array, in View I try to access using <?php echo $tindak->perbaikan_sementara ?> but error.
Since you are in Laravel, I assume you use a blade templating engine, so you can try this in the view to access property of object :
{{ $tindak->perbaikan_sementara }}
Or if it's an array of objects :
#foreach ($tindak as $example)
{{ $example->perbaikan_sementara }}
#endforeach
if you really have an array it's in your Laporan.ptkp (better use laporan.blade.php instead)
#foreach ($tindak as $item)
{{ $item['perbaikan_sementara'] }}
#endforeach

Trouble fetching data from 1 to 1 relationsip Laravel

I have set up two model with its row in table. And made a single form to fill both tables and it works perfectly
Tour.php
public function featuredImage()
{
return $this->hasOne('App\FeaturedImage');
}
tours table
id|name|content|featured_status
featuredImage.php
public function tour()
{
return $this->belongsTo('App\Tour');
}
Featured_images table
id|tour_id|path|name
Code in my controller to pass data to view.
$tours = Tour::where('featured', 1)->get();
return view('public.pages.index')
->withTours($tours);
Code in my view
#foreach($tours as $featured)
<div class="thumbnail">
<img src="{{$featured->featuredimage->path}}" alt="{{$featured->featuredImage->name}}">
</div>
<h4>{{$featured-name}}</h4>
#endforeach
The trouble is I'm not able to fetch featured images by writing
{{$featured->featuredimage->path}}
and the error is
Trying to get property of non-object
on the line {{$featured->featuredimage->path}}. I have used this method in my previous project and it had worked perfectly but it isn't going well in this one.
I tried replacing {{$featured->featuredimage->path}} with {{$featured->featuredImage->path}} but didn't worrked out.
Do this:
{{ $featured->featuredImage()->path }}
Also, you're creating a lot of additional queries here. You should use eager loading to solve N + 1 problem:
$tours = Tour::with('featuredImage')->where('featured', 1)->get();
And display data with:
{{ $featured->featuredImage->path }}

Categories