softya
softya

Reputation: 260

Laravel getNameAttribute not working with search

I have Account model, and inside I have this method:

public function getNameAttribute()
{
    if(\App::getLocale() == 'en')
        return $this->attributes['foreign_name'];
    else
        return $this->attributes['name'];
}

In the index page it's working. If the locale is en the name will be the foreign_name, like this picture:

enter image description here

But I have search method, and this is the code:

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']];       
}

If I try to search about anything I get this error:

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: 465
type: "->"
2: {,…}
class: "Illuminate\Database\Eloquent\Model"
file: "C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php"
function: "mutateAttribute"
line: 479
type: "->"

But if I change:

return $this->attributes['foreign_name'];

to

return $this->attributes['id']; // or any integer field in the accounts table 

It's working without problem.

With Laravel 6 it was working like that with me but now I get this error. Any help here?

This is the full model code:

namespace App\Models;
use Helper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Auth;
class Account extends Model
{
    use Notifiable,SoftDeletes;
    protected $table = 'accounts';
    protected $fillable = 
    [
        'number',
        'name',
        'foreign_name',
        'main_account_id',
        'user_id',
        'account_state_id',
        'note',
        'created_by','deleted_by'
    ];
    public function get_created_by()
    {
        return $this->belongsTo('App\Models\User','id','created_by');
    }
    public function get_deleted_by()
    {
        return $this->belongsTo('App\Models\User','id','deleted_by');
    }
    public function get_user_id()
    {
        return $this->hasOne('App\Models\User','id','user_id');
    }
    public static function search_scope($keyword)
    {
        $keyword = Helper::clean_keyword($keyword);
        $data = Account::where
        ('number','like','%'.$keyword.'%')->
        orWhere('name','like','%'.$keyword.'%')->
        limit(30)->
        get(['id','number','name']);
        if(count($data) == 0)
            return Helper::no_result();
        return [$data,['number','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']];       
    }
    public function getNameAttribute()
    {
        if(\App::getLocale() == 'en')
            return $this->attributes['foreign_name'];
        else
            return $this->attributes['name'];
    }
}

this is the search and select html code

<search_and_select>
<div slot='search_and_select' slot-scope="{change_keyword,results,no_result,chose_data,keyword,hidden_id,chose_first_value}">
    <div class="container" style='width:40vw;'>
            <input class="form-control {{$search_and_select}}" type="text" @keyup='change_keyword($event.target.value,"{{$type}}")' @keyup.enter='chose_first_value()' v-model='keyword' :run="!keyword ? keyword = '{{$value}}' : true" {{$required ?? null}}>
            <input type='hidden' v-model='hidden_id' name='{{$search_and_select}}' :run="!hidden_id ? hidden_id = '{{$hidden_id}}' : true"/>
        <ul v-if='no_result' class="list-group">
            <li class="list-group-item">{{trans('language.no_result')}}</li>
        </ul>
        <ul v-if='results' class="list-group">
            <li v-for='result in results'><input class='results' @click='chose_data(result.id,result.number,result.name)' @keyup.enter='chose_data(result.id,result.number,result.name)'  type='text' :value='result.number+" "+result.name' /></li>
        </ul>
    </div>
</div>

and this is the include file

@include(
    'treats.create_search_and_select',
    [
        'search_and_select' => $search_and_selects[$key],
        'value' => $search_and_selects_values[$key],
        'hidden_id' => $search_and_selects_hidden_ids[$key],
        'type' => $search_and_selects_type[$key],
        'required' => $search_and_selects_requireds[$key],
        'language' => $search_and_selects_languages[$key],
        'hint' => $search_and_selects_hints[$key],
        'policy' => $search_and_selects_policies[$key],
        'show_type' => $search_and_selects_show_types[$key],
        'rank' => $search_and_selects_ranks[$key],
    ]
)

and this is the console errors enter image description here enter image description here and this is how i call search_and_select

public function search_and_select(Request $request)
{
    if(!Gate::allows($request['model'].'.view'))
        return Helper::not_auth();
    $model = Model::where('name',$request['model'])->firstOrFail();
    $model_name = '\App\Models\\'.$model['model'];
    $data = $model_name::search_and_select($request['keyword']);
    return $data;
}

Upvotes: 1

Views: 1245

Answers (1)

Daniel Protopopov
Daniel Protopopov

Reputation: 7236

This is because of your line

get(['user_id','number','name'])

Which asks to fetch only these columns. You need to add foreign_name to this array and make it

get(['user_id','number','name', 'foreign_name'])

According to Laravel reference, get function accepts a list of columns.

Same applies at other functions

Upvotes: 1

Related Questions