gpt4 book ai didi

laravel - 在 Eloquent 中从 RAM 加载模型关系

转载 作者:行者123 更新时间:2023-12-02 03:21:51 25 4
gpt4 key购买 nike

当我们用它的关系加载一个 Laravel Eloquent 模型时,像这样:

$user = User::with('Interest')->find(1);

查询MySQL如下:

100 Prepare   select * from `users` where `users`.`id` = ? limit 1
100 Execute select * from `users` where `users`.`id` = '1' limit 1
100 Close stmt
100 Prepare select `interest_id`, `user_interests`.`user_id` as `pivot_user_id`, `user_interests`.`interest_id` as `pivot_interest_id` from `interests` inner join `user_interests` on `interests`.`id` = `user_interests`.`interest_id` where `user_interests`.`user_id` in (?)
100 Execute select `interest_id`, `user_interests`.`user_id` as `pivot_user_id`, `user_interests`.`interest_id` as `pivot_interest_id` from `interests` inner join `user_interests` on `interests`.`id` = `user_interests`.`interest_id` where `user_interests`.`user_id` in ('1')
100 Close stmt
100 Quit

在下一行,我想访问加载的 Interest 模型,在我看来,它已经被 ::with 语句预先加载了。但是,当我尝试像这样访问 interest_id 时:

$user->interest->interest_id;

我看到另一个数据库查找出现在 MySQL 查询日志中,访问相同的已经预加载的关系:

100 Prepare   select `interest_id`, `user_interests`.`user_id` as `pivot_user_id`, `user_interests`.`interest_id` as `pivot_interest_id` from `interests` inner join `user_interests` on `interests`.`id` = `user_interests`.`interest_id` where `user_interests`.`user_id` = ?
100 Execute select `interest_id`, `user_interests`.`user_id` as `pivot_user_id`, `user_interests`.`interest_id` as `pivot_interest_id` from `interests` inner join `user_interests` on `interests`.`id` = `user_interests`.`interest_id` where `user_interests`.`user_id` = '1'
100 Close stmt
100 Quit

我希望 Laravel 为我提供已经加载的关系,但它再次查询 MySQL。我看到当我使用 getRelation('Interest') 时,它返回已经加载到 User::with('Interest') 上的关系,而不是查询 MySQL再次,像这样:

$user->getRelation("Interest")->interest_id;

我想知道这是否是要走的路,我对使用 ::with 进行预加载的期望是否完全错误?或者也许还有其他最佳实践来访问预加载的关系而不是再次查询 MySQL。在我看来,多次查询数据库以获取相同信息的成本更高。

用户模型

<?php namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Models;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

use Authenticatable, CanResetPassword;

protected $table = 'users';

protected $fillable = ['name', 'email', 'password'];

protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at'];

public function Interest()
{
return $this->belongsToMany('App\Models\Interest', 'user_interests', 'user_id', 'interest_id')->select(array('interest_id'));
}

}

兴趣模型

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Interest extends Model
{
protected $table = 'interests';

protected $fillable = ['description', 'parent_id'];

protected $hidden = ['created_at', 'updated_at'];

}

最佳答案

你的代码是 $user->interest->interest_id; 你的函数是 public function Interest()

PHP 函数区分大小写。

关于laravel - 在 Eloquent 中从 RAM 加载模型关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com