Laravel 使用 foreach 循环更新/编辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51299643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:53:45  来源:igfitidea点击:

Laravel update/edit using foreach loop

phpmysqllaravel

提问by Grace

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

我试图更新我的表,但我无法让它工作..我总是以错误告终:

Invalid argument supplied for foreach()

为 foreach() 提供的参数无效

Here is my Controller:

这是我的控制器:

 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:

这是我的表格,我试图循环更新的分数:

enter image description here

enter image description here

回答by rkj

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

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

Change in view

视图的变化

@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 

Controller update score

控制器更新分数

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(); 
    }
} 

回答by WebDevB

Using Score::find($id)you're only ever going to return 1 result so there won't be anything to foreach over.

使用Score::find($id)你只会返回 1 个结果,所以不会有任何东西可以 foreach。

If you just want to update a single row in the table you don't need to foreach it.

如果您只想更新表中的单行,则无需 foreach。

You can simply run

你可以简单地运行

$score = Score::find($request->input($id));
$score->jan_ap = $request->input('jan_ap');
$score->jan_hm = $request->input('jan_hm');
$score->save();

if you want to return multiple rows you need to change your find to a get()

如果要返回多行,则需要将 find 更改为 get()

so $scores = Score::where('something', $request->input('something))->get();

所以 $scores = Score::where('something', $request->input('something))->get();

If you want to update every row in the table with the same information you'd do a:

如果您想使用相同的信息更新表中的每一行,您可以执行以下操作:

$scores = Score::all();

$scores = Score::all();

That would return every row in the table.

这将返回表中的每一行。