Lee
Lee

Reputation: 1171

new to CRUD in Laravel 5

I'm very new to Laravel 5. I'm currently doing a task project with CRUD functions. I did the delete function, but update and add are still messy. Please help me.

My database has only 1 table, 'tasks' with 3 columns: id, name and day.

My TaskModel:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class TasksModel extends Model {

    //
    protected $table = 'tasks';

}

Here is my home view:

<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>

Here's my Add view:

   <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>

My Route:

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

Upvotes: 2

Views: 10322

Answers (3)

Joel Fernandes
Joel Fernandes

Reputation: 161

You can do CRUD in laravel easily by following these steps

Route::group(['prefix' => 'admin'], function () {
  ...
  Route::resource('/categories', 'admin\CategoryController');
  ...
});

Create the resource using PHP artisan command available in Laravel

php artisan make:controller CategoryController --resource

This will create the resource controller and you will have emtpy methods index, store, update, show, destroy.

You can then tie your methods to views

For detailed explaination please follow the tutorial at http://deepdivetuts.com/basic-create-edit-update-delete-functionality-laravel-5-3

Upvotes: 0

Mahmood
Mahmood

Reputation: 1197

You don't need to do CRUD manually, Laravel can do that automatically using RESTFul Controllers.

Take a look here:

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

To build the RESTFul URLs you use route method

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

simple :)

Upvotes: 5

joseph
joseph

Reputation: 464

First off the below belongs in your Task controller. Your view should be dumb. There is basically no reason to insert raw php into a view file.

<?php
    $records = \App\TasksModel::all()->sortBy('id');
    foreach($records as $mytask)
    {
        ...
    }
?>

You'll have your TasksController respond to the route and prepare the data for the view. Then in the view do something like:

@foreach($tasks as $task)
   ...
@endforeach

Your routes should be updated to: Route::delete('task/{id}', TaskController@delete);

Your form will need to have a method of delete as well.

Upvotes: 1

Related Questions