Laravel 5

Erreur bizarre lors d'un search en ajax

  • Avatar de t0m
    Membre depuis :
    06/12/2018
    Messages :
    3

    Bonjour,

    je bloque sur une erreur, lors d'un search en ajax (laravel 5.7):

    Error 500

    Call to undefined method App\Models\Coregistration::getCategoryAttribute()

    J'ai essayé plusieurs choses qui n'ont pas fonctionné

    Voici le modèle Coregistration:


    /
    App\Models\Cospo

    @property-read \App\Models\Category $category
    @property-read \App\Models\Entity $entity
    @property-read \App\Models\Logo $logo
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration newModelQuery()
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration newQuery()
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration query()
    @mixin \Eloquent
    @property int $id
    @property string|null $int_name
    @property int $active
    @property string|null $title
    @property string $max_per_day
    @property string|null $date_start
    @property string|null $date_end
    @property int|null $testfile_id
    @property int|null $postfile_id
    @property string|null $url_privacy
    @property int|null $logo_id
    @property int $is_cospo
    @property int $is_coreg
    @property int|null $category_id
    @property int|null $entity_id
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereActive($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereCategoryId($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereDateEnd($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereDateStart($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereEntityId($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereId($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereIntName($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereIsCoreg($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereIsCospo($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereLogoId($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereMaxPerDay($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration wherePostfileId($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereTestfileId($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereTitle($value)
    @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coregistration whereUrlPrivacy($value)
    /
    class Coregistration extends Model
    {

    use SoftDeletes, Sortable;

    protected $table = 'coregistrations';
    protected $fillable = [
    "int_name",
    "active",
    "title",
    "max_per_day",
    "date_start",
    "date_end",
    "url_privacy",
    "logo_id",
    "is_coreg",
    "is_cospo",
    "category_id",
    "entity_id",
    "testfile_id",
    "postfile_id",
    ];

    public $sortable = ['id', 'active','int_name', 'category', 'entity','date_start', 'date_start'];

    protected $casts = [
    'active' => 'boolean',
    ];

    protected $hidden = [

    ];

    public $timestamps = false;

    protected $appends = [
    'category',
    'entity',
    'logo',
    'testfile',
    'postfile'
    ];

    /

    The attributes that should be mutated to dates.

    @var array
    /
    protected $dates = ['deleted_at'];

    /public function getCategory()
    {

    return $this->category();

    }
    /

    / public function getEntity()
    {

    return $this->entity();

    }
    /

    public function category()
    {
    return $this->belongsTo('App\Models\Category');
    }

    public function entity()
    {
    return $this->belongsTo('App\Models\Entity');
    }

    /public function category()
    {
    return $this->hasOne('App\Models\Category', 'id', 'category_id');
    }

    public function entity()
    {
    return $this->hasOne('App\Models\Entity','id', 'entity_id');
    }
    /

    public function logo()
    {
    return $this->hasOne('App\Models\Logo', 'id', 'logo_id');
    }

    public function testfile()
    {
    return $this->hasOne('App\Models\ScriptFile', 'id', 'testfile_id');
    }
    public function postfile()
    {
    return $this->hasOne('App\Models\ScriptFile', 'id', 'postfile_id');
    }

    /
    @return string
    /
    public function getLogoName()
    {
    return $this->logo->getName();
    }

    /

    @return string
    /
    public function getUrlPrivacy()
    {
    return $this->url_privacy();
    }

    /*
    @param Logo $logo
    @return $this
    /
    public function setLogo(Logo $logo)
    {
    $this->logo=$logo;

    return $this;
    }

    /
    @return array
    /
    public function getFillable()
    {
    return $this->fillable;
    }

    /

    @param array $fillable
    /
    public function setFillable($fillable)
    {
    $this->fillable = $fillable;
    }

    /public function isActive()
    {
    return $this->active;
    }
    /

    public function setActiveAttribute($active)
    {
    $this->attributes['active'] = $active;
    }

    public function __get($key)
    {
    return $this->getAttribute($key);
    }

    }

    le controlleur (CoregistrationController:

    /*
    @param CoregistrationRequest $request
    @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
    /
    public function search(CoregistrationRequest $request)
    {
    $input = Input::get('search-term');

    $coregistrations = Coregistration::where('int_name', 'LIKE', '%'.$input.'%')->first();

    /if ($request->ajax()) {
    return view('partials.search-cospo', ['coregistrations',$coregistrations]);
    }
    return view('partials.search-cospo', ['coregistrations',$coregistrations]);
    /

    return response()->json($coregistrations);

    }

    Auriez-vous une idée d'où pourrait venir le propblème ?

    Merci d'avance pour vos réponses

  • Avatar de jay4sem
    Membre depuis :
    25/04/2016
    Messages :
    44

    Salut,

    Je ne vois pas la methode "getCategoryAttribute()" dans le code de ton model...

  • Avatar de bestmomo
    Membre depuis :
    07/04/2013
    Messages :
    2487

    Salut,

    Comme category est dans le tableau $appends il devrait effectivement y avoir une méthode getCategoryAttribute() pour créer cet attribut.

  • Avatar de t0m
    Membre depuis :
    06/12/2018
    Messages :
    3

    Merci pour vos réponses,

    Pour les relations BelongsTo, si j'ai bien compris, il faut mettre les modèles liés dans la propriété with et pas appends, et du coup plus besoin d'un accesseur getCategoryAttribute()

    class Coregistration extends Model
    {

    use SoftDeletes, Sortable;

    protected $table = 'coregistrations';
    protected $fillable = [
    "int_name",
    "active",
    "title",
    "max_per_day",
    "date_start",
    "date_end",
    "url_privacy",
    "logo_id",
    "is_coreg",
    "is_cospo",
    "category_id",
    "entity_id",
    "testfile_id",
    "postfile_id",
    ];

    public $sortable = ['id', 'active', 'int_name', 'category', 'entity', 'date_start', 'date_start'];

    protected $casts = [
    'active' => 'boolean',
    ];

    protected $hidden = [

    ];

    public $timestamps = false;

    protected $with = [
    'category',
    'entity',
    ];

    protected $appends = [
    'logo',
    'testfile',
    'postfile'
    ];

    /
    The attributes that should be mutated to dates.

    @var array
    /
    protected $dates = ['deleted_at'];

    /public function getCategory()
    {

    return $this->category();

    }
    /

    / public function getEntity()
    {

    return $this->entity();

    }
    /

    public function category()
    {
    return $this->belongsTo('App\Models\Category');
    }

    public function entity()
    {
    return $this->belongsTo('App\Models\Entity');
    }

    /public function category()
    {
    return $this->hasOne('App\Models\Category', 'id', 'category_id');
    }

    public function entity()
    {
    return $this->hasOne('App\Models\Entity','id', 'entity_id');
    }
    /

    public function logo()
    {
    return $this->hasOne('App\Models\Logo', 'id', 'logo_id');
    }

    public function testfile()
    {
    return $this->hasOne('App\Models\ScriptFile', 'id', 'testfile_id');
    }

    public function postfile()
    {
    return $this->hasOne('App\Models\ScriptFile', 'id', 'postfile_id');
    }

    /

    @return string
    /
    public function getLogoName()
    {
    return $this->logo->getName();
    }

    /
    @return string
    /
    public function getUrlPrivacy()
    {
    return $this->url_privacy();
    }

    /

    @param Logo $logo
    @return $this
    */
    public function setLogo(Logo $logo)
    {
    $this->logo = $logo;

    return $this;
    }

    /
    @return array
    /
    public function getFillable()
    {
    return $this->fillable;
    }

    /

    @param array $fillable
    /
    public function setFillable($fillable)
    {
    $this->fillable = $fillable;
    }

    /public function isActive()
    {
    return $this->active;
    }
    /

    public function setActiveAttribute($active)
    {
    $this->attributes['active'] = $active;
    }

    public function __get($key)
    {
    return $this->getAttribute($key);
    }

    }

Vous ne pouvez pas répondre à ce sujet.