Laravel,获取关系行而不是id

发布于 2025-01-11 21:58:16 字数 1562 浏览 3 评论 0原文

我想获取作者行而不是author_id。我可以通过添加集合并一一更改来实现这一点,但是 Laravel 有这方面的功能吗?好吧,我想让这一行 我可以使用这样的

Book::where('id',$bid)->with('author')->first('author_id AS author'); //Changes only coulmn name :(

模型

public function author()
    {
        return $this->hasOne(Author::class,'id','author_id');
    }

查询

Book::where('id',$bid)->with('author')->first()

输出

  {
    "id": 1,
    "name": "Book 1",
    "author_id": 3,
    "category_id": 2,
    "level_id": 1,
    "book_language_id": 1,
    "book_length": 0,
    "img": "book1.png",
    "summary": "Summary 1",
    "rate_avg": "2.75",
    "created_at": "2022-03-04T18:46:32.000000Z",
    "updated_at": "2022-03-04T18:52:28.000000Z",
    "author": {
        "id": 3,
        "name": "Author 3",
        "created_at": "2022-03-04T18:46:32.000000Z",
        "updated_at": "2022-03-04T18:46:32.000000Z"
    }
}

想要

{
    "id": 1,
    "name": "Book 1",
    "author": {
        "id": 3,
        "name": "Author 3",
        "created_at": "2022-03-04T18:46:32.000000Z",
        "updated_at": "2022-03-04T18:46:32.000000Z"
    },
    "category_id": 2,
    "level_id": 1,
    "book_language_id": 1,
    "book_length": 0,
    "img": "book1.png",
    "summary": "Summary 1",
    "rate_avg": "2.75",
    "created_at": "2022-03-04T18:46:32.000000Z",
    "updated_at": "2022-03-04T18:52:28.000000Z",
    
}

I want to get author row instead of author_id. I could this with add collection and change one by one but has Laravel any function for this? Well, I want make this one line
Can i use something like this

Book::where('id',$bid)->with('author')->first('author_id AS author'); //Changes only coulmn name :(

model

public function author()
    {
        return $this->hasOne(Author::class,'id','author_id');
    }

query

Book::where('id',$bid)->with('author')->first()

Output

  {
    "id": 1,
    "name": "Book 1",
    "author_id": 3,
    "category_id": 2,
    "level_id": 1,
    "book_language_id": 1,
    "book_length": 0,
    "img": "book1.png",
    "summary": "Summary 1",
    "rate_avg": "2.75",
    "created_at": "2022-03-04T18:46:32.000000Z",
    "updated_at": "2022-03-04T18:52:28.000000Z",
    "author": {
        "id": 3,
        "name": "Author 3",
        "created_at": "2022-03-04T18:46:32.000000Z",
        "updated_at": "2022-03-04T18:46:32.000000Z"
    }
}

Want

{
    "id": 1,
    "name": "Book 1",
    "author": {
        "id": 3,
        "name": "Author 3",
        "created_at": "2022-03-04T18:46:32.000000Z",
        "updated_at": "2022-03-04T18:46:32.000000Z"
    },
    "category_id": 2,
    "level_id": 1,
    "book_language_id": 1,
    "book_length": 0,
    "img": "book1.png",
    "summary": "Summary 1",
    "rate_avg": "2.75",
    "created_at": "2022-03-04T18:46:32.000000Z",
    "updated_at": "2022-03-04T18:52:28.000000Z",
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏大泽ㄣ 2025-01-18 21:58:16

在您的查询中,

Book::where('id',$bid)->with('author')->first()

您将获得具有该 id 的书籍,并且您渴望加载作者关系,因此为了获取作者,您必须访问作者字段:

Book::where('id',$bid)->with('author')->first()->author

in your query

Book::where('id',$bid)->with('author')->first()

you are getting the book that has that id and you are eager loading the author relation, so in order to get the author you have to access the author field:

Book::where('id',$bid)->with('author')->first()->author
半岛未凉 2025-01-18 21:58:16

正如我所说,我可以这样收集:

$b=Book::where('id',$bid)->with('author')->first();

                    $book=collect([
                        'id'=>$b->id,
                        'name'=>$b->name,
                        'author'=>$b->author,
                        'category_id'=>$b->category_id,
                        'level_id'=>$b->level_id,
                        'book_language_id'=>$b->book_language_id,
                        'book_length'=>$b->book_length,
                        'summary'=>$b->summary,
                        'rate_avg'=>$b->rate_avg,
                    ]);

但这种方法似乎没有必要

As I said I can this with collection like this:

$b=Book::where('id',$bid)->with('author')->first();

                    $book=collect([
                        'id'=>$b->id,
                        'name'=>$b->name,
                        'author'=>$b->author,
                        'category_id'=>$b->category_id,
                        'level_id'=>$b->level_id,
                        'book_language_id'=>$b->book_language_id,
                        'book_length'=>$b->book_length,
                        'summary'=>$b->summary,
                        'rate_avg'=>$b->rate_avg,
                    ]);

But this method seems unnecessary

一张白纸 2025-01-18 21:58:16

在您的示例中,输出您想要的之间的唯一区别是“author_id”:3,已被删除。

因此,如果您不需要列或重命名列,则需要使用 ->select 并获取您想要的所有字段。你也可以用类似的东西重命名

-> select(DB::raw('author_id as auhtor'), 'books.*')

In your example the only difference between the output and what you want is "author_id": 3, as been deleted.

So if you don't want a column or rename a column, you need to use ->select and take all the field you want. And you can also rename with something like that

-> select(DB::raw('author_id as auhtor'), 'books.*')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文