rentalhost / laravel-insight

IDEA plugin to works with Laravel Framework.
MIT License
27 stars 6 forks source link

Add support to the $fillable array in the eloquent classes. #31

Open phiter opened 6 years ago

phiter commented 6 years ago

The $fillable array defines what columns in a model can be filled trough mass assignment.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];
}

It'd allow us to use this function:

$flight = App\Flight::create(['name' => 'Flight 10']);

Or this one if the model is already defined:

$flight->fill(['name' => 'Flight 22']);

Could you add inspection for this array, and maybe typehint the expected type on the fill and create methods based on annotated column type?

Thanks!

rentalhost commented 6 years ago

Thanks. I have added to 1.0.0+ milestone, but it doesn't mean that will be a very future feature. Is just a control milestone.

rentalhost commented 6 years ago

It should works like:

$fillable = [
    'name',
    'age'
];

App\User::create([
    'name' => 'John',
    'age' => 18,
    'title' => 'Commander', 
        // Warn: property $title was not declared as $fillable.
        // Quick-fix: add $title to $fillable array.
]);
phiter commented 6 years ago

I'd add this too if possible/necessary:

/* @property int $age */

$fillable = [
    'name',
    'age'
];

App\User::create([
    'name' => 'John',
    'age' => "18", // Warning: expected type is integer, string given.
]);
rentalhost commented 6 years ago

Uhh... I don't know if it is possible, but I can try. Could you please create another issue for that? Just fill title and put a reference to your previous comment.