【问题标题】:Laravel how to join tables and use where condition using Eloquent?Laravel 如何使用 Eloquent 连接表并使用 where 条件?
【发布时间】:2015-10-26 21:52:47
【问题描述】:

假设有 2 个表:

视频(id、文件名)

匹配项(id、video_id、match_number)

我想通过 match_number 获取视频文件名。我读过,使用 eloquent 可以避免连接。

所以我尝试了以下示例中的各种方法:

视频.php

class Video extends Eloquent {

    protected $table = 'videos';

    public function matches()
    {
        return $this->hasMany('Match', 'videos_id');
    }

    /*public function match()
    {
        return $this->belongsTo('Match', 'id');
    }*/

    /**
     * @param $matchNumber
     * @return mixed
     */
    public function getByMatchNumber($matchNumber)
    {
        return $this
            ->matches()
            //->select('videos.file_name')
            //->limit(10)
            ->where('matches.match_number', $matchNumber)
            ->file_name;
             //   ->match()


    }
}

但没有任何效果。

我认为我假设视频有很多匹配项,因为实际上有很多匹配项具有相同的视频。

那么它应该是什么样子?

更新

Match_number 列是唯一的,例如 id。不能有多个具有相同匹配号的匹配。 一场比赛只有一个视频。 Limit(10) 只是在尝试没有 where 条件,而不是获得数百万个结果。 但是同一个视频被分配给许多匹配项。

更新:

我根据 jedrzej.kurylo 的答案重新制作了函数,并修复了我假设的错误 - whereMatchNumber - 这样的函数将不存在:

public function getByMatchNumber($matchNumber)
    {
       return Match::with('video')
            ->where('match_number', $matchNumber)->first(); //->video->file_name;



    }

但它不起作用。我得到错误:

调用未定义的方法 Illuminate\Database\Query\Builder::video()

更新

在 Match.php 模型中我添加了关系:

public function video()
    {
        return $this->belongsTo('Video', 'videos_id');
    }

仍然遇到同样的错误:

调用未定义的方法 Illuminate\Database\Query\Builder::videos()

更新: 我有一个错误,在gettign错误时尝试添加复数形式并忘记撤消:

return Match::with('videos')

需要:

return Match::with('video')

更新

仍然无法得到结果。我运行这个:

$video = $this->video->getByMatchNumber($this->matchNumber);

print_r($video->file_name);

我在查询日志中看到了很好的查询:

 [1] => Array
        (
            [query] => select * from `matches` where `match_number` = ? limit 1
            [bindings] => Array
                (
                    [0] => 104439
                )

            [time] => 4.62
        )

    [2] => Array
        (
            [query] => select `file_name` from `videos` where `videos`.`id` in (?)
            [bindings] => Array
                (
                    [0] => 1089
                )

            [time] => 37.24
        )

我使用 HeidySQL 中的绑定执行了最后一个查询并找到了结果。那么为什么 $video->file_name 可以为空呢?

更新

我也试过了

die($video->video->file_name);

并得到错误

试图获取非对象的属性

【问题讨论】:

  • 您想通过匹配号获取文件名或视频对象吗?可以有多个具有相同 match_number 的匹配项吗?我可以看到你在匹配列中有 id 和 match_number 所以我假设 match_number 不是唯一的,否则它将是关键。您还在执行“limit(10)”,这表明单个匹配号可以有多个视频。
  • @jedrzej.kurylo - 更新了问题信息

标签: php mysql laravel laravel-4 eloquent


【解决方案1】:

首先,使用 Eloquent 并不是要摆脱连接,而是使用对象表示法更轻松地操作数据。根据您使用它的方式,您将使用联接或不使用联接来获取数据。而且使用连接并没有错,只要使用得当。

要实现您的需要,最简单的方法是:

$match = Match::with('video')->whereMatchNumber($number)->first();
if ($match) {
  echo $match->video->file_name;
}

这将在您的数据库中运行 2 个查询。如果您想使用一个查询来执行此操作,则需要使用连接:

$match = Match::join('videos', 'matches.videos_id', '=', 'videos.id')->whereMatchNumber($number)->first();
if($match) {
  echo $match->file_name;
}

【讨论】:

  • 对我来说这不起作用。我更新了问题,我尝试了什么。到目前为止,我发现操作数据并不容易:)
  • 您在 Match 类中也定义了与视频的关系。你是否有一个?它叫什么?
  • 我没有,因为我在一些博客上看到不需要双向关系,但现在我添加了。更新了问题。
  • 好吧,如果你不使用它就不需要它,但在这里你显然需要一个
  • 不,现在你得到一个关于video()方法的不同错误,而不是video()。你没有在 with() 中使用“videos”而不是“video”吗?
猜你喜欢
  • 2021-07-12
  • 2018-11-06
  • 2015-09-06
  • 1970-01-01
  • 2015-04-23
  • 2012-04-05
  • 1970-01-01
  • 2014-03-27
  • 2021-05-28
相关资源
最近更新 更多