Laravel - retrieving attribute from within a model relationship in a blade view - php

I have created a relationship between products and users using a foreign key called "previous_owner". I can see when I dump the value that I'm passing to the view that the data I'm after is available under:
$product->relations->product_owner->user->attributes->name
But how do I access it from within my view. I'm trying to loop through the products and then do something like:
{{ $product->product_owner->user->name }}
or
{{ $product->product_owner->name }}
But non of it works, keep getting:
" Trying to get property 'name' of non-object "

In your view code must be similar this
#foreach($products as $product)
{{ $product->product_owner->name }}
#endforeach
This code mean each product has product_owner. When one product does not have product_owner that case value of $product->product_owner is Null and then your code is null->name and you get error. For fix it you must be check relation is null or use ?? operator. Change your code to
#foreach($products as $product)
{{ $product->product_owner->name ?? ''}}
#endforeach
or
#foreach($products as $product)
#if($product->product_owner)
{{ $product->product_owner->name}}
#endif
#endforeach

If name is attribute and product_owner is relationship function then try the below:
{{ $product->product_owner()->name }}

Your product_owner isn't stdClass. It is array. So you can't access this key of this array with "->" operator. try
{{ $product->product_owner['name'] }}

Related

Laravel(how to bring two columns from two separate tables in one .blade file) without touching the DB?

What is the most effective way for returning data from two tables in on view?
Like an employee check the vehicle for each order.
Route::get('orderVehicle',"adminController#orderVehicle");
public function orderVehicle(Request $reques){
$orders = new Order;
$vehicles = new Vehicle; $orders->id; $vehicles->id; return view('adminVeiw.orderVehicle',compact('orders','vehicles')); }
#foreach($orders as $or) {{ $or->id }} #endforeach {{ $vehicles->id }}
And the error is
"Trying to get property 'id' of non-object (View:
/var/www/html/full-Restaurant-App-Using-Laravel/resources/views/adminVeiw/orderVehicle.blade.php)"
So any suggestions?
do this if you don't wanna change your code in controller
#foreach($orders as $or)
#if(!empty($or->id))
{{ $or->id }}
#endif
#endforeach
#if(!empty($vahicles->id))
{{ $vehicles->id }}
#endif
in this case you won't get error but i don't know how you wanna this works for you,
i hope it helps
public function orderVehicle()
{
$orders = Order::create();
$vehicles = Vehicle::create();
return view('adminVeiw.orderVehicle', compact('orders', 'vehicles'));
}

How can i iterate over attributes in laravel models?

I have a Laravel model with many attributes. So, I need iterate over these attributes. How can I do this?
Something like this:
#foreach($model->attributes as $attribute)
// use $attribute
#endforeach
is it possible?
If you have an object, use getAttributes():
#foreach($model->getAttributes() as $key => $value)
// use $attribute
#endforeach
If these attributes were returned from a query, you can access the Model class directly:
E.g.
#foreach (User::all() as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
or, you can access the model reference given to a template:
E.g.
#foreach ($user->permissions as $permission)
<p>This is an user permission {{ $permission->id }}</p>
#endforeach
See more in the Laravel Docs

Laravel: print URL or default value in blade view

I have Post model which has user function returning User model - creator of the post.
class Post extends Model
{
/**
* Get the user that owns the post.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
In blade view I achieved how to print author name
{{ $post->user->name or 'Anonymous' }}
Above code works, but it is very hmm sensitive?
For example, I tried change this code to:
{{ $post->user->name, 'Anonymous' }}
<?php $post->user->name or 'Anonymous' ?>
Results? Trying to get property of non-object error on line with this code.
Maybe I am skipping something simple but important. How to print URL or default value in blade view. Pseudo code what I mean:
{{ '' or 'Anonymous' }}
Try
{{ isset($post->user->name) ? '' : 'Anonymous' }}
If this doesn't work (I haven't check it) try something like this:
#if (isset($post->user->name))
#else
Anonymous
#endif
Actually the error is not because the code is sensitive, it is because you are actually trying to access non objects values.
To achieve what you are looking for:
{{ isset($post->user)?'' : 'Anonymous' }}

Laravel Get item syntax

I use Laravel with foreach in a blade.php
{{ $users = App\User::where('user_id', $post->user_id)->get() }}
and I got the result of this which show on the page
[{"user_id":"123","email":"123#com","password":"1234","first_name":"Tony"}]
But I want to get "Tony" only, how can I call?
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Or better in sense of performance, if you select posts with user:
{{ $post->user->first_name }}
But it is better to prepare necessary data in controller.
get() returns an array, you should use first()
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Since you said you are using foreach in blade. Try something like this
#foreach($user as $u)
{{$u->first_name}}
#endforeach
This will give you first_name for all users

Unique collection in Laravel

I need to find out unique value. So I tried bellow code. It is through undefined variable error.
Controller:
$employee = Employee::all();
Return view ('page', compact('employee'));
Page view:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach
//Display Entire data
#foreach($employee as #employee)
//Display all value
#endforeach
But I got an uniqueEmpLoc is undefined error. I'm using LARAVEL 5.1. Please help me to solve this problem.
There are some errors in your code:
I don't think compact(employee) would work. Shouldn't that suppose to be compact('employee') ?
In Blade, there is no need of putting curly braces at all. Remove them.
Try out the following:
$employees = Employee::unique('locations')->values()->list('location')->toArray();
return view('page', compact('employees'));
And then in your view:
#foreach($employees as $employee)
{{ $employee }}
#endforeach
Use this query in controller
$employees = Employee::distinct()->list('location')->toArray();
return view('page', compact('employees'));
In view
#foreach($employees as $employee)
{{ $employee }}
#endforeach
I agree with the other answers here, this code should be in the controller. You shouldn't be doing logic in the views.
In the controller do:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
$employee = Employee::all();
Return view ('page', compact('employee', 'uniqueEmpLoc'));
The reason your code isn't working is the line that defines $uniqueEmpLoc is interpreted by blade as text, not code.
If you really want to do that in the view, you need to wrap it in #php tags.
#php
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#endphp
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach

Categories