Support the ongoing development of →
posted 8 years ago
Last updated 10 months ago.

You are using ->firstOrFail(), if your query doesn't return any hits it will throw an exception. Use ->first() instead if you don't want an exception when you doesn't get any hits.

Last updated 10 months ago.

If i do that i get "Trying to get property of non-object.

This is my code

	$timeline = DB::table('Timeline')
						->orderBy('Timestamp', 'desc')
								@foreach($timeline as $entry)
								<? $avatar = User::where('Name', $entry->Name)->first(); ?>
									<div class="comment">
										<img src="{{ URL::route('index') }}/assets/images/40skin/{{ $avatar->FavSkin }}.png" alt="" class="comment-avatar">
										<div class="comment-body">
											<div class="comment-by">
												<a href="{{ URL::route('index') }}/public/user/{{ $entry->Name }}" title="">{{ $entry->Name }}</a> {{ $entry->Text }} {{ $entry->Issuer }}
												<span class="pull-right">{{ \Carbon\Carbon::createFromTimeStamp($entry->Timestamp)->diffForHumans() }}</span>
											<div class="comment-actions">
												<a href="#"></a>
										</div> <!-- / .comment-body -->
									</div> <!-- / .comment -->
Last updated 10 months ago.

Depending on what you app is trying to do, you need to account for the fact that there might not be a user who matches those criteria and that you won't have a user model returned. It can be done in the controller, at the app level with error pages or even in the views with if statements.

Last updated 10 months ago.

Hi! The find() method from query builder can return a Model instance or null if no record was found in database. So you have to handle this by checking first if user exists. If he does you can for example show his avatar, otherwise you can show login button.

Your code is a bit messy and I would advice you to not mix logic with view code. You should get user in controller then pass him to view like

$user = User::first();
return View::make('view')->with('user',$user);
Last updated 10 months ago.

Sign in to participate in this thread!


Your banner here too?

Joe96 joe96 Joined 28 Apr 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.