Php Laravel如何连接表并使用where条件使用雄辩?

Php Laravel如何连接表并使用where条件使用雄辩?,php,mysql,laravel,laravel-4,eloquent,Php,Mysql,Laravel,Laravel 4,Eloquent,假设有两个表: 视频(id、文件名) 匹配项(id、视频\u id、匹配号) 我想通过匹配号获得视频文件名。我已经读到,使用雄辩,你可以避免加入 因此,我尝试了以下示例中的各种方法: Video.php class Video extends Eloquent { protected $table = 'videos'; public function matches() { return $this->hasMany('Match', 'vid

假设有两个表:

视频(id、文件名)

匹配项(id、视频\u id、匹配号)

我想通过匹配号获得视频文件名。我已经读到,使用雄辩,你可以避免加入

因此,我尝试了以下示例中的各种方法:

Video.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)只是在没有任何条件的情况下尝试,无法获得数百万的结果。 但同一个视频被分配给许多比赛

更新:

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

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



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

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

更新

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

public function video()
    {
        return $this->belongsTo('Video', 'videos_id');
    }
仍然收到相同的错误:

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

更新: 我犯了一个错误,在GetSign error时尝试添加复数形式,但忘记撤消:

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\u name可以是空的呢

更新

我也试过了

die($video->video->file_name);
并得到错误


尝试获取非对象的属性

首先,使用Elounce并不是为了摆脱连接,而是为了使用对象表示法更容易地操作数据。根据您使用它的方式,您将使用联接或不使用联接来获取数据。只要正确使用联接,使用联接并没有错

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

$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;
}

是否要按匹配号获取文件名或视频对象?是否有多个匹配项具有相同的匹配项编号?我可以看到您在matches列中同时有id和match_number,所以我假设match_number不是唯一的,否则它将是键。你也在做“限制(10)”,这意味着一个比赛号码可以有多个视频。@jedrzej.kurylo-更新的问题信息对我来说这不起作用。我更新了问题,我尝试了什么。到目前为止,我发现操纵数据并不容易:)你们在Match类中定义了和视频的关系。有吗?它叫什么?我没有,因为我在一些博客上读到,两种方式都不需要关系,但现在我补充说。更新了问题。如果你不使用它,就不需要了,但是这里你显然需要oneNo,现在你得到的是关于videos()方法的不同错误,而不是video()。在with()中,您不是使用了“videos”而不是“video”吗?