Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 模型的拉维通滤波器_Php_Laravel_Laravel 5_Filter - Fatal编程技术网

Php 模型的拉维通滤波器

Php 模型的拉维通滤波器,php,laravel,laravel-5,filter,Php,Laravel,Laravel 5,Filter,我需要一些帮助。本质上,有两个表tblplayers和tblmatches。并非每个球员都存在于TBL比赛中。我的控制器有以下代码: use App\Model\Players; use App\Model\Matches; use Illuminate\Support\Facades\DB; class PlayersController extends Controller { public function index(Request $request) { $players = Pla

我需要一些帮助。本质上,有两个表tblplayers和tblmatches。并非每个球员都存在于TBL比赛中。我的控制器有以下代码:

use App\Model\Players;
use App\Model\Matches;
use Illuminate\Support\Facades\DB;

class PlayersController extends Controller
{
public function index(Request $request) {
$players = Players::select('*');
我想更改上面的select语句,以便它只返回tblmatches中也存在的播放机,其中tblmatches.P1_ID=tblplayers.ID

下面我做错了什么

$players = Players::addSelect(DB::raw('(SELECT * from tblmatches where (P1_ID = ID))'));
我应该改一下型号吗?感谢您的帮助。

将一对多关系详细信息添加到您的玩家模型中,也可能添加到您的比赛模型中

public function matches()
{
    return $this->hasMany('App\Model\Matches');
}
而且

将一对多关系详细信息添加到玩家模型中,也可能添加到比赛模型中

public function matches()
{
    return $this->hasMany('App\Model\Matches');
}
而且


你绝对应该在这两张桌子之间设置一条线。这将使这样的案件更容易处理

然而,你所寻找的本质上是一个存在的地方。因此,下面这样的方法应该可以达到目的

$players = Players::whereExists(function ($query) {
        $query->select(DB::raw(1))
            ->from('tblmatches')
            ->whereRaw('tblmatches.player_id = tblplayers.id');
    })
    ->get();
我假设whereRaw中有这两个字段,但是您应该相应地更改它

whereRaw('tblmatches.player_id = tblplayers.id');

但是,一定要看一下关系:

您一定要在两个表之间设置一个连接。这将使这样的案件更容易处理

然而,你所寻找的本质上是一个存在的地方。因此,下面这样的方法应该可以达到目的

$players = Players::whereExists(function ($query) {
        $query->select(DB::raw(1))
            ->from('tblmatches')
            ->whereRaw('tblmatches.player_id = tblplayers.id');
    })
    ->get();
我假设whereRaw中有这两个字段,但是您应该相应地更改它

whereRaw('tblmatches.player_id = tblplayers.id');
但是,一定要看看关系:

检查此链接

检查此链接


不幸的是,该解决方案引发了一个错误BadMethodCallException Method Illumb\Database\Eloquent\Collection::addSelect不存在。根据错误消息,感觉您正在将addSelect链接到作为集合的结果。你不应该那样做$玩家本身将包含一组你可以使用的玩家模型。它应该可以工作:顺便说一下,这是一个可以使用的工作。不幸的是,该解决方案引发了一个错误BadMethodCallException Method Illumb\Database\Eloquent\Collection::addSelect不存在。根据错误消息,您感觉像是将addSelect链接到了作为集合的结果。你不应该那样做$玩家本身将包含一组你可以使用的玩家模型。它应该有用:顺便说一句,这是一个你可以玩的工作。很好!谢谢你,先生。工作得像个查姆尼斯!谢谢你,先生。工作得很有魅力