Creating a CRUD system in laravel

To interact with our database, we might need to create a CRUD (create, read, update, and
delete) system. That way, we add and alter our data without needing a separate database
client. This recipe will be using a RESTful controller for our CRUD system.

Getting ready

For this recipe, we’ll be building on the User tables created in the recipe Using automatic
validation in models.

How to do it…

To complete this recipe, follow these steps:
1. In the app/controllers directory, create a file named as UsersController.
php and add the following code:
<?php
class UsersController extends BaseController {
public function getIndex()
{
$users = User::all();
return View::make(‘users.index’)->with(‘users’,
$users);
}

public function getCreate()
{
return View::make(‘users.create’);
}

public function postCreate()
{
$user = new User();
$user->username = Input::get(‘username’);
$user->email = Input::get(’email’);
$user->save();
return Redirect::to(‘users’);
}
public function getRecord($id)
{
$user = User::find($id);
return View::make(‘users.record’)->with(‘user’,
$user);
}
public function putRecord()
{
$user = User::find(Input::get(‘user_id’));
$user->username = Input::get(‘username’);
$user->email = Input::get(’email’);
$user->save();
return Redirect::to(‘users’);
}

public function deleteRecord()
{
$user = User::find(Input::get(‘user_id’))
->delete();
return Redirect::to(‘users’);
}
}
2. In our routes.php file, add a route to the controller:
Route::controller(‘users’, ‘UsersController’);
3. In the app/views directory, create another directory named as users, create a file
named index.php in that, and add the following code:
<style>
table, th, td {
border:1px solid #444
}
</style>
<table>
<thead>
<tr>
<th>User ID</th>

<th>User Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $user): ?>
<tr>
<td><?php echo $user->id ?></td>
<td><?php echo $user->username ?></td>
<td><?php echo $user->email ?></td>
<td>
<a href=”users/record/<?php echo $user
->id ?>”>Edit</a>
<form action=”users/record”
method=”post”>
<input type=”hidden” name=”_method”
value=”DELETE”>
<input type=”hidden” name=”user_id”
value=”<?php echo $user->id
?>”>
<input type=”submit”
value=”Delete”>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<a href=”users/create”>Add New User</a>

4. In the app/views/users directory, create a new file named create.php and the
form as follows:
<form action=”create” method=”post”>
Username:<br>
<input name=”username”><br>
Email<br>
<input name=”email”><br>
<input type=”submit”>
</form>
5. In the app/views/users directory, add a file named record.php and use the
following form:
<form action=”” method=”post”>
<input type=”hidden” name=”_method” value=”put”>
<input type=”hidden” name=”user_id” value=”<?php echo
$user->id ?>”>

Username:<br>
<input name=”username” value=”<?php echo $user
->username ?>”><br>
Email<br>
<input name=”email” value=”<?php echo $user->email
?>”><br>
<input type=”submit”>
</form>

How it works…

In our controller, our method names can be prepended with the HTTP verb we want to use. We
then add in the route in our routes file, so it points to the correct place.
Our first method will generate a list of all our users. We pass the users to our view, and then
loop through them and display them in a simple table.
Under that table, we have a link to our second method to add a new user. Our getRreate()
method displays a simple form, and that form gets posted and saved. After saving, we’re
redirected back to the list page.
To edit a record, we create a getRecord() method that gets the record’s ID passed to it.
Our view is a form that is automatically filled in with the values of the user for the ID that
was passed in. Since we’re doing an update, we want to use the put verb; to accomplish this,
we need a hidden field with the name _method and the value of the request we want to use.
When the form is submitted, Laravel will send it to the putRecord() method, and update
the information.
Finally, to delete a record, we create a simple form that has the hidden field named _method
and the value DELETE. When submitted, Laravel will send it

Leave a Reply

Your email address will not be published. Required fields are marked *