[英]How can I add records on an empty table
我在MySQL的数据库中有一个空表,并且尝试添加多个记录
(I do this in this way because I'm following and order; If I delete a record then the auto increment field (id) skips 1 value. For example. I delete the id = 340 and then the following record start with an id with a value = 341)
我在控制器的函数中有一个If Else
语句,用于比较表Hours的id
本身。
public function showHours($id){
$complex = ComplexNew::find($id);
$fields = CourtsComplex::all();
$hours = HoursNew::all();
$last_hours = collect($hours)->last();
if ($last_hours->id == $last_hours->id){
$last_hours->id = $last_hours->id + 1;
}else{
return Redirect()->back();
}
return view('hours.FormNewHours')->with('complex', $complex)->with('fields', $fields)->with('last_hours', $last_hours);
}
这行是我有错误的行。
if ($last_hours->id == $last_hours->id){
//DO SOMETHING
// ...
}
错误是:
'Trying to get property 'id' of non-object'
。
我也试图添加另一个if else语句,像这样:
if(is_null($last_hours->id)){
$last_hours->id = 1;
}else if($last_hours->id == $last_hours->id){
//ADD +1 TO ID.
}
因为我希望如果表为空,则第一条记录的id必须以value = 1开始,但是如果表不为空,则将1添加到最后一个id,就像for语句一样,因为此条件总是将+1添加到最后一个id 。
更改
$last_hours = collect($hours)->last();
至
$last_hours = $hours->last();
加dd($last_hours)
如果返回对象 ,然后尝试访问id
喜欢$last_hours->id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.