如果您对Laravel未知列'updated_at'感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Laravel未知列'updated_at'的详细内容,我们还将为您解答未
如果您对Laravel未知列'updated_at'感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Laravel未知列'updated_at'的详细内容,我们还将为您解答未知列是什么的相关问题,并且为您提供关于Gorm 中的更新将created_at、updated_at 作为默认时间、laravel deleted_at 字段is null、Laravel Eloquent 比较 created_at 和 updated_at、Laravel Model updating&updated 事件使用注意事项的有价值信息。
本文目录一览:- Laravel未知列'updated_at'(未知列是什么)
- Gorm 中的更新将created_at、updated_at 作为默认时间
- laravel deleted_at 字段is null
- Laravel Eloquent 比较 created_at 和 updated_at
- Laravel Model updating&updated 事件使用注意事项
Laravel未知列'updated_at'(未知列是什么)
我刚开始使用Laravel,却遇到以下错误:
未知列’updated_at’插入gebruikers(naam,wachtwoord,updated_at,created_at)
我知道错误是由您在迁移表时的时间戳列引起的,但我没有使用该updated_at
字段。我遵循Laravel教程时曾经使用它,但是现在我正在制作(或尝试制作)自己的东西。即使不使用时间戳,我也会收到此错误。我似乎找不到使用它的地方。这是代码:
控制者
public function created(){ if (!User::isValidRegister(Input::all())) { return Redirect::back()->withInput()->withErrors(User::$errors); } // Register the new user or whatever. $user = new User; $user->naam = Input::get(''naam''); $user->wachtwoord = Hash::make(Input::get(''password'')); $user->save(); return Redirect::to(''/users'');}
路线
Route::get(''created'', ''UserController@created'');
模型
public static $rules_register = [ ''naam'' => ''unique:gebruikers,naam''];public static $errors;protected $table = ''gebruikers'';public static function isValidRegister($data){ $validation = Validator::make($data, static::$rules_register); if ($validation->passes()) { return true; } static::$errors = $validation->messages(); return false;}
我一定会忘记某事…我在这里做错了什么?
答案1
小编典典在模型中,编写以下代码;
public $timestamps = false;
这会起作用。
Explanation : By default laravel will expect created_at & updated_at column in
your table. By making it to false it will override the default setting.
Gorm 中的更新将created_at、updated_at 作为默认时间
我正在尝试使用 gorm 更新数据库中的实体帖子,更新在数据库中正确反映,但在 gorm 包返回的对象中,created_at 和 Updated_at 字段是默认时间,即“0001/01/01 ...” p>
我的数据库模型是
type Base struct { ID uuid.UUID `json:"id" gorm:"primaryKey;default:gen_random_uuid();not null"` CreatedAt time.Time `json:"created_at" gorm:"default:now()"` UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"` DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at" swaggertype:"primitive,string"` } type Post struct { Base Title string `json:"title" gorm:"column:title"` TLDR string `json:"tldr" gorm:"column:tldr"` HTML string `json:"html" gorm:"column:html"` JSON string `json:"json" gorm:"column:json"` BannerImage string `json:"banner_image" gorm:"column:banner_image"` Slug string `json:"slug" gorm:"column:slug"` Status Status `json:"status" gorm:"column:status"` AuthorID uuid.UUID `json:"author_id" gorm:"column:author_id;"` }
查询是
func (p *pgPostRepository) UpdatePost(postDetails *models.Post) (*models.Post, error) { db := p.db.GetClient().(*gorm.DB) err := db.Model(&models.Post{}).Where(&models.Post{ Base: models.Base{ ID: postDetails.ID, }, }).Updates(&postDetails).Error if err != nil { return nil, err } return postDetails, nil }
仅供参考,API 响应是 -
{ "message": "post updated successfully", "post": { "id": "e4645d79-51da-4e08-85d3-e7409ad6b77b", "created_at": "0001-01-01T00:00:00Z", "updated_at": "0001-01-01T00:00:00Z", "deleted_at": null, "title": "Sample Title", "tldr": "Brief summary of the content", "html": "<p>This is the HTML content.</p>", "json": "{\"key\": \"value\"}", "banner_image": "https://example.com/image.jpg", "slug": "sample-title-1694539437151872840", "status": "draft", "author_id": "96fa719c-358e-4c5d-b878-daee02e2c38b" } }
正确答案
您应该尝试在更新记录时手动设置 UpdatedAt 字段。像这样的事情:
func (p *pgPostRepository) UpdatePost(postDetails *models.Post) (*models.Post, error) { db := p.db.GetClient().(*gorm.DB) postDetails.UpdatedAt = time.Now() err := db.Model(&models.Post{}).Where(&models.Post{ Base: models.Base{ ID: postDetails.ID, }, }).Updates(&postDetails).Error if err != nil { return nil, err } return postDetails, nil }
以上就是Gorm 中的更新将created_at、updated_at 作为默认时间的详细内容,更多请关注php中文网其它相关文章!
laravel deleted_at 字段is null
在laravel中 deleted_at 字段默认查询方式是 deleted_at is null ,而现在很多数据库默认字段不能设置为null,目前有一个解决方案,在model类中将deleted_at设置格式为时间戳
在model里加上如下代码:
//const DELETED_AT=''deleted_at'';
//set the date format as timestamp
protected $dateFormat=''U'';
官方文档:https://learnku.com/docs/lara...
关于null的讨论 https://learnku.com/laravel/t...
Laravel Eloquent 比较 created_at 和 updated_at
如何解决Laravel Eloquent 比较 created_at 和 updated_at?
TblUserProfile::where(''updated_at'',''>'',''created_at'')->pluck(''user_id'');
以上查询返回以下
Illuminate\Support\Collection {#1854
#items: []
}
但是当我直接向 MysqL 运行以下查询时
select `user_id` from `tbl_user_profile` where updated_at > created_at
给我如下结果
我在 Eloquent 中做错了什么吗?
解决方法
使用whereColumn
TblUserProfile::whereColumn(''updated_at'',''>'',''created_at'')->pluck(''user_id'');
Laravel Model updating&updated 事件使用注意事项
1 触发条件
1.1 updating
1.1.1 如果字段无变化,不会触发此事件。
1.1.2 除非更改至少一个字段的值
2 事件逻辑不会覆盖
2.1 Trait 中定义事件如下
/**
* The boot method.
*/
public static function bootHasArchive()
{
static::creating(function ($model) {
Log::info(__FILE__);
});
static::updating(function ($model) {
Log::info(__FILE__);
});
static::deleting(function ($model) {
if ($archive = $model->archive) {
$archive->delete();
}
});
}
2.2 Observer 中定义事件如下:
public function creating(News $news)
{
Log::info(__FILE__);
}
public function updating(News $news)
{
Log::info(__FILE__);
}
2.3 两个位置定义的逻辑都会执行
2.4 顺序是: trait->observer
3 结论
3.1 update 事件用来处理 model changed 的逻辑
3.2 模型数据不变,点击更新这样的逻辑,放在控制器方法中,而不应放在模型的事件中
关于Laravel未知列'updated_at'和未知列是什么的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Gorm 中的更新将created_at、updated_at 作为默认时间、laravel deleted_at 字段is null、Laravel Eloquent 比较 created_at 和 updated_at、Laravel Model updating&updated 事件使用注意事项等相关知识的信息别忘了在本站进行查找喔。
本文标签: