Javed
Javed

Reputation: 857

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

When i update my table with data there is a two extra fields found which i am not added on my table.Let me show my code with detail.

Migration table inquiry_plan detail:

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');
    });
}

On my planController added a function for add activity:

Here is my controller function

 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);
    }
}

And finally saveOrupdate function in my model.

Here is my model function

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;
    }
}

When i submit my form it return error

"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))

what is the problem ?

Upvotes: 1

Views: 3594

Answers (3)

MohamedBenhida
MohamedBenhida

Reputation: 616

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

So you need to add this on your migration file $table->timestamps(); or if you don't want the timestamps at all add this to your Model File public $timestamps = false;

Upvotes: 0

Illia Yaremchuk
Illia Yaremchuk

Reputation: 2025

You may add this fields to migration file and restart migration:

$table->timestamps();

all code:

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');
    });

}

OR Simply place public $timestamps = false; in your model.

Upvotes: 3

Sohel0415
Sohel0415

Reputation: 9853

In your model Inquiryplan, add this line:

public $timestamps = false;

Upvotes: 0

Related Questions