Laravel使用foreach循环进行更新/编辑 [英] Laravel update/edit using foreach loop

查看:55
本文介绍了Laravel使用foreach循环进行更新/编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新我的表,但是我无法使其工作..我总是以错误结尾:

I was trying to update my table but I couldn't make it work.. I always end up with the error:

为foreach()提供的参数无效

Invalid argument supplied for foreach()

这是我的控制人:

 public function show_score($id)
    {

        $scores = Score::with(['lead','subject'])->where(['subject_id'=>$id])->get();

        return view('markbook.show_score',compact('scores'));
    }

    public function update_score(Request $request)
    {
        $id = $request->input('id');

         $scores = Score::find($id);
         foreach ($scores as $datas) {
         $datas->jan_ap=$request->input('jan_ap');
         $datas->jan_hm=$request->input('jan_hm');
         $datas->save();  

        }


    }

route:
Route::get('markbook/scores/{id}', 'MarkbookController@show_score' );
Route::post('markbook/scores', 'MarkbookController@update_score'); 

这是我要循环更新分数的桌子:

Here is my table I'm trying to loop the updated score:

推荐答案

从聊天讨论中发现,您想要更新多个分数,该分数列在tr,td中.您可以像这样更改它

From chat discussion what found is that you want to update multiple scores, which is listed in tr, td. You can change it like this

更改视图

@foreach($scores as $score) 
    <tr> 
        <td>{{$score->lead->student_name}} <input type="hidden" name="scores[{{$loop->index}}][id]" value="{{$score->id}}"></td> 
        <td><input type="text" name="scores[{{$loop->index}}][jan_ap]" value="{{$score->jan_ap}}"></td> 
        <td><input type="text" name="scores[{{$loop->index}}][jan_hm]" value="{{$score->jan_hm}}"></td> 
    </tr> 
@endforeach 

控制器更新得分

public function update_score(Request $request) 
{ 
    $scores = $request->input('scores');  //here scores is the input array param 

    foreach($scores as $row){
        $score = Score::find($row['id']); 
        $score->jan_ap = $row['jan_ap']; 
        $score->jan_hm = $row['jan_hm']; 
        $score->save(); 
    }
} 

这篇关于Laravel使用foreach循环进行更新/编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆