我是Laravel 5的新手,目前正在做一个带有CRUD功能的任务项目。我做了删除功能,但更新和添加仍然很混乱。请帮帮我。
我的数据库只有一个表,'tasks‘,有3列: id,name和name。
My TaskModel:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class TasksModel extends Model {
//
protected $table = 'tasks';
}这是我的个人观点:
<html>
<header>
<style>
.wrapper{
width:600px;
margin:auto;
}
</style>
<title>Tasks List</title>
</header>
<body>
<div class="wrapper">
<h2 align="center"><font color="#a52a2a">Task List</font></h2>
<table name ="todo_table" border="1px" cellspacing="0" width = "600px">
<tr align="center">
<td>ID</td>
<td>Name</td>
<td>Time</td>
<td>Action</td>
</tr>
<?php
$records = \App\TasksModel::all()->sortBy('id');
foreach($records as $mytask)
{
?>
<tr align="center">
<td><?php echo $mytask->id; ?></td>
<td width="200px"><?php echo $mytask->name; ?></td>
<td><?php echo $mytask->day; ?></td>
<td width="100px">
<a href = "{{ URL::to('add') }}"> <img src = "/public/images/add.jpg" width="30px" height="30px"> </a>
<a href = "{{ URL::to('delete' . '/id='. $mytask->id ) }}"> <img src = "/public/images/del.jpg" width="30px" height="`30px"> </a>
<a href = "{{ URL::to('update' . '/id='. $mytask->id ) }}"> <img src = "/public/images/update.jpg" width="30px" height="30px"> </a>
</td>
</tr>
<?php
}// End of foreach($records as $mytask)
?>
</table>
</div>
</body>
</html>下面是我的Add视图:
<html>
<head>
<style>
.wrapper{
width:600px;
margin:auto;
}
</style>
</head>
<body>
<div>
{!! Form::open(array('url' => 'process', 'method' => 'post')) !!}
<div class="wrapper">
<h2><font color="#a52a2a">Add Task</font></h2>
<p><input type = "text" name = "new-task" placeholder = "Add new task..." /></p>
<p><input type = "text" name = "new-time" placeholder = "Add new time..." /></p>
{!! Form::submit('Add', array('name' => 'bt_add')) !!}
</div>
{!! Form::close() !!}
</div>
</body>
</html>我的路线:
Route::get('/', 'HomeController@index');
Route::get('add', 'HoneController@add');
Route::get('delete/id={del}', 'HomeController@delete');
Route::get('update/id={edit}', 'HomeController@update');
Route::get('process', 'ProcessController@index'); // I think process page will handle update/add发布于 2015-03-07 23:51:37
您不需要手动执行CRUD,Laravel可以使用RESTFul控制器自动执行。
看看这里:
http://laravel.com/docs/5.0/controllers#restful-resource-controllers
要构建RESTFul URL,请使用路由方法
http://laravel.com/docs/5.0/helpers#urls
简单:)
发布于 2015-02-13 13:24:37
首先,下面的代码属于你的任务控制器。你的观点应该是愚蠢的。基本上没有理由将原始php插入到视图文件中。
<?php
$records = \App\TasksModel::all()->sortBy('id');
foreach($records as $mytask)
{
...
}
?>您将让TasksController响应路由并为视图准备数据。然后在视图中执行类似以下操作:
@foreach($tasks as $task)
...
@endforeach您的路由需要更新为: Route::delete('task/{id}',TaskController@delete);
你的表单也需要有一个删除的方法。
发布于 2017-02-10 20:44:42
您可以通过以下步骤轻松地在laravel中执行CRUD
Route::group(['prefix' => 'admin'], function () {
...
Route::resource('/categories', 'admin\CategoryController');
...
});使用Laravel中提供的PHP artisan命令创建资源
php artisan make:controller CategoryController --resource这将创建资源控制器,您将拥有emtpy方法索引、存储、更新、显示和销毁。
然后,您可以将您的方法绑定到视图
有关详细说明,请参阅http://deepdivetuts.com/basic-create-edit-update-delete-functionality-laravel-5-3上的教程
https://stackoverflow.com/questions/28479280
复制相似问题