Laravel Pgination not working when putting link after foreach - laravel

I am using laravel 5.2 and I am facing some issue with pagination link. When I am adding this code:
{{ $wachat->links() }}
After foreach I am getting this error:
Call to undefined method Illuminate\Database\Query\Builder::links() (View: /Applications/MAMP/htdocs/kc/kyo-webservice/resources/views/wechat/show.blade.php)
In my controller I am using this code:
public function wechatshows($d1, $id, $name, Request $request)
{
$users = Keyuser::where('imception_id', '=', $d1)->first();
$user = UserKey::where('id', '=', $id)->first();
if ($name == "all") {
$wachat = Wechat::where('key', '=', $d1)->orderBy('id', 'DESC')->paginate(5);
} else {
$wachat = Wechat::where('key', '=', $d1)->where('groupName', '=', $name)->orderBy('id', 'DESC')->paginate(5);
}
$wechatcontact = Wechat::select('groupName')->groupBy('groupName')->get();
return view('wechat.show', ['wachat' => $wachat, 'user' => $user, 'product' => $d1, 'users' => $users, 'keyUser' => $d1, 'wechatcontact' => $wechatcontact]);
}
I am using this code in my views:
<tbody id="chatListBody" style="overflow-y: scroll;">
#foreach ($wachat as $wachat)
<tr>
<input type="hidden" class="wechatname" data-id="{{ $wachat->id }}">
<td>{{ $wachat->id }}</td>
<td>{{ $wachat->wxid }}</td>
<td>{{ $wachat->username }}</td>
#if ($wachat->type === 'image')
<td><a class="example-image-link" href="{{ url('/images/'.$wachat->imgPath.'') }}" data-lightbox="{{$wachat->imgPath}}"
data-title="{{$wachat->imgPath}}"><img class="imgwhatsapp" data-lightbox="roadtrip" src="{{ url('/images/'.$wachat->imgPath.'') }}"></a></td>
#elseif ($wachat->type === 'video')
<td>{{$wachat->imgPath}}</td>
#elseif ($wachat->type === 'audio')
<td> {{$wachat->imgPath}} </td>
#elseif ($wachat->type === 'text')
<td>{{$wachat->message}}</td>
#else
<td>no data</td>
#endif
<td>{{ $wachat->created_at }}</td>
</tr>
#endforeach
</tbody>
{{ $wachat->links() }}
</div>
</table>
When I am putting {{ $wachat->links() }} above foreach it is working I don't know why it is happening.

Perhaps, $wachat is getting override inside #foreach($wachat as $wachat). so, it can not find links() method.
you should use #foreach($wachat as $singleWachat) or as per your convenience.

When you loop through your collection, Your collection become object. After the loop when you do {{ $wachat->links() }}. Your Code try to paginate a object and error shown.
Avoid this kind of problem try to use your collection variable name plural and your object name singular.
public function wechatshows($d1, $id, $name, Request $request)
{
$users = Keyuser::where('imception_id', '=', $d1)->first();
$user = UserKey::where('id', '=', $id)->first();
if ($name == "all") {
$wachats = Wechat::where('key', '=', $d1)->orderBy('id', 'DESC')->paginate(5);
} else {
$wachats = Wechat::where('key', '=', $d1)->where('groupName', '=', $name)->orderBy('id', 'DESC')->paginate(5);
}
$wechatcontact = Wechat::select('groupName')->groupBy('groupName')->get();
return view('wechat.show', ['wachats' => $wachats, 'user' => $user, 'product' => $d1, 'users' => $users, 'keyUser' => $d1, 'wechatcontact' => $wechatcontact]);
}
and
<tbody id="chatListBody" style="overflow-y: scroll;">
#foreach ($wachats as $wachat)
<tr>
<input type="hidden" class="wechatname" data-id="{{ $wachat->id }}">
<td>{{ $wachat->id }}</td>
<td>{{ $wachat->wxid }}</td>
<td>{{ $wachat->username }}</td>
#if ($wachat->type === 'image')
<td><a class="example-image-link" href="{{ url('/images/'.$wachat->imgPath.'') }}" data-lightbox="{{$wachat->imgPath}}"
data-title="{{$wachat->imgPath}}"><img class="imgwhatsapp" data-lightbox="roadtrip" src="{{ url('/images/'.$wachat->imgPath.'') }}"></a></td>
#elseif ($wachat->type === 'video')
<td>{{$wachat->imgPath}}</td>
#elseif ($wachat->type === 'audio')
<td> {{$wachat->imgPath}} </td>
#elseif ($wachat->type === 'text')
<td>{{$wachat->message}}</td>
#else
<td>no data</td>
#endif
<td>{{ $wachat->created_at }}</td>
</tr>
#endforeach
</tbody>
{{ $wachats->links() }}
</div>
</table>

Related

How to access map function in Laravel

This is my controller code:
public function RentCertificate()
{
$report = Report::distric()->status(1)->desk(15)->get()
->groupBy(function (Report $item) {
return $item->created_at->format('Y-m');
})
->map(function($rows, $key) {
return [
'column_one' => $rows->column_one,
'rows' => $rows
];
});
return view('adcr.report.rent_certificate', compact('report'));
}
when I try
#foreach($report as $key => $data)
<tr role="row" class="odd">
<td class="input_bangla">{{ $key+1 }}</td>
<td>{{ $data->fiscal_year }}</td>
<td>{{ $data->month }}</td>
</tr>
#endforeach
I got:
Property [fiscal_year] does not exist on this collection instance.
How can I access this data?
Here,
#foreach($report as $key => $data)
$data is the array you've mapped. An array like ['column_one' => ..., 'rows' => ...], it's not an object. You need to access one of its keys (column_one or rows).
Also, you can use a ternary operator with loop iteration to assign the classes even or odd in your <tr> element.
#foreach($report as $key => $data)
<tr role="row" class="{{ ($loop->iteration % 2) ? 'even' : 'odd' }}">
<td class="input_bangla">{{ $key+1 }}</td>
<td>{{ $data['rows']->fiscal_year }}</td>
<td>{{ $data['rows']->month }}</td>
</tr>
#endforeach

how to display nested data in table laravel

How can I get the result as drawn on the right
in Controller
$programs = Program::with(['activities', 'activites.subactivities'])->get();
in Blade View
#foreach ($programs as $programKey => $program)
#foreach ($program->activities as $activity)
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>{{ $programKey + 1 }}</td>
<td>{{ strtoupper($program->name) }}</td>
<td>{{ $activity->name }}</td>
<td>{{ $subactivity['name'] }}</td>
</tr>
#endforeach
#endforeach
#endforeach
I would say to save in a variable and if different the show
Like this:
#foreach ($programs as $programKey => $program)
#php
$newProgram = true;
#endphp
#foreach ($program->activities as $activityKey => $activity)
#php
$newActivity = true;
#endphp
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>
#if($newProgram == true)
{{ $programKey + 1 }}
#endif
</td>
<td>
#if($newProgram == true)
{{ strtoupper($program->name) }}
#php
$newProgram = false;
#endphp
#endif
</td>
<td>
#if($newActivity == true)
{{ $activity->name }}
#php
$newActivity = false;
#endphp
#endif
</td>
<td>
{{ $subactivity['name'] }}
</td>
</tr>
#endforeach
#endforeach
#endforeach

Laravel year, sum, count, group by eloquent query

Can someone show me how I would build this query with eloquent, or is this required to be written with a DB:Raw query, or would it be best to be a raw sql query?
select `pay_week`,
year(`start_timestamp`) as year,
sum(`total_duration`) as 'duration',
count(`pay_week`) as 'entries'
from `time_clock`
group by year(`start_timestamp`),`pay_week`;
Any help to better understand would be greatly appreciated.
Thank you.
$entries = TimeClock::select(
'pay_week',
DB::raw('count(pay_week) as entries'),
DB::raw('YEAR(start_timestamp) as year'),
DB::raw('sum(total_duration) as duration')
)
->where('user_id', $user->record_id)
->sum('total_duration')
->groupBy('pay_week')
->get();
results in a
Call to a member function groupBy() on float
What I have now;
Blade;
<table class="table table-hover dataTable table-striped width-full" data-plugin="dataTable">
<thead>
<th>Pay Week</th>
<th>Date Range</th>
<th>Year</th>
<th>Total Hours</th>
<th>Gross Pay</th>
<th>Entries</th>
<th>Action</th>
</thead>
<tbody>
#foreach($entries as $group)
#foreach($group->entries as $entry)
#php
$week_start = (new DateTime())->setISODate(date("Y"),$entry->pay_week)->format("m/d");
$start = \Carbon\Carbon::createFromFormat("m/d", $week_start);
$end = $start->copy()->endOfWeek()->format('m/d');
$year = \Carbon\Carbon::parse($entry->start_timestamp)->format('Y');
#endphp
<tr>
<td>{{ $entry->pay_week }}</td>
<td>{{ $start->format('m/d') . ' - ' . $end }}</td>
<td>{{ $year }}</td>
<td>0</td>
<td>$ 0.00</td>
<td>0</td>
<td>[ Btn ]</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
This is what I have in the controller method.
$user = Auth::guard('user')->user();
$entries = TimeClock::select(
'pay_week',
'start_timestamp',
'total_duration'
)
->where('user_id', $user->record_id)
->get()
->groupBy('pay_week')
->map(function($entry) {
return (object)[
'count' => $entry->count(),
'duration' => $entry->sum('total_duration'),
//'year' => $entry->sum('start_timestamp')->year,
'entries' => $entry
];
});
return view('user.timeclock.reports.all', [
'entries' => $entries
]);
Given, that you set the cast in your model
class TimeClock extends Model
{
protected $cast = [
'start_timestamp' => 'datetime'
];
}
you can do the following:
$entries = TimeClock::select(
'pay_week',
'start_timestamp',
'total_duration'
)
->where('user_id', $user->record_id)
//->groupBy('pay_week')
->get()
->groupBy([ 'pay_week', function($entry) {
return $entry->start_timestamp->year
})
/*->map(function($entry) {
return (object)[
'count' => $entry->count(),
'duration' => $entry->sum('total_duration'),
'year' => $entry->start_timestamp->year,
'entries' => $entry
];
})*/;
You loop over it like this:
<table>
<thead>
<tr>
<th>Pay week</th>
<th>Date range</th>
<th>Year</th>
<th>Total hours</th>
</tr>
</thead>
<tbody>
#foreach($entries as $pay_week => $groups)
#foreach($groups as $year => $group)
<!--pay_week: {{ $pay_week }}<br />
count: {{ $group->count() }}<br />
duration: {{ $group->sum('total_duration') }}<br />
year: {{ $year }}<br />-->
<tr>
<td>{{ $pay_week }}</td>
<td></td>
<td>{{ $year }}</td>
<td>{{ $group->sum('total_duration') }}</td>
</tr>
#foreach($group as $entry)
#endforeach
#endforeach
#endforeach
</tbody>
</table>

How to fetch value from multiple table through pivot table in Laravel?

I'm trying to fetch value from different tables. I get the value but it repeat same quantity. I'm getting this see this image
I expect this see this image
Here is my controller code
$orders = Order::all();
view('admin.order.index', compact('orders'));
Here is my model relationship
return $this->belongsToMany(FoodItem::class, 'order_food_items', 'order_id', 'food_item_id')->withTimestamps();
}
public function orderItems() {
return $this->hasMany(OrderFoodItem::class);
}
public function user() {
return $this->belongsTo(User::class);
}
Here is in blade code, May be I'm doing wrong
#foreach ($orders as $key => $order)
#foreach ($order->orderFoodItems as $product)
#foreach ($order->orderFoodItemPrice as $price)
<tr>
<th scope="row">{{ $key + 1 }}</th>
<td>{{ $product->name }}</td>
<td>
<img src="{{ asset('/storage/items/food/' . $product->image) }}"
alt="">
</td>
<td>{{ $order->user->name }}</td>
<td>
<span class="badge badge-primary">Pending</span></td>
<td>something</td>
<td>{{ $price->discounted_price }}</td>
</tr>
#endforeach
#endforeach
#endforeach
Can anyone help me?

Facade\Ignition\Exceptions\ViewException Trying to get property 'id' of non-object

I've been trying to fetch all the rows where the user id is. then I got this error. Please help me.
CONTROLLER:
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first();
if (empty($applications)) {
$applications = "0";
return view('user.application.index',compact('applications'));
}
else{
return view('user.application.index',compact('applications'));
}
}
blade file
#foreach ($applications as $application)
<tr>
<td>{{ $loop->index +1 }}</td>
<td>{{ $application->id }}</td>
<td>{{ $application->created_at }}</td>
<td>{{ $application->surname }}, {{ $application->firstname }} {{ $application->middlename }}</td>
#if ($application->status == 1)
<td><p style="color:green;">APPROVED</p></td>
#elseif ($application->status == 0)
<td><p style="color:rgb(243, 180, 8);">PENDING</p></td>
#else
<td><p style="color:RED;">DISAPPROVED</p></td>
#endif
<td><img src = {{ asset('assets/img/eye.ico') }}/>
</td>
</tr>
Try replacing $applications='0' with $applications=[] and see if it works ;)

Resources