【问题标题】:LARAVEL Eloquent: link post to user then user to profileLARAVEL Eloquent:将帖子链接到用户,然后将用户链接到个人资料
【发布时间】:2018-03-18 18:10:02
【问题描述】:

我的目标是检索用户图像(来自个人资料表列“profile_img”)并显示在每个帖子的页脚上。

我可以使用$post->author->name 检索作者姓名,使用$post->author->profile->profile_image 检索个人资料图片,但它仅在我有一条记录(帖子)时才有效。当有多个记录时,我收到一个错误尝试获取非对象的属性“配置文件”(查看:xampp/......./home.blade.php)

谁能告诉我哪里出错了??

型号:

用户

public function post() 
{
  return $this->hasMany('App\Post');  
}
public function profile()
{
  return $this->hasOne('App\Profile');
}

简介

public function user()
{
  return $this->belongsTo('App\User');
}

发帖

public function author()
{
  return $this->belongsTo('App\User', 'id');
}

控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class MainController extends Controller
{
  public function index() {
  $posts = Post::paginate(9);

return view('pages.home')->with('posts', $posts);
}

主页视图

@if(count($posts) > 0)
@foreach($posts as$posts)
<div>
  <div class="post-title"><h3>{{$post->title}}</h3></div>
  <div class="post-description">
    {!!mb_substr($post>body,10,rand(35,40)) !!} ....
  </div>
  <div class="featured-details">
    <div class="p-clearfix">
       <img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
        <div class="author-title lite">{{ $post->author->name }}</div>
      <div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
    </div>
  </div>
</div>
<hr>
@endforeach

@else
  no post yet
@endif

【问题讨论】:

    标签: php laravel eloquent table-relationships laravel-5.6


    【解决方案1】:

    您的主页视图中似乎存在一些问题。用这段代码再试一次看看。

    主页视图

    @if(count($posts) > 0)
    @foreach($posts as $post)
    <div>
      <div class="post-title"><h3>{{$post->title}}</h3></div>
      <div class="post-description">
        {!!mb_substr($post>body,10,rand(35,40)) !!} ....
      </div>
      <div class="featured-details">
        <div class="p-clearfix">
           <img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
            <div class="author-title lite">{{ $post->author->name }}</div>
          <div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
        </div>
      </div>
    </div>
    <hr>
    @endforeach
    
    @else
      no post yet
    @endif
    

    您甚至可以通过在您的控制器

    上运行像这样的雄辩的模型查询来进一步避免作者的 n+1 问题
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Post;
    
    class MainController extends Controller
    {
      public function index() {
      $posts = Post::with("author")->paginate(9);
    
    return view('pages.home')->with('posts', $posts);
    }
    

    我希望这会有所帮助。

    【讨论】:

    • 感谢 Lamin Barrow 的回复,但仍然可以正常工作,如果我在 foreach 循环中返回 $post-&gt;author,则只会检索一条记录
    • PostUser 之间存在belongsTo 关系,因此作者将只返回一个用户。如果你想要它的倒数,那么你会在User 模型上获得hasMany 帖子Post
    【解决方案2】:

    在您的控制器索引方法中尝试:

    public function index() {
      $posts = Post::with("author.profile")->paginate(9);
    
      return view('pages.home')->with('posts', $posts);
    }
    

    并更新后期模型关系:

    public function author()
    {
        return $this->belongsTo('App\User', 'user_id'); // update to users table foreign key
    }
    

    【讨论】:

    • 仍然报错 Trying to get property 'profile' of non-object (View: xampp/......../home.blade.php) @Shipu
    猜你喜欢
    • 2018-09-27
    • 2014-10-09
    • 2012-01-20
    • 2012-09-06
    • 2020-05-15
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多