Laravel 关联查询只获取管理对象的部分数据

PHP中文网
Release: 2023-02-28 11:32:01
Original
2439 people have browsed it

$user = UserAttribute::where('uid', $uid)->with([
            'attribute' => function ($query) {$query->pluck('title');},
        ])->get();
Copy after login

上边这段代码可以执行,但是仍然把关联模型attribute里所有的字段都查询出来了,并不是想要的只查询title

回复内容:

       $user = UserAttribute::where('uid', $uid)->with([
            'attribute' => function ($query) {$query->pluck('title');},
        ])->get();
Copy after login

上边这段代码可以执行,但是仍然把关联模型attribute里所有的字段都查询出来了,并不是想要的只查询title

可以在Model里select,但是id不能少,不然没有关联条件。

public function attribute() {
        return $this->hasMany('Attribute', 'attribute_id')->select('id','title');
    }
Copy after login

相关文章:

关于Laravel多个条件的关联查询问题 ?

laravel 关联查询问题

laravel 关联查询文章和文章作者

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!