Home > Mobile >  Laravel model create() and fill() methods doesn't work
Laravel model create() and fill() methods doesn't work

Time:09-29

I have the following model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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 Illuminate\Database\Seeder;


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

Migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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?

CodePudding user response:

 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.

CodePudding user response:

protected $guarded = [];

Replace protected $fillable = ['*']; by protected $guarded = [];

  • Related