刚开始用laravel使用phpStorm。我试图创建一个接受输入的表单,然后选中一个复选框,然后将其添加到数据库中。 数据库中的所有行都列在视图index.html中。应允许用户激活或取消激活(使用复选框)浏览index.html文件时应列出的消息。因为如果新闻文章处于活动状态,并且您想要取消激活它,您应该只需取消选中该复选框,它就会被停用。 所以我想知道我的功能应该是什么样子。
我也不确定当选中或取消选中复选框时如何运行ActivatedOrDeactivate函数。
这是我的第一篇文章,所以请告诉我在描述中是否有难以理解的内容。
我的路线:
Route::get('admin',
[
'uses' => 'NyhetsController@index',
'as' => 'admin.index'
]
);
Route::get('admin/create',
[
'uses' => 'NyhetsController@create',
'as' => 'admin.create'
]
);
Route::post('admin',
[
'uses' => 'NyhetsController@store',
'as' => 'admin.store'
]
);
Route::get('admin/{id}',
[
'uses' => 'NyhetsController@show',
'as' => 'admin.show'
]
);
Route::get('admin/{id}/edit',
[
'uses' => 'NyhetsController@edit',
'as' => 'admin.edit'
]
);
Route::put('admin/{id}',
[
'uses' => 'NyhetsController@update',
'as' => 'admin.update'
]
);
Route::get('admin/{id}/delete',
[
'uses' => 'NyhetsController@delete',
'as' => 'admin.delete'
]
);
Route::delete('admin/{id}',
[
'uses' => 'NyhetsController@destroy',
'as' => 'admin.destroy'
]
);
包含该功能的我的控制器(我在愤怒中写的东西):
public function ActivatedOrDeactivated($id){
$news = News::findOrFail($id);
$input = Input::get('active');
if($input == true)
{
$news->active = 'false';
$news->update($input);
}
else{
$news->active = 'true';
$news->update($input);
}
}
我的索引文件包含以下格式:
<h1>Alle postene i databasen</h1>
<p>{{ link_to_route('admin.create', 'Legg til ny nyhet') }}</p>
@if($news->count())
<table class="table table-striped table-boarded">
<thead>
<tr>ID</tr>
<tr>Title</tr>
<tr>Author</tr>
<tr>Message</tr>
<tr>Active</tr>
<tr>Picture path</tr>
<tr>Activate/Deactivate</tr>
</thead>
<tbody>
@foreach($news as $n)
<tr>
<td>{{ $n->id }}</td>
<td>{{ $n->title }}</td>
<td>{{ $n->author }}</td>
<td>{{ $n->message }}</td>
<td>{{ $n->active }}</td>
<td>{{ $n->picture_path }}</td>
<td>{{ Form::checkbox('active') }}</td>
<td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>
</tr>
@endforeach
</tbody>
</table>
答案 0 :(得分:3)
如果您想在用户点击没有ajax的复选框时更新新闻项,这将是一种方法:
router.php
Route::post('admin/{id}/active',
[
'uses' => 'NyhetsController@toggleActive',
'as' => 'admin.toggle_active'
]
);
HTML index.blade.php
@if($news->count())
<table class="table table-striped table-boarded">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Message</th>
<th>Picture path</th>
<th>Active</th>
</tr>
</thead>
<tbody>
@foreach($news as $n)
<tr>
<td>{{ $n->id }}</td>
<td>{{ $n->title }}</td>
<td>{{ $n->author }}</td>
<td>{{ $n->picture_path }}</td>
<td>{{ $n->message }}</td>
<td>
{{ Form::open(array('url' => 'admin/' . $n->id . '/active')) }}
{{ Form::submit('', [ 'class' => ($n->active ? 'glyphicon glyphicon-ok' : 'glyphicon glyphicon-remove') ]);
{{ Form::close() }}
<td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
NyhetsController
public function toggleActive($id) {
$news = News::findOrFail($id);
if($news->active)
{
$news->active = 'false';
$news->update($input);
}
else{
$news->active = 'true';
$news->update($input);
}
}
return Redirect::to('admin');
代码未经测试!
答案 1 :(得分:1)
在你的表格中::
{{ Form::checkbox('active', 'true') }}
在您的控制器
中$news = News::findOrFail($id);
if (Input::get('active') === 'true') {
$news->active = 'true'; // UPDATE:must be true of course
$news->update($input);
} else {
$news->active = 'false'; // UPDATE:must be false of course
$news->update($input);
}
顺便说一下:如果你只是route restfully使用
,你可以节省一些打字Route::resource('admin', 'NyhetsController');