向Mysql中插入有关查看器Laravel的数据
我有一些问题 我需要一张有访客资料的桌子。我有这个模式:向Mysql中插入有关查看器Laravel的数据,mysql,laravel,Mysql,Laravel,我有一些问题 我需要一张有访客资料的桌子。我有这个模式: public function up() { Schema::create('viewers', function (Blueprint $table) { $table->increments('id')->primary('id'); $table->string('ip_address', 45)->nullable();
public function up()
{
Schema::create('viewers', function (Blueprint $table) {
$table->increments('id')->primary('id');
$table->string('ip_address', 45)->nullable();
$table->string('country')->nullable();
$table->string('city')->nullable();
$table->text('device')->nullable();
$table->timestamp('last_activity')->useCurrent();
$table->rememberToken();
});
}
这是我页面中的控制器
public function index(Request $request)
{
$viewer = New Viewers();
$data = \Stevebauman\Location\Facades\Location::get('83.143.245.162');
$viewer->ip_address = $request->ip();
$viewer->country = $data->countryName;
$viewer->city = $data->cityName;
$viewer->device = $request->userAgent();
$viewer->save();
}
那是我的错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into `viewers` (`ip_address`, `country`, `city`, `device`, `updated_at`, `created_at`) values (::1, Germany, Frankfurt am Main, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15, 2020-11-26 10:11:06, 2020-11-26 10:11:06))
为什么
updated_at,created_at
尝试插入到表中???默认情况下,laravel上的所有模型都已更新_at并创建了_at。如果您的表没有它,您应该通过添加public$timestamps=false来禁用它模型上的代码>
class Viewers extends Model
{
public $timestamps = false;
这是默认的Laravel功能,Laravel会尝试自动填写created_at/updated_at,但找不到它们
如果您不想在查看器
模型中使用这两列,请添加以下行
public $timestamps = FALSE;
迁移过程中缺少$table->timestamps()
public function up()
{
架构::创建('viewers',函数(Blueprint$表){
$table->increments('id')->primary('id');
$table->string('ip_address',45)->nullable();
$table->string('country')->nullable();
$table->string('city')->nullable();
$table->text('device')->nullable();
$table->timestamp('last_activity')->useCurrent();
$table->rememberToken();
$table->timestamps();//用于创建的列和更新的列
});
}
如果您不想在
列中更新和在
列中创建
类查看器扩展模型
{
//在模型类中,将时间戳设置为false
public$timestamps=false;
}
默认情况下,它可能希望这些列在那里,如果没有,它需要知道。你是如何创建这个表的?您是否删除了默认迁移中的某些内容?