我更改值状态时的ID问题

时间:2019-07-15 22:43:06

标签: laravel laravel-5

在我的revision表格中,我有2辆摩托车正在修订中。有数字000001000003

enter image description here

在表单motorbikes中,我看到摩托车000002的ID已被修改,而不是ID号000003

enter image description here

有关ID的问题?

在我的Controller摩托车中,我有这个:

 public function index()
    {
        $motorbikes = Motorbike::oldest()->paginate(5);

        $bikeIdsDown = Revision::where('date_revision_start', "<" , Carbon::now())->where('date_revision_end', ">", Carbon::now())->pluck('id')->toArray();

        return view('admin.motorbikes.index', compact('motorbikes', 'bikeIdsDown'))
          ->with('i', (request()->input('page',1) -1)*5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
   public function create()
    {       

        $revisions = Revision::all();
        $bikeIdsDown = Revision::where('date_revision_start', "<" , Carbon::now())->where('date_revision_end', ">", Carbon::now())->pluck('id')->toArray();

        return view('admin.motorbikes.create', compact('revisions', 'motorbikes'));
    }

在我的index.blade中,我也有这个:

@foreach($motorbikes as $motorbike)
  <tr>
    <td>{{$motorbike->matriculation }} </td>
    <td>{{$motorbike->number_motorbike}}</td>
    <td> @if(in_array($motorbike->id, $bikeIdsDown))
           UNAVAILABLE
         @else
           Available
         @endif
     </td>

1 个答案:

答案 0 :(得分:1)

看起来$bikeIdsDown正在收集修订版id,而不是摩托车。 修订版模型上的摩托车的FK为fk_motorbike。因此...尝试更改$bikeIdsDown查询以拉出FK,而不是修订版ID:

 $bikeIdsDown = Revision::where('date_revision_start', "<=" , Carbon::now())
      ->where('date_revision_end', ">=", Carbon::now())
      ->pluck('fk_motorbike')->toArray(); //<-- fk of motorbike, not revision

还请注意,我在日期比较中添加了<=>=-不知道您是否要在修订中包括同一天。