Mysql “我如何解决?”;无效的日期时间格式:1366不正确的整数值;拉威尔7号的例外?
我对Laravel很陌生,我想更新mySQL数据库中一个名为“client_id”的列。表的名称是“projects”。我想在客户端创建新项目时插入此列,因为该项目属于创建它的客户端。“client_id”是名为“clients”的表中的主键(id)。“客户”表和“项目”表之间存在关系。我已经编写了一些代码来解决这个问题,但是我得到了这个异常,“client_id”列没有更新。 请帮忙。 在我的控制器中存储方法:Mysql “我如何解决?”;无效的日期时间格式:1366不正确的整数值;拉威尔7号的例外?,mysql,laravel,Mysql,Laravel,我对Laravel很陌生,我想更新mySQL数据库中一个名为“client_id”的列。表的名称是“projects”。我想在客户端创建新项目时插入此列,因为该项目属于创建它的客户端。“client_id”是名为“clients”的表中的主键(id)。“客户”表和“项目”表之间存在关系。我已经编写了一些代码来解决这个问题,但是我得到了这个异常,“client_id”列没有更新。 请帮忙。 在我的控制器中存储方法: /** * Store a newly created Projec
/**
* Store a newly created Project in storage.
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'description' => 'required|string|max:255',
'start_date' => 'required|date',
'start_time' => 'required|string|max:10',
]);
$project = Project::create([
'name' => $request->name,
'description' => $request->description,
'start_date' => $request->start_date,
'start_time' => $request->start_time,
]);
$loggedinId = Auth::id();
$project->update(['created_by' => $loggedinId]);
$userId = $loggedinId;
$clientId = Client::where('user_id', '=', $userId)->pluck('id')->all();
DB::table('projects')->where('created_by', '=', $loggedinId)->update([ 'client_id' => $clientId]);
return redirect()->back()->with('message', 'You have successfully submitted your Job Card');
}
我的客户模型中的关系:
/**
* @return HasMany
*/
public function projects()
{
return $this->hasMany(Project::class);
}
我的项目模型中的关系:
/**
* @return BelongsTo
*/
public function client()
{
return $this->belongsTo(Client::class, 'client_id');
}
我的刀片视图中的表单:
<form method="POST" action="{{ route('client-jobcard.store') }}">
@csrf
<div class="form-group row">
<div class="col-sm-8">
<label for="name">{{ __('Job Card Name') }}</label><span class="required">*</span>
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<div class="col-sm-8">
<label for="description">{{ __('Job Card Description') }}</label><span class="required">*</span>
<textarea class="form-control{{ $errors->has('description') ? ' is-invalid' : '' }}" id="description" rows="4" style="height: 150px;" name="description" value="{{ old('description') }}" required autofocus></textarea>
@if ($errors->has('description'))
<span class="invalid-feedback">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<label for="start_date">Start Date</label><span class="required">*</span>
<input type="date" class="form-control" id="start_date" name="start_date" value="{{ old('start_date') }}" required>
</div>
<div class="col-sm-4">
<label for="submission_date">Start Time</label><span class="required">*</span>
<input type="text" class="form-control" id="start_time" name="start_time" value="{{ old('start_time') }}" required>
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">
{{ __('Submit') }}
</button>
</div>
</div>
</form>
@csrf
{{{(“工作卡名称”)}*
@如果($errors->has('name'))
{{$errors->first('name')}
@恩迪夫
{{{(“工作卡说明”)}*
@如果($errors->has('description'))
{{$errors->first('description')}
@恩迪夫
开始日期*
开始时间*
{{{('Submit')}
此行:
$clientId = Client::where('user_id', '=', $userId)->pluck('id')->all();
将返回一列,而不是所需的单个ID(“2”)
然后,当您将其分配给client\u id
这是一个整数时,ORM将把它呈现为字符串[2]
,而不是数字2
,这将给您一个错误
只从客户端
中检索第一个元组,或者手动提取$clientId[0]
(浪费且不推荐)
您还有第二个错误我忽略了:
'created_by', '=', $loggedinId
上面的意思是由创建的
必须是一个整数值,但错误文本暗示SQL希望它是一个日期时间。数据库中的定义似乎有误。您的时间字段是字符串,必须更改,格式不同
你能试试这个吗
感谢您的回复,“检索第一个元组”意味着使用ID检索?就像文档中解释的那样:是的。基本上,如果您确定查询将返回某些内容,您可以使用->first()
而不是->all()
。我想你最好做一点实验来熟悉雄辩。我现在注意到错误有点复杂。补充回答。非常感谢您的帮助。从->all()
更改为->first()
已经解决了这个问题。感谢您的回复,我尝试使用时间数据类型而不是字符串,并且得到了相同的错误。我决定暂时换成字符串。但我也遇到了验证时间域的挑战。