简体   繁体   English

Laravel:如何从查询结果中获取模型?

[英]Laravel: How to get Models out of query results?

Suppose I have a query that, among other things, returns user id's, this query was built using DB::table()... rather than using the models, so, as a result, I got a collection with arrays for each retrieved row, something like this:假设我有一个查询,除其他外,返回用户 ID,这个查询是使用DB::table()...构建的DB::table()...而不是使用模型,因此,结果,我得到了一个包含数组的集合行,是这样的:

user_id | calculated_data
--------+----------------
      1 |     123
      2 |     111
      3 |     222
    ... |     ...

Supose I store this collection on a $data variable, of course if I do a foreach ($data as $d) { $d->user_id ... } will work.假设我将此集合存储在$data变量上,当然,如果我执行foreach ($data as $d) { $d->user_id ... }将起作用。

But I want this query to return something more like what the ORM does, so instead user_id s, return User models so I can do, for example, a $data->user->name但我希望这个查询返回更像 ORM 所做的事情,所以代替user_id s,返回 User 模型,这样我就可以做,例如, $data->user->name

Can this be even done?这甚至可以做到吗? if so, how?如果是这样,如何?

You can use the hydrate() function, it accepts an array of stdClass objects (or even associative arrays AFAIR) as input and returns a collection of Eloquent Models , so you can do things like this:您可以使用hydrate()函数,它接受一个stdClass对象array (甚至关联数组 AFAIR)作为输入并返回 Eloquent Eloquent Modelscollection ,因此您可以执行以下操作:

$result = DB::table('users')->take(10)->get();
$users = App\User::hydrate($result->all()); 

You can even get a collection of Eloquent Models directly from a RAW query with the fromQuery() function, ie:您甚至可以使用fromQuery()函数直接从原始查询中获取fromQuery() Eloquent Models的集合,即:

$users = App\User::fromQuery('SELECT * FROM users WHERE id > ?', [2])

Update: If in your collection you don't have all the fields to hydrate a model, you can preload all the users you need with one query and modify your collection , ie:更新:如果在您的集合中没有所有字段来对模型进行hydrate ,您可以使用一个查询预加载您需要的所有用户并修改您的collection ,即:

$users = App\User::find($data->pluck('user_id'));
$data->transform(function($item) use($users) {
            $item->user = $users->where('id', $item->user_id)->first()
            return $item;
        }); 

You need to use Eloquent to call value the "ORM way" I did not find anything related to the query builder in relation to ORM.您需要使用 Eloquent 以“ORM 方式”调用 value 我没有找到与 ORM 相关的查询构建器的任何相关内容。

You could do something like this:你可以这样做:

$flights = App\Flight::all(); 

foreach ($flights as $flight) {
    echo $flight->name;
}

And in the ORM way you would get the user like this:在 ORM 方式中,您将获得这样的用户:

foreach ($flights as $flight) {
    echo $flight->user->name;
}

Of course you would need to setup the correct relations.当然,您需要设置正确的关系。

// initial query will return collection of objects
$query = DB::table('user_something')
   ->join('users', 'users.id', 'user_something.user_id');

// You could cast it to the model query, by using fromSub.
// Make sure to alias a subquery same as the model's name,
// otherwise it would not be able to parse models data
User::fromSub($query, 'users');

// The most robust way is to get table name from the model
User::fromSub($query, User::make()->getTable());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM