因此,我遇到以下两种模式的情况:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Landing;
class Network
{
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function landings()
{
return $this->belongsToMany(Landing::class);
}
}
和
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Network;
class Landing
{
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function networks()
{
return $this->belongsToMany(Network::class);
}
}
所以,当我称之为关系:
<?php
$landing = Landing::find(16);
$networks = $landing->networks()->get();
Eloquent生成以下SQL:
SELECT *
FROM `landings`
INNER JOIN `landing_network` ON `landings`.`id` = `landing_network`.`landing_id`
WHERE `landing_network`.`network_id` = '16' AND `landings`.`deleted_at` IS NULL;
正如您所看到的,landing_network.network_id
代码段是作为字符串生成的,但由于转换而在MySQL上存在性能问题。
如何在不将整数转换为字符串的情况下使Laravel / Eloquent创建此关系/连接查询?