3

I'm encountering error like SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into buildings (building_name, updated_at, created_at) values (Building Four, 2017-10-12 02:56:13, 2017-10-12 02:56:13)). but i don't have a updated_at and created_at column in my database how come these columns shows up in the error?

BuildingRepository.php

 <?php namespace App\Repositories\Building; use App\Building; interface BuildingRepository { public function getById($id); public function getAll(); public function create(array $attributes); public function update($id, array $attributes); public function delete($id); } 

EloquentBuilding.php

<?php namespace App\Repositories\Building; use \App\Building; class EloquentBuilding implements BuildingRepository { private $model; public function __construct(Building $model) { $this->model = $model; } public function getById($id) { return $this->model->findOrFail($id); } public function getAll() { return $this->model->all(); } public function create(array $attributes) { return $this->model->fill($attributes)->save(); } public function update($id, array $attributes) { } public function delete($id) { } } 

BuildingController.php

<?php namespace App\Http\Controllers; use App\Building; use Illuminate\Http\Request; use App\Repositories\Building\BuildingRepository; class BuildingController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ private $building; public function __construct(BuildingRepository $building) { $this->building = $building; } public function createBuilding() { return view('building.create'); } public function store(Request $request) { $this->validate($request, array( 'building_name'=>'required', 'building_information'=>'required', )); $buildings = array('building_name' => $request->building_name, 'building_inforamtion' => $request->building_information); $this->building->create($buildings); } public function getAllBuilding() { $buildings = $this->building->getAll(); return view('building.show')->with('buildings', $buildings); } public function getSpecificRecord() { $buildings = $this->building->getById(1); return view('building.show')->with('buildings', $buildings); } } 

Building.php

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Building extends Model { protected $fillable = [ 'id', 'building_name', 'building_information', 'building_image', ]; } 
6
  • 1
    try public $timestamps = false; in your model Building.php Commented Oct 12, 2017 at 3:24
  • @NitishKumar thanks it works, now how can i make one of my columns nullable? Commented Oct 12, 2017 at 3:32
  • You can define the same in the migration. Commented Oct 12, 2017 at 3:34
  • yeah i define it then i use migrate:rollback then migrate it again, thanks again. Commented Oct 12, 2017 at 3:36
  • @NitishKumar im newbie here, i am wondering how did i manage to connect my buildingcontroller to the eloquentbuilding? Commented Oct 12, 2017 at 3:45

4 Answers 4

12

By default, Eloquent expects created_at and updated_at columns to exist in your tables. If you do not wish to have these columns to be automatically managed by Eloquent, then you need to set the $timestamps property in your model to false. Your file Building.php should look like:

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Building extends Model { public $timestamps = false; protected $fillable = [ 'id', 'building_name', 'building_information', 'building_image', ]; } 
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, by default Eloquent expects created_at & updated_at columns in all your tables. You can set $timestamps property to false.

To make a column nullable, you mention that in your migration. For example, I want to make address column of my table nullable, I do this.

$table->string('address')->nullable();

Comments

0

I faced the same issue. The problem was that the column name that I mentioned inside migration was not same as what I was using inside my controller, model and view. So, I would suggest that you check whether you have taken same column name inside the migration that you are further using for mass assignment.

Comments

0

Turn places created_at and updated_at from the database

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.