Last active
August 11, 2022 19:51
-
-
Save tiagofrancafernandes/bf2ec8bf11fb00d64beda6f7e24c03b7 to your computer and use it in GitHub Desktop.
Getting the Laravel/Eloquent model fillable list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
trait ModelFillableList | |
{ | |
/** | |
* function getAllFillable | |
* | |
* @return ?array | |
*/ | |
public static function getAllFillable(): ?array | |
{ | |
return (new static())->getFillable(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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