group data in foreach in laravel - laravel

I have this table
here's the index function
use App\GrantLoanAmount;
use App\LoanAmount;
use App\User;
use App\Profile;
public function index(Request $request)
{
$defaultAmount = LoanAmount::where('default', 1)->first();
$grantloanamounts = GrantLoanAmount::with('users','amounts')->get();
return view('loan.grant-loan-amounts.index', compact('grantloanamounts'))
->with('defaultAmount',$defaultAmount);
}
here's my index blade table
#foreach($grantloanamounts as $item)
#if($item->amounts[0]->amount > $defaultAmount->amount)
<tr>
<td>{{ $item->users[0]->email }}</td>
<td>{{ $item->amounts[0]->amount }}</td>
<td>{{ $item->users[0]->name }}</td>
<td class="text-right">action buttons</td>
</tr>
#endif
#endforeach
How can I group the user name to become look like this?
username
amount
amount
amount

Related

How to clear undefined variable Laravel 9.48.0

public function student(){
$users=DB::select('select * from details');
return view('welcome',['users'->$users]);
}
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email}}</td>
<td>{{ $user->status}}</td>
<td>{{ $user->subject}}</td>
</tr>
#endforeach
don't use raw sql query instead you can do it like this
public function student()
{
$users=Details::all();
return view('welcome',compact('users'));
}
or like this
public function student()
{
$users = DB::table('details')
->select('*')
->get();
return view('welcome', compact('users'));
}
and then in your blade you could get it like this
#isset($users)
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->status }}</td>
<td>{{ $user->subject }}</td>
</tr>
#endforeach
#endisset

how to retrieve one to many relationship. Property [produks] does not exist on this collection instance

Operator Model
public function produk()
{
return this->hasMany(Produk::class);
}
Produk Model
public function operator()
{
return this->belongsTo(Operator::class);
}
Controller
public function operator()
{
$data = Operator::all();
return view('data', compact('data'));
}
View
#foreach ($data as $o)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $o->n_oper }}</td>
<td>
#foreach ($data->produk as $p)
{{ $p->n_prod }}
#endforeach
</td>
</tr>
#endforeach
Output
Exception
Property [produk] does not exist on this collection instance. (View: C:\xampp\htdocs\laravel\oni\resources\views\data.blade.php)
what went wrong? please kindly assist me, im new to this
this is a typo may be?? whatever, you are calling relationship on collection. relation belongs to an object.
#foreach ($data as $o)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $o->n_oper }}</td>
<td>
#foreach ($o->produk as $p)
{{ $p->n_prod }}
#endforeach
</td>
</tr>
#endforeach
data is the collection. you have to call relationship on $o
and there's some other typos may be. you are missing the $ sign in $this keyword. update your relationship
public function produk()
{
return $this->hasMany(Produk::class);
}
and
public function operator()
{
return $this->belongsTo(Operator::class);
}
You are calling relation on collection. You have to call it on single instance like this
#foreach ($data as $o)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $o->n_oper }}</td>
<td>
#foreach ($o->produk as $p)
{{$p->n_prod}}
#endforeach
</td>
</tr>
#endforeach
And also change
return this->hasMany(Produk::class);
this to
return $this->hasMany(Produk::class);
and also for other relation like
return this->belongsTo(Operator::class);
to
return $this->belongsTo(Operator::class);
I hope its work.

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?

Trying to get property 'column_name' of non-object while displaying data on view

I want to show data on my blade view from relationship data but when am trying to display data whereas table contains only one row of data it's showing on view but if I insert more than one data in table it's giving me an error.
I have three tables courses, sections, course_section. In course_section table these are the following columns course_id and section_id.
I have tried {{ $section->courses()->first()->courseTitle }} this snippet in view found on stackoverflow.
My Section Model code:-
class section extends Model
{
public function courses(){
return $this->belongsToMany('App\Model\admin\course\course','course_sections','course_id');
}
}
My Section Controller code:-
$sections = section::with('courses')->orderBy('id','DESC')->get();
return view('backend.courses.section.all',compact('sections','courses'));
My view code:-
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $section->courses()->first()->courseTitle }}</td>
</tr>
#endforeach
I am getting this error
"Trying to get property 'courseTitle' of non-object (View:
resources/views/backend/courses/section/all.blade.php)"
Here is the following you are doing wrong:
Replace $section->courses() with $section->courses as you are already doing early loading. And $section->courses() will query to db again.
Check if relational data exists then show.
So your code would be as follow:
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>
#php
$course = $section->courses->first();
#endphp
{{ $course->courseTitle or "" }}
</td>
</tr>
#endforeach
Let me know if it helps!
Edited:
As per conversation the relationship has been changed like course ->hasMany -> sections and section ->belongsTo -> course so the blade would be change like.
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>
#php
$course = $section->course;
#endphp
{{ $course->courseTitle or "" }}
</td>
</tr>
#endforeach
Section Controller:
$sections = section::with('courses')->orderBy('id','DESC')->get();
return view('backend.courses.section.all', compact('sections'));
In the View you have to loop the sections and then the courses and create each row. For ex:
#foreach ($sections as $section)
#foreach ($section->courses as $course)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $course->courseTitle }}</td>
</tr>
#endforeach
#endforeach
Note it's $section->courses and not $section->courses(), because the related courses are already there, you doesn't need to query them again.
Update
Or you can do the query over course
$courses = course::with('sections')->get();
return view('backend.courses.section.all',compact('courses'));
And in the View:
#foreach ($courses as $course)
#foreach ($course->sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $course->courseTitle }}</td>
</tr>
#endforeach
#endforeach

laravel get Data from table using model in laravel 5.2

i want to get vendor_name and user who assign order to vendor in view. But every time i got this error
ErrorException in b6bb559eccdc8a2d45a2d2d6ce89e8e217411386.php line 26:
Trying to get property of non-object (View: E:\xampp\htdocs\ftdindia\resources\views\view\Orders\allorder.blade.php)
in b6bb559eccdc8a2d45a2d2d6ce89e8e217411386.php line 26
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
my codes are given below
Controller for Order
public function allorder(){
$orders = OrderGrid::paginate(100);
return view ('view.Orders.allorder', ['orders' => $orders]);
//dd($orders->all());
}
Here is My Model
class OrderGrid extends model{
protected $table = 'order_grids';
public function vendor() {
return $this->belongsTo(User::class);
}
public function assignedBy() {
return $this->belongsTo(User::class, 'vendor_assigned_by_user');
}
}
Here is my view
#foreach($orders as $order)
<tr>
<td> {{ $i }}</td>
<td>{{ $order->order_id }}</td>
<td>{{ $order->vendor->username }}</td>
<td>{{ $order->assignedBy->username }}</td>
<td>{{ $order->delivery_city }}</td>
<td>{{ $order->delivery_date }}</td>
<td>{{ $order->delivery_time }}</td>
<td>{{ $order->order_statuss }}</td>
<td>{{ $order->order_status }}</td>
<td>
#if(!$order->vendor_id)
Unassigned
#else
Assigned
#endif
</td>
<td></td>
</tr>
<?php $i +=1; ?>
#endforeach
#foreach($orders as $order)
<tr>
<td> {{ $i }}</td>
<td>{{ $order->order_id }}</td>
<td>{{ $order->vendor->username }}</td> inspite of this do <td>{{ !is_null($order->vendorName) ? $order->vendorName->username : null }}</td>
<td>{{ $order->assignedBy->username }}</td> and same here also <td>{{ !is_null($order->assignedBy) ? $order->assignedBy->username : null }}</td>
<td>{{ $order->delivery_city }}</td>
<td>{{ $order->delivery_date }}</td>
<td>{{ $order->delivery_time }}</td>
<td>{{ $order->order_statuss }}</td>
<td>{{ $order->order_status }}</td>
<td>
#if(!$order->vendor_id)
Unassigned
#else
Assigned
#endif
</td>
<td></td>
</tr>
<?php $i +=1; ?>
#endforeach
and in Model
public function vendorName() {
return $this->belongsTo('App\Model\User', 'vendor_id', 'id');
}
public function assignedBy() {
return $this->belongsTo('App\Model\User', 'assigned_by', 'id');
}
The error you are getting relates to how you are calling the vendor() function in the $order collection.
The docs are a good place for a solid understanding on this, but essentially all Eloquent relationships are defined via functions and so you may call those functions to obtain an instance of the relationship without actually executing the relationship queries.
As such it needs to be:
<td>{{ $order->vendor()->username }}</td>
Nothing that we are now chaining vendor() rather than vendor.

Resources