尝试添加数据时,foreach()提供了无效的参数

这是提交数据的表格

info-admin.blade.php

<tbody class="text-center align-middle">     @foreach ( $info as $infos )         <tr>             <th scope="row" class="align-middle">{{ $loop->iteration }}</th>             <td class="align-middle">{{ $infos->judul }}</td>             <td class="align-middle">{{ $infos->konten }}</td>             <td class="align-middle">{{ $infos->image }}</td>             <td class="align-middle">{{ $infos->created_at }}</td>             <td class="align-middle">{{ $infos->Updated_at }}</td>             <td class="align-middle form">                 <a href="{{ route('infos.edit', [$infos->id]) }}"><button type="submit" class="btn btn-info mb-3">Edit</button></a>                 <form method="POST" action="{{ route('infos.destroy', [$infos->id]) }}">                     {{ csrf_field() }}                     {{ method_field('DELETE') }}                     <button type="submit" class="btn btn-danger">Hapus</button>                 </form>             </td>         </tr>     @endforeach </tbody> 

储存逻辑

InfosController

public function store(Request $request) {     $info = new Info();      $info->judul = $request->input('judul');     $info->konten = $request->input('konten');     $info->image = $request->input('image');      if($request->hasFile('image')){         $file = $request->file('image');         $extension = $file->getClientOriginalExtension();         $filename = time() . '.' . $extension;         $file->move('upload/info_penting' , $filename);         $info->image = $filename;     } else{         return $request;         $info->image = '';     }      $info->save();      return view('admin.info-admin')->with('info','$info'); } 

我的路线,以备不时之需

web.php

route::post('/tambah-info','InfosCont[email protected]')->middleware('auth','admin'); 

尝试更新时编辑未定义的变量

用于更新数据InfosController的逻辑的控制器

public function update(Request $request, Info $info)     {         //         Info::where('id', $info->id)             ->update([                  'judul' => $request->judul,                 'konten' => $request->konten,                 'image' => $request->image,             ]);          return redirect('/info-admin')->with('success', 'Berhasil Diedit');     } 

这是用于更新数据的表格edit-info.blade.php

<form action="{{ route('infos.update', [$info->id]) }}" method="patch" enctype="multipart/form-data>             @csrf             <input type="hidden" name="_method" value="PATCH">             <div class="mb-3">             <label for="judul">Judul</label>             <input type="string" class="form-control" value="{{$info->judul}}" id="judul" name="judul" required>                 <div class="invalid-feedback">                 Kolom Wajib Diisi                 </div>             </div>             <div class="form-group">                 <label for="exampleFormControlTextarea1">Konten</label>                 <textarea class="form-control" value="{{$info->konten}} id="exampleFormControlTextarea1" rows="3" name="konten" required></textarea>                 <div class="invalid-feedback">                 Kolom Wajib Diisi                 </div>             </div>             <input class="mb-3" type="file"value="{{$info->image}} name="image">             <button type="submit" class="btn btn-success btn-lg btn-block mt-auto">Kirim</button>         </form>     </div> 

===============>>#1 票数:0 已采纳

保存帖子后,您应该重定向到另一条路线,可能是显示所有帖子的路线

return redirect()->route('info-admin'); 

您应该有另一种用于显示所有数据的控制器方法,您将获得与此类似的信息

public function index() {     $infos = Info::all();     return view('info-admin', $infos); } 

然后用于将集合发送到视图。 您应该以这种方式而不是其他方式使用foreach

@foreach($infos as $info) // your code here @endforeach  

更新

您没有在update方法中通过$id

public function update(Request $request, $id)     {         Info::where('id', $id)             ->update([                  'judul' => $request->judul,                 'konten' => $request->konten,                 'image' => $request->image,             ]);          return redirect('/info-admin')->with('success', 'Berhasil Diedit');     } 

您的观点应该是这样的

<tbody class="text-center align-middle">     @foreach ( $infos as $info )         <tr>             <th scope="row" class="align-middle">{{ $loop->iteration }}</th>             <td class="align-middle">{{ $info->judul }}</td>             <td class="align-middle">{{ $info->konten }}</td>             <td class="align-middle">{{ $info->image }}</td>             <td class="align-middle">{{ $info->created_at }}</td>             <td class="align-middle">{{ $info->Updated_at }}</td>             <td class="align-middle form">                 <a href="{{ route('infos.edit', $info->id) }}"><button type="submit" class="btn btn-info mb-3">Edit</button></a>                 <form method="POST" action="{{ route('infos.destroy', $info->id) }}">                     {{ csrf_field() }}                     {{ method_field('DELETE') }}                     <button type="submit" class="btn btn-danger">Hapus</button>                 </form>             </td>         </tr>     @endforeach </tbody> 

  ask by noone translate from so

本文未有回复,本站智能推荐: