我想更新数据库中的多行并找到了一种方法。但这似乎不是最好的方法,因为它目前正在多次调用中进行。
如果有更好的方法可以做到这一点,我现在就想。
homecontroller
具有以下功能updatePersons
:
<?php
class HomeController extends BaseController {
public function index()
{
$persons = Person::get();
return View::make('hello')
->with(compact('persons'));
}
public function updatePersons()
{
$persons = Person::get();
foreach ($persons as $person) {
$person->fname = Input::get('fname'.$person->id);
$person->lname = Input::get('lname'.$person->id);
$person->save();
}
return Redirect::route('home')->with('succes', 'Siden er opdateret');
}
}
带有表单的视图
@extends('layouts.master')
@section('content')
<div class="container">
<ul class="list-group">
{{ Form::open(array('route'=>'personUpdate', 'method' => 'post')) }}
@foreach ($persons as $person)
<li class="list-group-item">
{{ Form::text('fname'.$person->id,$person->fname,array('class'=>'form-control', 'placeholder'=>'fname')) }}
{{ Form::text('lname'.$person->id,$person->lname,array('class'=>'form-control', 'placeholder'=>'lname')) }}
</li>
@endforeach
<li class="list-group-item">
<div class="box-footer">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</li>
{{ Form::close() }}
</ul>
</div>
</div>
@stop
路线:
<?php
Route::get('/', array('as'=>'home', 'uses'=>'HomeController@index'));
Route::post('/', array('as'=>'personUpdate', 'uses'=>'HomeController@updatePersons'));
我试图在 foreach 循环之后使用该savemany()
函数,但没有结果。$persons
updatePersons()