Laravel Get item syntax - php

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

Related

laravel 8 join query table.column_name does not work in blade or controller

here is my code:
$data = Courses::select('courses.id','courses.standid', 'courses.publisher', 'courses.coverpic', 'courses.course_sts', 'standards.standtitle AS stitle', 'subjects.subtitle AS btitle')
->join('subjects', 'subjects.id', '=', 'courses.subjectid')
->join('standards', 'standards.id', '=', 'courses.standid')
->get();
and blade codes are:
#foreach ($data as $row)
<p>{{ $row->stitle }}</p>
#endforeach
the query works fine when i check dd($data) under attributes, but the stitle and btitle columns never shows inside blade as {{ $row->stitle }} or {{ $row->btitle }}
what am I missing or how to collect the joining table column into the blade file?
cheers
Thanks for response. It was the redirect method that taking me back to history page rather then the post update status.
it was return redirect (the_view_name)->with(vars)
instead of return view (the_view_name)->with(vars)
my first experience to ask a question at stackoverflow and resolved by myself.

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

"Undefined variable: users" when trying to use #foreach

I am trying to make foreach loop in laravel to list all users but when I use the code that laravel suggested I get this error:
Undefined variable: users (View: /home/vagrant/Code/SimFly/resources/views/profile.blade.php)
The code causing the error is:
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
Please can someone help me.
To use users variable in a view you need to pass it from a controller first:
$users = User::all();
return view('profile', compact('users'));
Its also a great practice to check state (isset, count for example) of the array/variable before looping through it, to protect any unforeseen errors.
As per the suggestions above, the problem is that the view was not being passed the variable.
To catch this in the blade template use something like a #forelse instead of a #foreach
ProfileController
$users = User::all();
return view('profile', [
'users' => $users
]);
Profile Blade
#forelse
<p>This is user {{ $user->id }}</p>
#empty
<p>There are no users</p>
#endforelse

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

Laravel Count with where inside blade

I'm trying to get the total comments the user have..
Controller:
public function index()
{
$setting = Setting::findOrFail(1);
$comments = Comment::where('published', '=', '1')->get();
$users = User::all();
return view('page.index', compact('setting', 'comments', 'users'));
}
View:
#foreach($comments as $comment)
{{ count($users->where('user_id', '=', $comment->user_id)) }}
#endforeach
The problem is that it only returns 0 and i have 2 comments there.. even using the user id to instead of "$comment->user_id" it doesnt work. still display 0.
$users is a collection, not a query. Treat it as such:
#foreach ($comments as $comment)
{{ $users->where('user_id', $comment->user_id)->count() }}
#endforeach
The collection's where method does not take an operator.
From the wording in your question it seems you actually want it the other way around:
$comments = Comment::wherePublished(1)->get()->groupBy('user_id');
Then in your view:
#foreach ($users as $user)
{{ $comments->has($user->id) ? count($comments[$user->id]) : 0 }}
#endforeach
I'm late to answer your question and Joseph already showed you the problem but you may also do it differently. You can group comments using Collection::groupBy, for example:
$comments = Comment::all()->groupBy('user_id');
Then in your view you may try this:
#foreach($comments as $user => $userComments)
// count($userComments)
// Or
#foreach($userComments as $comment)
// {{ $comment->title }}
#endforeach
#endforeach
In the first loop (#foreach($comments as $user => $userComments)), the $user is the user_id and it's value would be all comments (an array) by this user but in group.

Categories