Laravel:在我的 table 上添加数据时发现 "updated_at" 和 "created_at" 字段

Laravel : An "updated_at" and "created_at" field found when adding data on my table

当我用数据更新我的 table 时,发现了两个额外的字段,我没有添加到我的 table.Let 我显示我的代码详细信息。

迁移tableinquiry_plan详细信息:

public function up()
{
   Schema::create('inquiry_plan', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('inquiry_id');
        $table->string('title_vacation');
        $table->string('email');
        $table->string('phone_no', 20)->nullable();
        $table->dateTime('start_date')->nullable();
        $table->dateTime('end_date')->nullable();
        $table->foreign('inquiry_id')->references('id')->on('inquiry_master');
    });
}

在我的 planController 上添加了一个函数用于添加 activity:

这是我的控制器函数

 public function addactivity(Request $request) {
    try {
        $validator = Validator::make($request->all(), Inquiryplan::planRules(), Inquiryplan::PlanMessages());
        $plandetails = Inquiryplan::SaveOrUpdate($request);
        if($plandetails !== false) {
            return redirect()->route('plan')->with('success', trans('Plan details added successfully.!!'));
        } else {
            return back()->with('error', "Unable to save plan details.!!")->withInput();
        }
    } catch (\Exception $ex) {
        dd($ex);
        return back()->with('error', "Unable to save plan details.!!")->withInput()->withErrors($validator);
    }
}

最后在我的模型中使用 saveOrupdate 函数。

这是我的模型函数

public static function SaveOrUpdate(Request $request) {
    try {
        $id = $request->get('id', false);
        $plandetails = false;
        DB::transaction(function () use ($request, &$plandetails, $id) {
            $plandetails = $id ? Inquiryplan::findOrFail($id) : new Inquiryplan();
            $plandetails->fill($request->all());
            try {
                $plandetails->save();
            } catch (\Exception $ex) {
                throw $ex;
            }
            dd($ex);
        });
        return $plandetails;
    } catch (\Exception $ex) {
        throw $ex;
    }
}

当我提交表单时 return 错误

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into inquiry_plan (inquiry_id, title_vacation, phone_no, start_date, end_date, updated_at, created_at) values (0, test title, 9974031835, 2018-02-06, 2018-02-14, 2018-02-06 10:44:45, 2018-02-06 10:44:45))

有什么问题?

在您的模型 Inquiryplan 中,添加此行:

public $timestamps = false;

您可以将此字段添加到迁移文件并重新开始迁移:

$table->timestamps();

所有代码:

public function up()
{
   Schema::create('inquiry_plan', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('inquiry_id');
        $table->string('title_vacation');
        $table->string('email');
        $table->string('phone_no', 20)->nullable();
        $table->dateTime('start_date')->nullable();
        $table->dateTime('end_date')->nullable();
        $table->timestamps();
        $table->foreign('inquiry_id')->references('id')->on('inquiry_master');
    });

}

或只需将 public $timestamps = false; 放入您的模型中。

By default, Eloquent expects created_at and updated_at columns to exist on your tables.

所以你需要在你的迁移文件中添加这个 $table->timestamps(); 或者如果您根本不需要时间戳,请将其添加到您的模型文件 public $timestamps = false;