I was trying to make that when there was a post made by user John whit
user_id
1
It check if he has an profile and if he has an profile it puts the image next to it that corresponds to the user and the profile he or she created. so when I login whit the user it works fine because I'm getting the user id out of
Auth::user()->id
But when I log in whit another user and look at the page I get off an error.
Error:
Trying to get property 'id' of non-object (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\blogesources\views\posts\post.blade.php) (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\blogesources\views\posts\post.blade.php)
so mine question is How can I make that the Image that belongs to the user is connected to the post the user image and post are in 3 different tables. here how I try to do it now?
Post.blade.php
<div class="blog-post">
<h2 class="blog-post-title">
<a href="{{ route('posts.show', ["post" => $post->id]) }}">
{{ $post->title }}
</a>
</h2>
<p class="blog-post-meta">
@foreach($image as $path)
@if(Auth::user()->id == $path->user_id)
{{--{{dd($path)}}--}}
@if(!$path->image_path == null)
<img src="/images/{{ $path->image_path }}" id="imgProfile" style="width: 150px; height: 150px" class="img-thumbnail" />
@else
<img src="http://placehold.it/150x150" id="imgProfile" style="width: 150px; height: 150px" class="img-thumbnail" />
@endif
@endif
@endforeach
<a href="#">{{ $post->user->name }}</a> on
{{ $post->created_at->toFormattedDateString() }}
</p>
<p>
{{
$post->body
}}
</p>
<br>
@if (Auth::check())
@if(Auth::user()->id == $post->user_id)
<div class="button-box col-lg-12">
<a href="{{ route('posts.edit', ["post" => $post->id]) }}" class="btn btn-info" role="button">Edit</a>
<a href="{{ route('posts.delete', ["post" => $post->id]) }}" class="btn btn-info" role="button">Delete</a>
</div>
@endif
@endif
<hr>
AppService provider
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use View;
use App\Profile;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Schema::defaultStringLength(191);
view()->composer('layouts.sidebar', function($view){
$view->with('archives', \App\Post::archives());
});
View::share('image', Profile::all());
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
If u need anything like extra information note it and I will edit it in