laravel - Laravel 多对多关系的 Carbon 中缺少数据
问题描述
我最近将我的数据库从 MySql 更改为 MSSql,但我遇到了日期格式问题。我更改了所有模型以使用$dateFormat = 'Y-m-d H:i:s'
并且工作正常,但是今天我遇到了与其他模型具有多对多关系的模型的问题。
我的数据库以 Ymd H:i:s.000 格式保存日期,即 2018-08-20 16:01:12.000。我认为雄辩不尊重日期格式。
我正在使用 Laravel 5.4 和 php 5.6;
今天,我收到了这个错误
InvalidArgumentException in Carbon.php line 909:
Data missing
in Carbon.php line 909
at Carbon::createFromFormat('Y-m-d H:i:s.000', '2018-07-19 10:40:21') in Model.php line 3003
生成错误的代码,
public function getDetailQuiz()
{
$quiz_id = Input::get('quiz_id');
*$quiz = QuizTopic::with('questions.answers')->where('id', $quiz_id)->first();*
foreach ($quiz->questions as $question) {
$answers = [];
$answer_order = 0;
foreach ($question->answers as $i => $answer) {
$answers[] = ['answer' => $answer->description, 'id' => $answer->id, 'action' => 'update'];
if ($question->answer_id == $answer->id) $answer_order = $i;
}
$response['questions'][] = [
'id' => $question->id,
'question' => $question->description,
'correct_answer' => $question->answer_id,
'points' => $question->points,
'answers' => $answers,
'action' => 'update',
'answer_order' => $answer_order,
];
}
$quiz->start_date = Carbon::parse($quiz->start_date)->format('d/m/Y H:i');
$quiz->end_date = Carbon::parse($quiz->end_date)->format('d/m/Y H:i');
$response['quiz'] = $quiz->makeVisible(['award_value', 'type_id', 'start_date', 'end_date'])->toArray();
return response()->json(['result' => $response]);
}
我的测验主题模型
class QuizTopic extends Model
{
protected $table = 'quiz_topics';
protected $guarded = ['id'];
protected $hidden = [
'created_at',
'updated_at',
'deleted_at',
'created_user_id',
'updated_user_id',
'winner_clerk_id',
'award_value',
'status_id',
'type_id',
'start_date',
'end_date',
'raffle_date',
'fcm_notification_reference',
];
protected $dateFormat = 'Y-m-d H:i';
use SoftDeletes;
public function questions()
{
return $this->belongsToMany('App\Models\QuizQuestion', 'rel_topics_questions', 'topic_id', 'question_id')->withTimestamps();
}
}
我的测验问题模型
class QuizQuestion extends Model
{
protected $table = 'quiz_questions';
protected $guarded = ['id'];
protected $hidden = ['created_at', 'updated_at', 'deleted_at','points','pivot','created_user_id','updated_user_id'];
protected $dateFormat = 'Y-m-d H:i:s';
use SoftDeletes;
public function topics()
{
return $this->belongsToMany('App\Models\QuizTopic');
}
public function answers()
{
return $this->belongsToMany('App\Models\QuizAnswer', 'rel_questions_answers', 'question_id', 'answer_id')->withTimestamps();
}
}
解决方案
从手册:
默认情况下,时间戳的格式为“Ymd H:i:s”。如果您需要自定义时间戳格式,请在模型上设置 $dateFormat 属性。此属性确定日期属性在数据库中的存储方式,以及模型序列化为数组或 JSON 时的格式。
(强调我的)
由于您将日期保存为Y-m-d H:i:s.000
,因此您需要$dateFormat
在两个模型中设置属性,如下所示:
protected $dateFormat = 'Y-m-d H:i:s.000';
改变它,然后再试一次。
推荐阅读
- flutter - Flutter Drawer 与底部导航栏重叠
- c++ - 如何修复查找不存在的头文件的 Visual Studio 项目?
- visual-studio-code - 如何从 Visual Studio 代码扩展中获取主机操作系统名称?
- node.js - 如何为同一服务器上的三个应用程序配置我的 nginx 配置文件
- python - 如何在kivy python中使用滑块作为进度条并控制音频?
- amazon-cognito - 如何在亚马逊连接联系流块中获取 lex 属性?
- unity3d - TextMeshPro 字体不变
- azure - 在 Azure redis 缓存中加密和解密值
- python - UsageError:找不到行魔术函数`%sql`
- linux - 不运行 Cypress 和 linux cron