laravel SQLSTATE[42S22]:未找到列:1054 未知列 created_at 和 updated_at 列丢失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46700757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:47:54  来源:igfitidea点击:

SQLSTATE[42S22]: Column not found: 1054 Unknown column created_at and updated_at column missing

laravel

提问by Josel Parayno

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?

我遇到类似 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))。但我的数据库中没有 updated_at 和 created_at 列,这些列怎么会出现在错误中?

BuildingRepository.php

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

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

<?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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Building extends Model
{
    protected $fillable = [
        'id', 'building_name', 'building_information', 'building_image', 
    ];
}

回答by Nitish Kumar

By default, Eloquent expects created_atand updated_atcolumns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestampsproperty on your model to false. Try using the same in your model Building.php:

默认情况下,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', 
    ];
}

回答by Charu Agarwal

Yes, by default Eloquent expects created_at& updated_atcolumns in all your tables. You can set $timestampsproperty to false.

是的,默认情况下,Eloquent 需要所有表中的created_at&updated_at列。您可以将$timestamps属性设置为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();

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

回答by Anukriti

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.

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