我有Account模型,里面有這個方法:public function getNameAttribute(){ if(\App::getLocale() == 'en') return $this->attributes['foreign_name']; else return $this->attributes['name'];}在索引頁面中它正在工作。如果 locale 是enname 將是foreign_name,如下圖:但是我有搜索方法,這是代碼:public static function search_and_select($keyword){ $keyword = Helper::clean_keyword($keyword); $data = Account::where ('number','like','%'.$keyword.'%')-> orWhere('name','like','%'.$keyword.'%')-> limit(30)-> get(['user_id','number','name']); if(count($data) == 0) return Helper::no_result(); return [$data,['number','name']]; }如果我嘗試搜索任何內容,我會收到此錯誤:message: "Undefined index: foreign_name" 1: {,…}class: "App\Models\Account"file: "C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php"function: "getNameAttribute"line: 465type: "->"2: {,…}class: "Illuminate\Database\Eloquent\Model"file: "C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php"function: "mutateAttribute"line: 479type: "->"但如果我改變:return $this->attributes['foreign_name'];到return $this->attributes['id']; // or any integer field in the accounts table 它工作沒有問題。在 Laravel 6 中,它和我一樣工作,但現在我得到了這個錯誤。這里有什么幫助嗎?
1 回答

翻閱古今
TA貢獻1780條經驗 獲得超5個贊
這是因為你的線
get(['user_id','number','name'])
哪個要求只獲取這些列。您需要將 foreign_name 添加到此數組并使其成為
get(['user_id','number','name',?'foreign_name'])
根據 Laravel參考,get 函數接受列列表。
同樣適用于其他功能
- 1 回答
- 0 關注
- 234 瀏覽
添加回答
舉報
0/150
提交
取消