douduandiao1368 2019-02-21 11:08
浏览 38
已采纳

在Laravel 5.7中与用户对应的帖子旁边添加个人资料图片

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

  • 写回答

3条回答 默认 最新

  • douchan6512 2019-02-21 11:34
    关注

    as Suborno said, you shouldn't use Auth::user()->id since this will always put the image of the logged in user there.
    You want to link a user to the post with a one to many relationship and then you can use $post->user->id https://laravel.com/docs/5.7/eloquent-relationships#one-to-many

    class post extends Model
    {
        public function user()
        {
            return $this->belongsTo('App\User');
        }
    }
    
    @foreach($image as $path)
            @if($post->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
    

    note: this all depends on how you have your models/database set up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新