Laravel 5中的CRUD新手

Lee*_*Lee 2 laravel-5

我是Laravel 5的新手.我目前正在做一个带有CRUD功能的任务项目.我做了删除功能,但更新和添加仍然是凌乱的.请帮我.

我的数据库只有1个表,'tasks'有3列:id,name和day.

My TaskModel:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class TasksModel extends Model {

    //
    protected $table = 'tasks';

}
Run Code Online (Sandbox Code Playgroud)

这是我的家庭观点:

<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>
Run Code Online (Sandbox Code Playgroud)

这是我的添加视图:

   <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>
Run Code Online (Sandbox Code Playgroud)

我的路线:

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
Run Code Online (Sandbox Code Playgroud)

Mah*_*ood 5

您不需要手动执行CRUD,Laravel可以使用RESTFul控制器自动执行此操作.

看看这里:

http://laravel.com/docs/5.0/controllers#restful-resource-controllers

要构建RESTFul URL,请使用route方法

http://laravel.com/docs/5.0/helpers#urls

简单:)