SQLSTATE [42S22]:找不到列:1054缺少未知列created_at和updated_at列

时间:2017-10-12 03:05:43

标签: laravel

我遇到错误,如SQLSTATE [42S22]:未找到列:1054未知列' updated_at'在'字段列表' (SQL:插入buildingsbuilding_nameupdated_atcreated_at)值(Building Four,2017-10-12 02:56:13,2017-10-12 2点56分13秒))。但我的数据库中没有updated_at和created_at列,这些列如何显示在错误中?

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', 
    ];
}

4 个答案:

答案 0 :(得分:3)

默认情况下,Eloquent希望您的表中存在created_atupdated_at列。如果您不希望Eloquent自动管理这些列,请将模型上的$timestamps属性设置为false。尝试在模型Building.php中使用相同的内容:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Building extends Model
{
    public $timestamps = false;

    protected $fillable = [
        'id', 'building_name', 'building_information', 'building_image', 
    ];
}

答案 1 :(得分:0)

是的,默认情况下,Eloquent希望created_at&amp;所有表格中的updated_at列。您可以将$timestamps属性设置为false

要使列可为空,请在迁移中提及。例如,我想让我的表的地址列可以为空,我这样做。

  

$表 - &GT;字符串(&#39;地址&#39;) - &GT;可为空的();

答案 2 :(得分:0)

我遇到了同样的问题。 问题是我在迁移中提到的列名与我在控制器,模型和视图中使用的列名不同。 因此,我建议您检查是否在要进一步用于批量分配的迁移中使用了相同的列名。

答案 3 :(得分:0)

关闭数据库中的created_at和updated_at位置

enter image description here