在Eloquent

时间:2015-09-29 13:52:36

标签: laravel eloquent

当我们用它的关系加载一个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'];

}    

1 个答案:

答案 0 :(得分:1)

您的代码为$user->interest->interest_id;,您的功能为public function Interest()

PHP函数区分大小写。