当用户John whit
user_id
1发表帖子时,我试图做到这一点
它检查他是否具有个人资料,如果他具有个人资料,则会将与用户和他或她创建的个人资料相对应的图像放在旁边。因此,当我登录用户时,它工作正常,因为我从
Auth::user()->id
但是当我登录另一个用户并查看页面时,我得到了一个错误。
错误:
试图获取非对象的属性“ id”(查看: C:\ Users \ Merlijn \ AppData \ Roaming \ Composer \ Laravel 项目\博客\资源\视图\帖子\ post.blade.php)(查看: C:\ Users \ Merlijn \ AppData \ Roaming \ Composer \ Laravel 项目\博客\资源\视图\帖子\ post.blade.php)
所以我的问题是如何使属于用户的图像连接到帖子,而用户图像和帖子位于3个不同的表中。 这里我现在该怎么做?
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提供程序
<?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()
{
//
}
}
如果您需要其他信息,请注意,我将在其中进行编辑
答案 0 :(得分:0)
如果您未登录,您希望 Auth :: user()-> id 正常工作吗?
Auth :: user()仅具有已登录用户的会话数据。
将此条件 @if(Auth :: user()-> id == $ path-> user_id)更改为更适合的名称,例如(!empty)如果您希望访问多个用户数据或根本不登录。
答案 1 :(得分:0)
尝试一下
@foreach($image as $path)
@if(Auth::user()->id == $path->user_id)
@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
@else
<img src="http://placehold.it/150x150" id="imgProfile" style="width: 150px; height: 150px" class="img-thumbnail" />
@endif
@endforeach
希望这会有所帮助:)
答案 2 :(得分:0)
就像Suborno所说的那样,您不应该使用Auth::user()->id
,因为这将始终把已登录用户的图像放在此处。
您想要将用户链接到具有一对多关系的帖子,然后可以使用$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
注意:这全部取决于您如何设置模型/数据库。