skip to Main Content

I have the following model:

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Review extends Model
{
    protected $fillable = ['*'];
    public $dates = ['page_available_untill'];

    public static function findByUUID(string $uuid): self|null
    {
        return self::where('page_uuid', $uuid)->get()->first();
    }
}

Model seeder:

<?php

use IlluminateDatabaseSeeder;


class DatabaseSeeder extends Seeder
{
    public function run()
    {
        Review::create([
            'page_uuid' => ReviewUUIDGenerator::generate(),
            'order_id' => 10000
        ]);
    }
}

Migration:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateReviewsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('reviews', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->integer('order_id');
            $table->string('page_uuid');
            $table->dateTime('page_available_untill')->nullable();

            $table->integer('operator_speed')->nullable();
            $table->integer('operator_quality')->nullable();
            $table->integer('operator_politeness')->nullable();

            $table->integer('master_arrival_speed')->nullable();
            $table->integer('master_work_quality')->nullable();
            $table->integer('master_politeness')->nullable();

            $table->enum('materials_quality', ['Хорошее', 'Плохое', 'Не устанавливали'])->nullable();
            $table->enum('would_recommend', ['Да', 'Нет', 'Затрудняюсь ответить'])->nullable();

            $table->double('payment_summ', 9, 2)->nullable();
            $table->text('comment')->nullable();

            $table->json('photos')->default(json_encode([]));
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('reviews');
    }
}

Basically the model stores a review info for a given job (let’s say for example my job is to help people make some documents and after my job is done i ask my clients to submit a review to my job)
The problem is:
when i set $fillable = ['*']; i can access model attributes like an object properties, BUT i cant create a new model or fill model with some model attributes if i don’t hard code needed properties to $fillable like $fillable = ['page_available_untill', 'order_id', 'etc'] is that how it acually works or i don’t understand something?

2

Answers


  1.  protected $fillable = ['*'];
    

    this please enter column name in fillable, for example

    protected $fillable = ['page_uuid','order_id'];
    

    Add column name in fillable and let me know if its working or not

    In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment.

    Mass assignment refers to sending an array to the model to directly create a new record in Database.

    Refer this
    https://laravel.com/docs/9.x/eloquent#mass-assignment

    You can’t use * in fillable method. You have to add all needed column in the fillable.

    Login or Signup to reply.
  2. protected $guarded = [];
    

    Replace protected $fillable = [‘*’]; by protected $guarded = [];

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search