本文介紹了Laravel,如何忽略訪問器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個帶有自定義訪問器的模型,因此我獲得了該自定義屬性,
I have a model with a custom accessor so I get that custom attribute,
class Order extends GSModel{
$appends = ['orderContents'];
public function getOrderContentsAttribute()
{
return $this->contents()->get();
}
}
但是現(xiàn)在,在一種情況下,我只需要獲取一些字段,而沒有這個 OrderContents
一個.
But now, in one case, I need to get only some fields, without this OrderContents
one.
$openOrders = Order::open()->has('contents')->get(['id','date','tableName']);
但是這樣做,它也會返回OrderContents
.有沒有辦法不得到那個字段?
But doing it this way, it returns me the OrderContents
as well..
is there a way to not get that field?
謝謝!
推薦答案
沒有辦法一次性完成,因此您需要:
There's no way to do it in one go, so here's what you need:
$openOrders = Order::open()->has('contents')->get(['id','date','tableName']);
$openOrders->each(function ($order) {
$order->setAppends([]);
});
或者,您可以在最后一步使用 Laravel 的高階消息傳遞:>
Alternatively, you may use Laravel's Higher Order Messaging on the last step:
$openOrders->each->setAppends([]);
這篇關于Laravel,如何忽略訪問器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!