Eloquent doesn't recognize fillable fields when adding constructor to model


how

I have a model Categorylike this :

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 the correct data into the database, Eloquent doesn't recognize the model's fillable fields and then throws, Illuminate\Database\QueryExceptionbut when I remove the constructor from the category model, it works fine and Eloquent recognizes the fillable fields. What's causing this?

Marcin Nabiavik

Of course you should run the parent constructor, and using the default model signature is probably very reasonable, so your constructor should look like this:

public function __construct(array $attributes = [])
{
   parent::__construct($attributes);
   $this->category_cover_path = public_path() . '/upload/image/category/cover/';
   $this->category_logo_path = public_path() . '/upload/image/category/logo/';
}

Related


Eloquent doesn't work after overriding model constructor

Richard Widitz I'm trying to develop a web application with laravel 5.3 and I'm having issues that I can't solve so far. Here is the context. I got a simple model called Laravel that Sectionimplements a constructor like this; public function __construct($title

Eloquent doesn't work after overriding model constructor

Richard Widitz I'm trying to develop a web application with laravel 5.3 and I'm having issues that I can't solve so far. Here is the context. I got a simple model called Laravel that Sectionimplements a constructor like this; public function __construct($title

Laravel Eloquent doesn't recognize variables

Overlord Angel I'm trying to use laravel-eloquent to get rooms and their reservations after a certain date. $fdate='2015-01-05'; $roomdata=Rooms::where('city_id','=',$citydata['id']) ->with('types') ->with('localities') ->with('slideshow') ->with(array('bookin

Laravel Eloquent doesn't recognize variables

Overlord Angel I'm trying to use laravel-eloquent to get rooms and their reservations after a certain date. $fdate='2015-01-05'; $roomdata=Rooms::where('city_id','=',$citydata['id']) ->with('types') ->with('localities') ->with('slideshow') ->with(array('bookin

Eloquent model binding doesn't work when using Vendor package

Mula I am using the following package Spatie Laravel Mail Templates . I created a resource controller to CRUD the template and the following route declaration: Route::resource('mailtemplates', 'MailTemplateController'); If I access /mailtemplate/1it without l

Eloquent model binding doesn't work when using Vendor package

Mula I am using the following package Spatie Laravel Mail Templates . I created a resource controller to CRUD the template and the following route declaration: Route::resource('mailtemplates', 'MailTemplateController'); If I access /mailtemplate/1it without l

NSXPCInterface constructor doesn't recognize protocol in Swift

gbdavid I'm creating an XPC service in swift and the protocol is created: protocol MyProtocol { func myFunc() } When I try to set the interface implemented by the exported object (in main.swift) by initializing a new object of NSXPCInterface via the protoco

Why doesn't Monkeypatch recognize assignment in constructor?

Christianity I'm practicing TDD and I'm pretty new to this . In my test case I want to read from stdin and check if the output matches the input. For standard input, I want to use a mock object. So I monkeypatch.setattr('sys.stdin', StringIO(string))used to re

ProjectTo doesn't recognize parameterless constructor

Piper I have a class for fetching data from a database that looks like this (some fields are missing for simplicity): public class BranchDetailDto { public BranchDetailDto() { } public BranchDetailDto(int supplierId, string supplierName) {

NSXPCInterface constructor doesn't recognize protocol in Swift

gbdavid I'm creating an XPC service in swift and the protocol is created: protocol MyProtocol { func myFunc() } When I try to set the interface implemented by the exported object (in main.swift) by initializing a new object of NSXPCInterface via the protoco

FParsec doesn't recognize type's constructor

rfreytag The following top-level XML parser definition returns an error The value or constructor ‘TOP_LEVEL_RECORD’ is not defined.... let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL_RECORD, unit>() do xTop_Level_Ref := pipe4

NSXPCInterface constructor doesn't recognize protocol in Swift

gbdavid I'm creating an XPC service in swift and the protocol is created: protocol MyProtocol { func myFunc() } When I try to set the interface implemented by the exported object (in main.swift) by initializing a new object of NSXPCInterface via the protoco

Why doesn't Monkeypatch recognize assignment in constructor?

Christianity I'm practicing TDD and I'm pretty new to this . In my test case I want to read from stdin and check if the output matches the input. For standard input, I want to use a mock object. So I monkeypatch.setattr('sys.stdin', StringIO(string))used to re

FParsec doesn't recognize type's constructor

rfreytag The following top-level XML parser definition returns an error The value or constructor ‘TOP_LEVEL_RECORD’ is not defined.... let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL_RECORD, unit>() do xTop_Level_Ref := pipe4

ProjectTo doesn't recognize parameterless constructor

Piper I have a class for fetching data from a database, it looks like this (some fields are missing for simplicity): public class BranchDetailDto { public BranchDetailDto() { } public BranchDetailDto(int supplierId, string supplierName) {

Why doesn't Monkeypatch recognize assignment in constructor?

Christianity I'm practicing TDD and I'm pretty new to this . In my test case I want to read from stdin and check if the output matches the input. For standard input, I want to use a mock object. So I monkeypatch.setattr('sys.stdin', StringIO(string))used to re

FParsec doesn't recognize type's constructor

rfreytag The following top-level XML parser definition returns an error The value or constructor ‘TOP_LEVEL_RECORD’ is not defined.... let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL_RECORD, unit>() do xTop_Level_Ref := pipe4

NSXPCInterface constructor doesn't recognize protocol in Swift

gbdavid I'm creating an XPC service in swift and the protocol is created: protocol MyProtocol { func myFunc() } When I try to set the interface implemented by the exported object (in main.swift) by initializing a new object of NSXPCInterface via the protoco

NSXPCInterface constructor doesn't recognize protocol in Swift

gbdavid I'm creating an XPC service in swift and the protocol is created: protocol MyProtocol { func myFunc() } When I try to set the interface implemented by the exported object (in main.swift) by initializing a new object of NSXPCInterface via the protoco

ProjectTo doesn't recognize parameterless constructor

Piper I have a class for fetching data from a database, it looks like this (some fields are missing for simplicity): public class BranchDetailDto { public BranchDetailDto() { } public BranchDetailDto(int supplierId, string supplierName) {

Why doesn't Monkeypatch recognize assignment in constructor?

Christianity I'm practicing TDD and I'm pretty new to this . In my test case I want to read from stdin and check if the output matches the input. For standard input, I want to use a mock object. So I monkeypatch.setattr('sys.stdin', StringIO(string))used to re

Why doesn't Monkeypatch recognize assignment in constructor?

Christianity I'm practicing TDD and I'm pretty new to this . In my test case I want to read from stdin and check if the output matches the input. For standard input, I want to use a mock object. So I monkeypatch.setattr('sys.stdin', StringIO(string))used to re

FParsec doesn't recognize type's constructor

rfreytag The following top-level XML parser definition returns an error The value or constructor ‘TOP_LEVEL_RECORD’ is not defined.... let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL_RECORD, unit>() do xTop_Level_Ref := pipe4

FParsec doesn't recognize type's constructor

rfreytag The following top-level XML parser definition returns an error The value or constructor ‘TOP_LEVEL_RECORD’ is not defined.... let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL_RECORD, unit>() do xTop_Level_Ref := pipe4