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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建