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.
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
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.
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);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community