How to clear undefined variable Laravel 9.48.0 - laravel

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

Related

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?

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

group data in foreach in 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

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