我有以下模型:
<?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();
}
}
播種機模型:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
Review::create([
'page_uuid' => ReviewUUIDGenerator::generate(),
'order_id' => 10000
]);
}
}
移民:
<?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');
}
}
基本上,模型存盤給定作業的評論資訊(例如,我的作業是幫助人們制作一些檔案,在我的作業完成后,我要求我的客戶提交對我的作業的評論)問題是:當我設定$fillable = ['*'];我可以訪問模型屬性,如物件屬性,但是如果我不硬編碼所需的屬性,我無法創建新模型或用一些模型屬性填充模型$fillable是$fillable = ['page_available_untill', 'order_id', 'etc']它實際上是如何作業的,還是我不明白什么?
uj5u.com熱心網友回復:
protected $guarded = [];
替換受保護的 $fillable = ['*']; 通過受保護的 $guarded = [];
uj5u.com熱心網友回復:
protected $fillable = ['*'];
這個請在可填寫的地方輸入列名,例如
protected $fillable = ['page_uuid','order_id'];
在可填充中添加列名,讓我知道它是否有效
在 eloquent ORM 中,$fillable 屬性是一個陣列,其中包含可以使用批量賦值填充的所有表欄位。
批量賦值是指向模型發送一個陣列,直接在資料庫中創建一條新記錄。
請參閱此 https://laravel.com/docs/9.x/eloquent#mass-assignment
您不能在可填充方法中使用 *。您必須在可填充項中添加所有需要的列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510077.html
標籤:php拉拉维尔
