Eloquent将int作为字符串

时间:2017-09-22 19:33:02

标签: php mysql laravel laravel-5 eloquent

因此,我遇到以下两种模式的情况:

<?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创建此关系/连接查询?

0 个答案:

没有答案