VertexBuffer's avatar

Laravel - Create an Empty Eloquent Model and send as JSON?

I'm using InertiaJS and I'm basing my project scaffold as a bit of a cross between the demo PingCRM app (https://github.com/inertiajs/pingcrm) and an example application Sebastian De Deyne gave at Laracon (https://github.com/sebastiandedeyne/laracon-eu-2019-inertia-demo-app/)

The issue I'm having is I'm trying to replicate this code here in my own project; https://github.com/sebastiandedeyne/laracon-eu-2019-inertia-demo-app/blob/master/app/Http/Controllers/PostsController.php#L42

But the issue is, when I try and perform this code myself such as; 'model' => new Model() it just returns an empty array, where as ideally I'd want it to return all fillable attributes on the model as just empty strings or null or something similar.

Inside his demo it works, so I'm not sure what could be causing this as I can't really seem to find any differences between our projects at the moment.

Hopefully someone can help.

0 likes
5 replies
bobbybouwmann's avatar

Mmh yeah, in general, this should work. Did you set the fillable fields on the model? Laravel needs some way to map these fields I guess.

VertexBuffer's avatar

@bobbybouwmann Yeah this is my model file;

class News extends Model
{
    use SearchableTrait, Filterable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'body',
    ];
    /**
     * Searchable rules.
     *
     * @var array
     */
    protected $searchable = [
        'columns' => [
            'title' => 10
        ],
    ];

    /**
     * Returns the associated User.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
bobbybouwmann's avatar

I don't know what else it could be. It doesn't seem that Sebastian is doing something else magically here.

VertexBuffer's avatar

@bobbybouwmann Yeah it's strange. It's definitely a Laravel thing and not Inertia interfering as even if I dd the model like so;

        $news = new News();
        dd($news);

I get the ouput;

News {#774 ▼
  #fillable: array:2 [▶]
  #searchable: array:1 [▶]
  #keyType: "string"
  #keyIsUuid: true
  #uuidVersion: 4
  +incrementing: false
  #guarded: []
  #casts: array:3 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #search_bindings: []
  #filtered: []
}

and attributes is empty.

Could it be that I'm using API Resources and that is interfering? Do I instead need to instantiate a new resource or something like that?

bobbybouwmann's avatar

Aah yeah, if you use an extra API resource, you need to specify all fields as well.

Please or to participate in this conversation.