【问题标题】:Laravel Elloquent Model Table RelationsLaravel Eloquent 模型表关系
【发布时间】:2018-10-20 16:14:12
【问题描述】:

我正在学习 Laravel,但有一件事我无法解决。

我有什么:

用户、帖子、评论和相应模型表

用户.php:

public function comments() { return $this->hasMany(Comment::class); }

public function publishComment(Comment $comment)
    {
        $this->comments()->save($comment);
    }

Post.php:

public function comments() { return $this->hasMany(Comment::class); }

comment.php:

public function post() { return $this->belongsTo(Post::class); } 

public function user() { return $this->belongsTo(User::class); } 

我想要什么?

由于我有博客,一个登录用户可以创建许多帖子(这可行)。 但是同一个登录用户可以为一篇文章创建许多 cmets。 (用户有很多 cmets,post 有很多 cmets)。

表格:

用户:|编号 |姓名 |电子邮件 |密码

评论:|编号 |用户 ID | post_id |身体

帖子:|编号 |用户 ID |标题 |身体

我尝试了什么? 我创建了控制器

评论控制器:

class CommentsController extends Controller

{

public function store(Post $post)
{

    $this->validate(request(), ['body' => 'required|min:2']);

    auth()->user()->publishComment(

        new Comment(request('body'));

    );

    return back();

}

在我的刀片文件中,我只想要 $comment->user->name(获取评论所属的用户名)

我得到的错误试图获取非对象的属性

<?php echo e($comment->user->name); ?>

任何帮助将不胜感激。

【问题讨论】:

  • 您是否将 $comment 变量传递给刀片视图?
  • @foreach($post->cmets as $comment) 是的,抱歉没有提到这个

标签: laravel


【解决方案1】:

您需要Polymorphic Relations 关系。

表结构

posts
    id - integer
    title - string
    body - text
    user_id - integer

users
    id - integer
    name - string
    email - string
    password - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

commentable_id 中保存用户id 或帖子id
User 模型或 Post 模型保存到 commentable_type
您还可以使用 cmets 表创建另一个表。

模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    /**
     * Get all of the owning commentable models.
     */
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * Get all of the post's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

class User extends Model
{
    /**
     * Get all of the users's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

查看文档以获取更多详细信息。

在这个视频中你有一个很好的解释https://youtu.be/lePjXdMC6aM

【讨论】:

  • 好的,这似乎正是我所需要的。你能解释一下为什么我的解决方案不起作用吗?是因为试图为一个模型分配更多关系吗?
  • 不,关系数量没有问题。我不知道,但是...如果 Omar Tarek 的答案不是问题,我认为您的问题出在控制器中,该控制器返回带有发布数据的视图。
  • 您能在@foreach 循环中执行{{ dd($comment) }} 并将输出放在这里吗?
  • 评论 {#202 ▼ #guarded: [] #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] # withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:6 [▶] #original: array:6 [▶] #changes: [] #casts: [] #dates: [] # dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #fillable: [] }跨度>
  • 你必须这样做 auth()-&gt;user()-&gt;comments()-&gt;save($comment); OR auth()-&gt;user()-&gt;comments()-&gt;create(request()-&gt;all()); 我认为最后一种方式是最好的,因为你把所有东西都放在了一行中跨度>
【解决方案2】:

您可以在 view.blade.php 中简单地执行此操作:

// Here I am assuming you have a $post variable that contains your blog post.
<ul>
    @foreach ($post->comments as $comment)
      <li>{{ $comment->user->name }}</li>
    @endforeach
</ul>

这将遍历您为博客文章拥有的所有 cmets 并显示所有者的姓名,希望这会有所帮助。

【讨论】:

  • 那么你可能没有 $post 变量?向我们展示您的 index 方法中的代码,即您最初用于加载刀片页面的方法。
【解决方案3】:

如果您只需要创建评论的人的姓名,请尝试此操作。

{{ $post->user->name }}

或者

用户->名称); ?>

【讨论】:

  • $post->user->name 将用户的名称与帖子关联。但是,如果帖子上有不同的用户 cmets 怎么办? (就像 Pavel cmets Adam 的帖子)
  • 您的帖子模型中有与帖子表相关的评论表。通过添加 {{ $post->user->name }} 它将列出所有评论过帖子的用户。试试看
  • 我在 sql 表中添加了一条评论,其中包含创建帖子的不同用户的 id,您的解决方案给了我帖子创建者的名称。在此解决方案中,我没有看到与评论 - > user_id 的连接。只有一个与 Post 的连接
  • 将 cmets 与用户表建立关系,然后尝试使用 tinker 访问每个用户的评论表。通过这种方式,您可以找到显示所有评论过特定帖子的用户的最佳方式。公共函数 cmets() { return $this->hasMany(Comment::class); }
  • 将 cmets 与用户表建立关系,然后尝试使用 tinker 访问每个用户的评论表。通过这种方式,您可以找到显示所有评论过特定帖子的用户的最佳方式。在 Comment 模型中添加此关系,然后访问用户。 public function users() { return $this->belongsTo(User::class); }
猜你喜欢
  • 2016-02-26
  • 2021-10-01
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
相关资源
最近更新 更多