laravel 如何获取除一个模型之外的所有模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24585304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel how to get all the models except one
提问by Anastasie Laurent
I have a restaurant
model and a waitingtime
model. the relationship is one to many, everything is working , I am able to update, edit and delete
我有一个restaurant
模型和一个waitingtime
模型。关系是一对多,一切正常,我可以更新,编辑和删除
I can get all the waitingtimes for a specific restaurant like this
我可以获得像这样的特定餐厅的所有等待时间
public function edit($id)
{
$res = Restaurant::find(4);
$allWaitingTimesForThisRestaurant = $res->waitingtimes()->get();
That gives me an array and I am happy with using it in my jquery when the page return.
这给了我一个数组,当页面返回时,我很高兴在我的 jquery 中使用它。
My question
我的问题
is there any way so I still able to get all the waitingtimes for a specific restuarant but without a specific waitingtime
有什么办法让我仍然能够获得特定餐厅的所有等待时间,但没有特定的等待时间
for example, the input to the edit function is $id
, and this $allWaitingTimesForThisRestaurant = $res->waitingtimes()->get();
returns an array of waitingtimes, so I need that array but without the waitingtime with the id=$id
例如,编辑函数的输入是$id
,这$allWaitingTimesForThisRestaurant = $res->waitingtimes()->get();
将返回一个等待时间数组,所以我需要该数组但没有等待时间id=$id
can you help me please?
你能帮我吗?
I feel that I didn't explain my problem well, if so kindly tell me to explain more.
我觉得我没有很好地解释我的问题,如果有的话请告诉我更多解释。
many tnanks
许多坦克
回答by Joel Hinz
Why not just use a regular where?
为什么不直接使用常规 where 呢?
$res = Restaurant::find(4);
$allWaitingTimesForThisRestaurant = $res->waitingtimes()->where('id', '!=', $res)->get();
Edit: Also, see Hailwood's comment below for a good addendum.
编辑:另外,请参阅下面的 Hailwood 评论以获得一个很好的附录。
回答by Patrick Reck
The easiest way using the least code, is by removing it after retrieving them all.
使用最少代码的最简单方法是在检索所有代码后将其删除。
public function edit($id)
{
$res = Restaurant::find(4);
$allWaitingTimesForThisRestaurant = $res->waitingtimes()->get();
foreach($allWaitingTimesForThisRestaurant as $restaurant) {
if ($restaurant->id == $id) unset($restaurant);
}
As far as I remember, you will have to write the SQL on your own if you don't want to remove it afterwards.
据我所知,如果您以后不想删除它,则必须自己编写 SQL。