我在laravel 5上有一个项目。 我写了一个SQL来从数据库中选择游戏,在执行之后,我得到了模型集合,其中每个模型的id = 1。
这是我的疑问:
select * from `games` left join `game_vendors` on `games`.`vendor_id` =
`game_vendors`.`id` where `game_vendors`.`name` != 'some_vendor' and
`games`.`id` not in (1,2,3,4,5,6,7,8,9,10,11,12);
在mysql终端中检查了这个查询 - 一切都很好,id是正确的,但在邮递员或浏览器中,我得到了这个
array(2021) {
[0]=>
array(32) {
["id"] => int(1)
[1]=>
array(32) {
["id"] => int(1)
...
...
...
[24]=>
array(32) {
["id"] => int(1)
模型类包含:
class Game extends Model {
protected $table = 'games';
public function vendor()
{
return $this->hasOne('App\GameVendor', 'vendor_id', 'id');
}
答案 0 :(得分:0)
使用Laravel Eloquent Relationship和“WHERE NOT IN”
将模型更新为
class Game extends Model {
protected $table = 'games';
public function vendor()
{
return $this->hasOne('App\GameVendor', 'vendor_id', 'id')->where('name', '!=' ,'some_vendor');
}
并使用Eloquent作为
$games = Game::whereNotIn('id',[1,2,3,4,5,6,7,8,9,10,11,12])->get();
foreach($games as $game){
$game->vendor->name
}