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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀