【问题标题】:Trying to get data via related table in Laravel试图通过 Laravel 中的相关表获取数据
【发布时间】:2017-12-23 10:25:37
【问题描述】:

我要做的是显示用户在个人资料中保存的帖子。我将尝试参考我的代码尽可能好地解释它。所以:

public function userProfil($id)

我有从 userprofile 表中获取数据的配置文件功能。在里面我有以下保存数据的代码:

$authed = User::find($id);
$savedarticles = $authed->mysaves;
$allsavings = DB::select("Select * from article where id=$savedarticles->id");

但是这段代码无论如何都不是这样工作的。我可以这样做:

$authed = User::find($id);
$savedarticles = $authed->mysaves;

但是当我尝试使用 mysaves 的 article_id 从文章表中获取文章时,它不起作用,如下所示:

$allsaved= DB::table('article')->where('id', $savedarticles->article_id);

它给出的错误是这样的:

此集合实例上不存在属性 [article_id]。

虽然 savearticle 表有 article_id 我可以在没有上面的行的情况下输出它,并且在视图中我得到它们:

@foreach($savedarticles as $savedarticle)
    <p>{{$savedarticle}}</p>
@endforeach

它为我提供了 savearticle 表中的所有内容,我可以得到 savedarticle-&gt;article_id 并得到 article_id 但无法在控制器中得到它。

我正在使用 Laravel 5.4。

【问题讨论】:

    标签: php mysql laravel-5.4 relationship


    【解决方案1】:

    错误消息Property [article_id] does not exist on this collection instance. 表示您试图从集合中获取单个实例的属性。

    例如,集合可能是这样的

    [$article1, $article2, $article3]
    

    因此您尝试做的事情类似于

    [$article1, $article2, $article3]->article_id
    

    您正试图从集合而不是单个实例中获取属性。

    对于您的查询,您可以使用where in sql 语句来搜索匹配数组中任何项目的行

    $allsaved= DB::table('article')->whereIn('id', $savedarticles->pluck('article_id')->all());
    

    【讨论】:

    • 抛出错误Call to a member function pluck() on array
    • 我不知道它什么时候变成了一个数组,但你可能在$savedarticles = $authed-&gt;mysaves;之后更改了代码
    • aaaaahhh yeaaah 我做到了 :D 让我再试一次
    • 我试图把东西转回来,现在我得到的错误是 htmlspecialchars() 期望参数 1 是字符串,给定对象(查看:C:\wamp64\www\yazilimhaber \resources\views\userProfil.blade.php)
    【解决方案2】:

    我的理解是一个用户有很多帖子,一个帖子属于一篇文章。

    如果这是真的,那么您必须执行以下操作。

    1:在 USER 模型中定义一个关系以获取所有帖子。如下所示。

    public function posts() {
       // Foreign key will be a key that is stored in posts table and represent the user MAY BE: user_id
       $this->hasMany(Posts::class, 'foreign_key', 'local_key')
    }
    

    这将允许您获取属于某个用户的所有帖子。

    2:在帖子中,模型定义了如下所示的用户关系。

    public function user() {
       $this->belongsTo(User::class, 'foreign_key', 'local_key');
    }
    

    这将允许您获得一个帖子用户;

    3:现在在您的控制器中,您将拥有类似的东西。

    public function show($user_id) {
    
          // find a user with posts as eager loading(to avoid query again)
          $user = User::with(['posts'])->where('id', $user_id)->first();
    
          // get all posts that belong to this user
          $posts = $user->posts; 
       }
    

    在控制器 show($user_id) 方法中,您将拥有用户数据以及用户发布数据。现在,如果您想获得帖子关系,则只需定义如下。假设一个帖子也属于一篇文章。

    4:在帖子中,模型定义了获取文章的关系。

    public function article() {
      // This will allow you to get a post artcle
      $this->belongsTo(Article::class, 'foreign_key', 'local_key');
    }
    

    现在您可以在查找用户的同时获取文章。请看下文。我正在重写控制器显示操作以让您更好地理解。

    5:用user_id获取用户

    public function show($user_id) {
    
    // find a user with posts as eager loading(to avoid query again)
    // eager loading for posts & post child, this will give you NOSQL at runtime and all data will come from one query. 
              $user = User::with(['posts', 'posts.article'])->where('id', $user_id)->first();
    
              // get all posts that belong to this user
              $posts = $user->posts; 
    foreach($posts as $post) {
      $article = $post->article; // Child relation of post. 
    }
    
       }
    

    希望您能理解流程,您必须确保模型关系才能完美运行。如果您需要进一步的帮助,请告诉我。

    【讨论】:

    • 好吧,这非常接近我试图告诉的内容。用户保存了文章而不是帖子,保存的文章用于存储article_id和user_id。当用户试图在其个人资料中查看已保存的文章时,我需要从 savearticle 表中获取 article_id,其中 user_id 是我发送的 id,然后我将从文章表中获取与我在保存文章中的 article_id 相等的文章。但是你在这里做的逻辑是双向的,我会尝试建立联系。
    • 我不明白这条线到底应该做什么$user = User::with(['posts', 'posts.article'])-&gt;where('id', $user_id)-&gt;first();
    • 尤其是'posts.article'])-&gt;where('id', $user_id)
    • 这更像是一个用户保存了文章,一个用户可以保存许多文章。
    • 好的,现在一切正常,直到foreach,您知道可能是什么原因吗?顺便说一句,我不确定$article 会给我,单行还是所有帖子?我应该使用 foreach 在视图中输出结果还是立即输出 $article?
    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 2020-12-30
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多