dimanche 4 juin 2017

Eloquent does not recognize fillable fields when adding constructor to model

I have model Category as below:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    /**
     * @var $fillable
     */
    protected $fillable = array('title', 'slug' , 'descriptoin', 'cover', 'logo', 'parent_id');

    /**
     * @var $category_logo_pathp
     */
    protected $category_logo_path ;

    /**
     * @var $category_cover_path
     */
    protected $category_cover_path ;

    /**
     * constructor
     */
    function __construct()
    {
        $this->category_cover_path = public_path() . '/upload/image/category/cover/';
        $this->category_logo_path = public_path() . '/upload/image/category/logo/';
    }

    /**
     * relation with itself
     */
    public function parent()
    {
        return self::where('id', $this->parent_id)->first();
    }

    public function coverPath()
    {
        return $this->category_cover_path;
    }

    public function logoPath()
    {
        return $this->category_logo_path;
    }
}

When inserting a category instance with correct data into database , Eloquent does not recognize fillable fields of the model and then throws Illuminate\Database\QueryException but when i remove the constructor from category model , it works fine and fillable fields are recognized using Eloquent. what is causing this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire