gpt4 book ai didi

Laravel raw query and including model relations(Laravel原始查询和包含模型关系)

转载 作者:bug小助手 更新时间:2023-10-28 20:41:49 27 4
gpt4 key购买 nike



I am trying to return a single post with a sub-query to include a count of down-votes and up-votes. I am able to achieve this by using a raw query.

我正在尝试返回一个带有子查询的帖子,以包括倒计票和倒计票。我能够通过使用原始查询来实现这一点。



However doing so means that I can no longer access the post models relations e.g. comments.

然而,这样做意味着我不能再访问帖子模型关系,例如评论。



Is there more of an Eloquent way I can do this to keep the models relations?

有没有更有说服力的方式来保持模特关系?



Or is there a better way I could be going about the whole thing? Would love to hear how you would go about it.

或者有没有更好的办法让我来处理这整件事?我很想听听你会怎么做。



$post = DB::select(
"SELECT post.*,
(SELECT COUNT(*) FROM posts_votes
WHERE type = 1
AND post_id = posts.id)
AS up_votes,
(SELECT COUNT(*) FROM posts_votes
WHERE type = 2
AND post_id = posts.id)
AS down_votes
FROM posts
INNER JOIN posts_votes ON posts.id = posts_votes.post_id
WHERE posts.id = ?", [$id])[0];

更多回答

use Posts::setect with ->where(function($subquery) {})

使用POSTS::setect with->where(Function($subQuery){})

you can use raw with Eloquent model like post::raw("sql query")->with("relation")

您可以将RAW与雄辩模型一起使用,如POST::RAW(“SQL Query”)->With(“Relation”)

优秀答案推荐

Try this

尝尝这个



Post::select('*')
->addSelect(DB::raw('
(select count(*)
from posts_votes
where type = 1
and post_id = posts.id)
as up_votes
'))
->addSelect(DB::raw('
(select count(*)
from posts_votes
where type = 2
and post_id = posts.id)
as down_votes
'))
->whereIn('id', $ids)
->get();


This is an old question, but it's the first result I get when I google "laravel relation raw query", so it might be useful for other people that are searching the same thing.

这是一个老生常谈的问题,但这是我在谷歌上搜索到的第一个结果,所以它可能对其他正在搜索相同内容的人很有用。



I managed to solve it like this:

我设法这样解决了这个问题:



$query = ""; //raw query goes here
$result = Model::fromQuery($query);
$result->load('relationship');


Make sure your custom raw query contains the proper columns specified in the model, otherwise the relationship will always return null.

确保您的定制原始查询包含模型中指定的正确列,否则关系将始终返回NULL。



Tested on Laravel 6.17.0

在Laravel 6.17.0上测试



This is what worked great for me:

这就是对我很有效的方法:


Model::where('condition_one', 1)->doesntHave('RelationShip')->get();

更多回答

This worked well for me. As @DivyankMunjapara commented, I could have just added ->with('relation) to the end of my query. However I find this method to be more Eloquent like. Only thing I had to change in my case was whereIn() to where as I am only querying one post.

这对我来说很管用。正如@DivyankMunjpara评论的那样,我本可以在我的查询的末尾添加->with(‘relationship)。然而,我发现这个方法更有说服力。在我的例子中,我唯一需要更改的是where()到where,因为我只查询了一个帖子。

Ive got one for you (maybe I should open another question?). Using the above example, how would I go about applying the posts_votes count to the $posts->children() ?

我有一个问题要问你(也许我应该再问一个问题?)使用上面的例子,我将如何将POSTS_VORTS计数应用于$POSTS->子函数()呢?

@patskot u could just add all the selects to the children. $posts->children()->select($all) ->addSelect($upvotesquery) ->addSelect($downvotequery)->get(). In this case I would create a scope on your Post model. scopeUpDownVotes() for example

@patskot u可以将所有选择添加到子项。$POSTS->CHILD()->SELECT($ALL)->addSelect($upvotesQuery)->addSelect($down voteQuery)->Get()。在这种情况下,我将在您的Post模型上创建一个作用域。Scope eUpDownVotes(),例如

Creating a reusable scope for the model worked wonders! Thanks for the help :)

为模型创建一个可重用的作用域成功了!感谢您的帮助:)

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