如何使用复选框从视图传递数据到控制器laravel 5.2

时间:2016-04-06 17:11:25

标签: laravel

显示的界面正在使用@foreach方法显示。因此,我想知道如何只将一个复选框(视图)传递给控制器​​。

以下是视图编码。

@foreach($articles as $article)
<tr>
<td>{{ $article->jenis_simptom}}</td>
<td style="width: 10px">
<input type="checkbox" value="{{ $article->jenis_simptom}}" name="{{ $article->id }">
</td>
</tr>
@endforeach

谢谢

Here is the dummy interface

2 个答案:

答案 0 :(得分:0)

将articles []用作html字段名称,并将$ article-&gt; id作为键

@foreach($articles as $article)
    <tr>
        <td>{{ $article->jenis_simptom}}</td>
        <td style="width: 10px">
            <input type="checkbox" value="{{ $article->jenis_simptom}}" name="articles[{{ $article->id }]">
        </td>
    </tr>
@endforeach

在您的控制器中,您将能够通过提交的数组进行循环,该数组仅包含html中checked的元素

foreach ($request->input('articles') as $key => $value) {
    // $key will contain your article id
}

答案 1 :(得分:0)

您可以使用Laravel Collective表单。如果您使用的是Laravel 4,它开箱即用,对于Laravel 5,您应该install the package

CouchDbRepositorySupport

当用户提交表单时,@RunWith(EasyMockRunner.class) @SpringApplicationConfiguration(App.class) public class ServiceTest { @Rule public EasyMockRule mocks = new EasyMockRule(this); @TestSubject UserService userService = new UserServiceImpl(); @Mock UserRepository userRepositoryMock; @Test public void testGetUser() { User user = new User("Bob","bob87); user.setId("bob87"); //username is the id userService.getUser(user.getId()); EasyMock.expect(userRepositoryMock.get(user.getId())).andReturn(user); //the line where the error occurs EasyMock.expectLastCall().times(1); EasyMock.replay(userRepositoryMock); EasyMock.verify(userRepositoryMock); } } 的{​​{1}}操作会捕获所有表单数据,包括已选中的复选框,并将其放入{!! Form::open(array('action' => 'Controller@store')) !!} {!! Form::checkbox('name', 'value') !!} {!! Form::submit('Send') !!} {!! Form::close() !!} 变量中。

store