Laravel blade echo the value doesn't work - laravel-5

I have two tables ..
User and UserMeta
I made relationship between them with hasOne().
Model
public function user_meta() {
return $this->hasOne('App\UserMeta');
}
Controller
public function index() {
$users = User::orderBy('id', 'desc')->paginate(10);
return view('users.index')->with('users', $users);
}
View
#forelse ($users as $user)
{{ $user->user_meta->country }}
#empty
#endforelse
This return error
Trying to get property 'country' of non-object
But here if I use dd() like this.
#forelse ($users as $user)
{{ dd($user->user_meta->country) }}
#empty
#endforelse
I can see the value return correctly. United State

dd stop loop execution for first element. If you uses dump helper, you will see this error again after several iterations. You need check for null:
#forelse ($users as $user)
#if(!is_null($user->user_meta))
{{ $user->user_meta->country }}
#endif
#empty
#endforelse

Related

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$path

I wrote a relation belongsToMany between products and photos and now I want to show products in the home page. I did it like so:
#foreach ($latestProducts as $product)
<img src="{{$product->photos()->path}}">
#endforeach
homeController:
public function index()
{
$latestProducts = Product::orderBy('created_at' , 'desc')->limit(10)->get();
return view('front.layouts.index' , compact('latestProducts'));
}
photo model:
public function products() {
return $this->belongsToMany(Product::class);
}
product model:
public function photos() {
return $this->belongsToMany(Photo::class);
}
I got Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$path .And when I write {{$product->photos[0]->path}} , the error changes to "Undefined array key 0. Also when I write {{$product->photos->path}} ,I get an error as "Property [path] does not exist on this collection instance."
I believe you got a photo attribute/field in the Photo model. Because photos is a collection of photos, you might want to write:
#foreach ($latestProducts as $product)
#foreach ($product->photos as $photo)
<img src="{{$photo->path}}">
#endforeach
#endforeach
And only the first photo:
#foreach ($latestProducts as $product)
<img src="{{$product->photos->first()->path}}">
#endforeach
// Or to be safe
#foreach ($latestProducts as $product)
<img src="{{optional($product->photos->first())->path}}">
#endforeach
// Or this in php 8
#foreach ($latestProducts as $product)
<img src="{{$product->photos->first()?->path}}">
#endforeach
You have to use
{{ $product->photos->path }}
cause when you deal with relations in blade you deal with it as a property of class of the father class unlike when you deal with it in eloquent ORM you can use photos()
Basically the error is saying that there is no property photos in the $products. Assuming that the table name is correct and the foreign keys too
you can just do:
public function index()
{
$latestProducts = Product::with('photos')->orderBy('created_at' , 'desc')->limit(10)->get();
return view('front.layouts.index' , compact('latestProducts'));
}
And in blade file try to remove the (), because it will load the relationship.
#foreach ($latestProducts->photos as $product)
<img src="{{$product->path}}">
#endforeach

ErrorException Invalid argument supplied for foreach() (View: C:\xampp\htdocs\E-Commerce\resources\views

I am working on an E-commerce application, I want to retrieve an order together with it's associated products but I couldn't get pass that error, I don't know what am missing there. Thanks for help. Here is my code.
public function show(Order $order, Request $request)
{
$order = Order::where('id', $request->order->id)->first();
$products = $order->products();
// dd($products);
return view('orders.order')->with([
'products' => $products
]);
}
And if I dd($products) I got null
This is my views
#foreach ($products as $product)
<li class="">
{{ $product->id }}
</li>
#endforeach
Assuming that products() is a relationship in your Order model, $order->products() will return an instance of Builder.
To get the products, you can simply do:
$order->products

Laravel 5 how paginate with hasMany relationship

I have a relationship this like;
public function foods() {
return $this->hasMany('App\models\food\food', 'category_id', 'id');
}
My controller file content;
$datas = food_category::where('slug', $slug)->with('foods')->paginate(12);
But incoming datas in there all datas and this is causing bad performance. I want to paginate apply relationship datas.
If you want to paginate foods, try adding a separate method for that:
Model:
class food_category
{
public function getFoodsPaginatedAttribute()
{
return $this->foods()->paginate(12);
}
}
Controller:
$datas = food_category::where('slug', $slug)->get();
View:
#foreach ($datas as $data)
#foreach ($data->foods_paginated as $food)
{{ $food->name }}
#endforeach
#endforeach
Pass paginated collection to the view:
//view
#foreach ($datas as $data)
// do what you need
#endforeach
//link
{{$datas->links()}}

Property does not exist on this collection instance

I have a Client model that hasMany Appointment models. And an Appointment belongs to a Client. I am trying to return results that show me the client's name as well as their list of appointments on an index blade. Here is my code so far:
Client Model
public function appointment()
{
return $this->hasMany(Appointment::class);
}
Appointment Model
public function client()
{
return $this->belongsTo(Client::class);
}
Controlller
$clients = Client::with('appointment')->get();
//dd($clients);
return view('scheduler')->withclients($clients);
Blade
#foreach($clients as $client
{{ $client->name }}
{{ $client->appointment->id }}
#endforeach
How can I print $client name and their list of appointments? I have tried other help regarding this issue but I am not clear where I am going wrong.
So you relationship is a One-To-Many relationship. You have a lot of appointments then.
You can iterate over the clients list first and in the inner loop over the appointments:
#foreach($clients as $client)
{{ $client->name }}
#foreach($clients->appointment as $appointment)
{{ $appointment->id }}
{{ $appointment->name }}
#endforeach
#endforeach
Anyway, it might be easier to go over the appointment model, because it's in relation to exactly one client:
#foreach(Appointment::all() as $appointment)
{{ $appointment->name }}
{{ $appointment->client->name }}
#endforeach

Display Fields of Relationship Query in Blade

After binding tables in a model and writing a query, fetched data in a Blade shows error.
Trying to get property of non-object
Model
<?php
public function unit()
{
return $this->belongsTo('TEST\Units');
}
Query
$inst = Instructions::with('unit')
->where('id',$inst_id)
->first()
->get();
Model
#foreach ($inst as $item)
<pre>{{$item->units->name}}</pre>
#endforeach
However, when using this code and I delete a name or other field, no error displays...
#foreach ($inst as $item)
<pre>{{$item->units}}</pre>
#endforeach
output:
{"id":1,"name":"UNIT 101","description":null,"created_at":"2017-03-06 13:30:18","updated_at":"2017-03-06 13:30:18"}
Solved
#foreach ($inst as $item)
#if (!empty($item->unit->id))
<pre>{{$item->unit->name}}</pre>
#endif
#endforeach

Resources