亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel getNameAttribute 不適用于搜索

Laravel getNameAttribute 不適用于搜索

PHP
明月笑刀無情 2023-03-04 14:50:17
我有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 函數接受列列表。

同樣適用于其他功能


查看完整回答
反對 回復 2023-03-04
  • 1 回答
  • 0 關注
  • 234 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號