Laravel DB::select() 不返回“正确”数据
Posted
技术标签:
【中文标题】Laravel DB::select() 不返回“正确”数据【英文标题】:Laravel DB::select() does not return "correct" data 【发布时间】:2021-07-29 09:23:50 【问题描述】:我正在使用 Laravel 8.12
我正在使用 DB::raw() 方法过滤进行 DB::select() 调用。但为了方便起见,我也会发布带有值的完整声明。
这里是查询的php代码
$sql = "SELECT `medium_info`.* , `postings`.`posting_timestamp` FROM `postings` INNER JOIN `medium_info` ON `postings`.`medium_info_id` = `medium_info`.`id` INNER JOIN `accounts` ON `accounts`.`id` = `postings`.`account_id` INNER JOIN `merchants` ON `merchants`.`account_holder_id` = `accounts`.`account_holder_id` INNER JOIN `medium_types` ON `medium_types`.`id` = `accounts`.`medium_type_id` WHERE `merchants`.`account_holder_id` = :merchant_account_holder_id AND `medium_info`.`id` = :medium_info_id AND `medium_types`.`id` = :medium_types_id";
$result = DB::select ( DB::raw($sql), ['merchant_account_holder_id'=>230124, 'medium_info_id'=>551678, 'medium_types_id'=>1] );
当我打印 $result 时,它会给我这样的数据:
[0] => stdClass Object
(
[id] => 230124
[purchase_date] => 2020-11-22
[redemption_date] =>
[expiration_date] =>
[hold_until] => 2021-05-07 02:30:08
...more medium_info data here
[posting_timestamp] => 2020-11-25 23:27:13
...merchants table data which I did not request
[account_holder_id] => 230124
[name] => Best Buy
[logo] => /cdn/merchants/230124/logo.png
如果我执行以下操作,结果仍然相同:
$sql = "SELECT `medium_info`.* , `postings`.`posting_timestamp` FROM `postings` INNER JOIN `medium_info` ON `postings`.`medium_info_id` = `medium_info`.`id` INNER JOIN `accounts` ON `accounts`.`id` = `postings`.`account_id` INNER JOIN `merchants` ON `merchants`.`account_holder_id` = `accounts`.`account_holder_id` INNER JOIN `medium_types` ON `medium_types`.`id` = `accounts`.`medium_type_id` WHERE `merchants`.`account_holder_id` = 230124 AND `medium_info`.`id` = 551678 AND `medium_types`.`id` = 1";
$result = DB::select ( $sql));
但是,当我在 phpMyAdmin 中运行此查询时,它会为我提供带有来自 medium_info 表的“id”的“正确”结果。截图如下:
我想在此处添加通过 DB::select() 查询收到的结果附加了“商家”行,我在查询中没有请求。即使我只是做SELECT `postings`.`posting_timestamp` FROM...
请求,它也会给我这个结果:
Array
(
[0] => stdClass Object
(
[posting_timestamp] => 2020-11-25 23:27:13
[account_holder_id] => 230124
[id] => 230124
[name] => Best Buy
[logo] => /cdn/merchants/230124/logo.png
[description] => <p>When technology meets life, they come together at Best Buy®. Best Buy has the technology that’s fun and functional, from tablets and videogames, to appliances and big screen TVs. Use your gift card at BestBuy.com® or at any US Best Buy store.</p>
[website] => http://www.bestbuy.com
[merchant_code] => BES
[is_premium] => 1
[large_icon] => /cdn/merchants/230124/large_icon.png
[status] => 1
[get_gift_codes_from_root] => 0
[website_is_redemption_url] => 0
[cost_to_program] => 0
[toa_name] =>
)
)
所以很明显,无论我“选择”什么,它都会附加“商家”行。另外,请注意 [id] => 230124
来自“无处”,merchants
表中没有字段 id
。在medium_info
表中有一个id
字段,但它应该返回551678
而不是230124
,这是merchants
表中字段名称为account_holder_id
的商家ID。
编辑:只是想补充一点,当我在 phpMyAdmin 中运行它时,它不会附加 merchants
数据。
我仍在努力解决这个问题。如果您需要更多信息,我准备提供。这一定与我不理解的 Laravel DB::select 约定有关,因为它在 phpMyAdmin 中有效?任何帮助表示赞赏。
【问题讨论】:
【参考方案1】:试试这个伙伴,使用查询生成器:
\DB::table('postings')
->join('medium_info', 'postings.medium_info_id', '=', 'medium_info.id')
->join('accounts', 'postings.account_id', '=', 'accounts.id')
->join('merchants', 'merchants.account_holder_id', '=', 'accounts.account_holder_id')
->join('medium_types', 'medium_types.id', '=', 'accounts.medium_type_id')
->where('merchants.account_holder_id', 230124)
->where('medium_info.id', 551678)
->where('medium_types.id', 1)
->select('medium_info.*', 'postings.posting_timestamp')
->get();
【讨论】:
我已经通过做同样的事情解决了这个问题,它是工作伙伴!还是谢谢!【参考方案2】:这可能有助于检查您的数据库上实际运行的是哪个 SQL Laravel。
sn-p 波纹管可以让您看到这一点。
<?php
DB::listen(function ($query)
var_dump($query->sql);
);
Route::get('/', function ()
DB::select('SELECT * FROM users');
);
还要检查barryvdh/laravel-debugbar
扩展。
【讨论】:
$query 显示了我粘贴在问题中的完整查询。所以这对我来说仍然是个谜。以上是关于Laravel DB::select() 不返回“正确”数据的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Laravel 的 DB::Select() 中按变量添加 SQL 查询
如何在 DB::select(query) 上使用分页 - Laravel