laravel 数据库状态[42S22]:未找到列:1054未知列created_at和updated_at列丢失

gstyhher  于 2023-02-14  发布在  其他
关注(0)|答案(4)|浏览(179)

我遇到类似SQLSTATE [42S22]的错误:未找到列:1054 "字段列表"中的未知列"updated_at"(SQL:插入buildingsbuilding_nameupdated_atcreated_at)值(Building Four,2017 - 10 - 12 02:56:13,2017 - 10 - 12 02: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', 
    ];
}
unguejic

unguejic1#

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

dced5bon2#

是的,默认情况下Eloquent要求所有表中有created_atupdated_at列。您可以将$timestamps属性设置为false
要使一个列可以为空,你要在迁移中提到这一点。例如,我想使我的表的地址列可以为空,我这样做。
$table-〉字符串(“地址”)-〉可空();

4ngedf3f

4ngedf3f3#

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

ecbunoof

ecbunoof4#

从数据库中翻转位置created_atupdated_at

相关问题