Skip to content

Instantly share code, notes, and snippets.

@tiagofrancafernandes
Last active August 11, 2022 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagofrancafernandes/bf2ec8bf11fb00d64beda6f7e24c03b7 to your computer and use it in GitHub Desktop.
Save tiagofrancafernandes/bf2ec8bf11fb00d64beda6f7e24c03b7 to your computer and use it in GitHub Desktop.
Getting the Laravel/Eloquent model fillable list
<?php
namespace App\Traits;
trait ModelFillableList
{
/**
* function getAllFillable
*
* @return ?array
*/
public static function getAllFillable(): ?array
{
return (new static())->getFillable();
}
}
<?php
namespace App\Models;
use App\Traits\ModelFillableList;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
use HasFactory;
use ModelFillableList;
}
<?php
// ...
public function store(Request $request)
{
// ...
$data = $request->only(MyModel::getAllFillable());
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment