【问题标题】:Eloquent Injected Multiple Model RelationshipsEloquent 注入多个模型关系
【发布时间】:2018-11-26 12:10:48
【问题描述】:

所以我在 JeffreyWay 的截屏视频中了解到,我可以使用 Eloquent 从注入到另一个模型的模型中获取关联的 id。

我正在关注他关于 Laravel 5.4 的系列文章。

在这里,我有一个用户与帖子的一对多关系。

应用/发布

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

在我的用户模型中,我有一个发布方法,其中注入了 Post 模型。 publish 方法用于在数据库中创建一个 post 条目。

应用/用户

public function posts()
{
    return $this->hasMany(Post::class);
}

public function publish(Post $post)
{
    $this->posts()->save($post);
}

然后我的 PostsController 中有一个 store 方法,它调用我的用户模型中的 publish 方法。

PostsController

class PostsController extends Controller
{
     public function __construct()
    {
        $this->middleware('auth')->except(['index', 'show']);
    }

    public function store()
    {
        auth()->user()->publish(
           new Post(request(['title', 'body']))
        );
    }
}

当发布方法被调用时,注入的 Post 类会自动将 user_id 设置为 save 方法。

我的问题是,在每个帖子都有 cmets 的情况下,我如何建立这样的关系。这些 cmets 与帖子和创建评论的用户相关联。

简而言之,当我调用 addComment 方法时,我应该同时拥有 user_id 和 post_id。

【问题讨论】:

    标签: laravel eloquent laravel-5.6


    【解决方案1】:

    用户模型:

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

    帖子模型

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

    评论模型

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

    示例问题

    1) 获取用户cmets:

    解决方法:auth()->user()->cmets()->get();

    2) 从给定的评论中获取用户:

    解决方案:Comment::find($someCommentId)->user()->first()->name;

    3) 获取特定帖子的所有 cmets。

    解决方法:Post::first()->cmets()->get();或eager load Post::with('cmets')->first();

    4) 加载评论时加载用户:

    解决方案:Comment::with('user')->first();

    5) 同时加载特定用户帖子和该帖子的 cmets:

    解决方法:User::with('posts.cmets')->first();

    【讨论】:

      【解决方案2】:

      在你的问题中你写道:

      简而言之,当我调用 addComment 方法时,我应该同时拥有 user_id 和 post_id。

      这是绝对正确的,没有问题。您不必通过$user->posts()->save($post) 之类的方法设置这些属性 - 这只是为您完成这项工作的便捷方法(请参阅框架代码中的save($model) 和相关的setForeignAttributesForCreate($model);这些方法只是设置外部关键财产)。

      其实以下三种创建新帖的方式是可以互换的:

      // what you did
      $user->posts->save(
          new Post([
              'title' => 'Hello', 
              'body' => 'World!',
          ])
      );
      
      // equivalent
      Post::create([
          'user_id' => \Auth::user()->id, // or \Auth::id()
          'title' => 'Hello',
          'body' => 'World!',
      ]);
      
      // also equivalent
      $post = new Post([
          'user_id' => \Auth::user()->id, // or \Auth::id()
          'title' => 'Hello',
          'body' => 'World!',
      ]);
      $post->save();
      

      在存储新评论时,您很可能会有这样的控制器,因为评论始终属于帖子,因此您需要帖子的引用:

      class CommentsController extends Controller
      {
          public function __construct()
          {
              $this->middleware('auth')->except(['index', 'show']);
          }
      
          public function store(Post $post)
          {
              $comment = new Comment(request(['body']));
              $comment->user_id = \Auth::user()->id;
              $comment->post_id = $post->id;
              $comment->save();
          }
      }
      

      你也可以简写成:

      Comment::create(
          array_merge(request(['body']), ['user_id' => \Auth::id(), 'post_id' => $post->id])
      );
      

      【讨论】:

        猜你喜欢
        • 2015-09-26
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        • 2016-02-26
        • 2019-09-20
        • 2020-03-27
        • 1970-01-01
        • 2021-08-27
        相关资源
        最近更新 更多