Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 2 years ago.
0

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 2 years ago.
0

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

This is my code

	<?
	$timeline = DB::table('Timeline')
						->orderBy('Timestamp', 'desc')
						->get();
	?>
								@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>
											<div class="comment-actions">
												<a href="#"></a>
											</div>
										</div> <!-- / .comment-body -->
									</div> <!-- / .comment -->
								@endforeach
Last updated 2 years ago.
0

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 2 years ago.
0

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 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Joe96 joe96 Joined 28 Apr 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.