繁体   English   中英

如何在空表上添加记录

[英]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语句,用于比较表Hoursid本身。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM