CRUD Operations in Laravel 5 With MYSQL, RESTFUL
CRUD Operations in Laravel 5 With MYSQL, RESTFUL
Until this point our project is working fine. Otherwise learn how to install Laravel in previous posts.
Please make sure you have exactly these fields in your table, specially update_at and created_at, because Eloquent require
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
=> 'Illuminate\Html\FormFacade',
=> 'Illuminate\Html\HtmlFacade',
8. Restful Controller
In laravel 5, a resource controller defines all the default routes for a given named resource to follow REST principles, where
we could find more information about it. So when you define a resource in your bookstore/app/Http/routes.php like:
routes.php file
<?php
/*
|-------------------------------------------------------------------------| Application Routes
|-------------------------------------------------------------------------|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
/*
Route::get('/', 'WelcomeController@index');
Route::get('home', 'HomeController@index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);*/
Route::resource('books','BookController');
As we can see we block the original code to avoid the default authentication laravel created for us.
A restful controller follows the standard blueprint for a restful resource, which mainly consists of:
Domain
Method
URI
Name
Action
GET|HEAD
books
books.index
App\Http\Controllers\BookController@index
GET|HEAD
books/create
books.create
App\Http\Controllers\BookController@create
POST
books
books.store
App\Http\Controllers\BookController@store
GET|HEAD
books/{books}
books.show
App\Http\Controllers\BookController@show
GET|HEAD
books/{books}/edit
books.edit
App\Http\Controllers\BookController@edit
PUT
books/{books}
books.update
App\Http\Controllers\BookController@update
PATCH
books/{books}
DELETE
books/{books}
App\Http\Controllers\BookController@update
books.destroy
App\Http\Controllers\BookController@destroy
Midleware
In order to display images for books cover we need to create a folder called img inside bookstore/public and download
some books cover picture and rename the name according to the field image in our table as shown above with extension
jpg.
To display book list we need to create a view: Go to folder bookstore/resources/view and create a folder called books;
inside this new folder create a new file called index.blade.php and copy the following code
index..blade.php file
@extends('layout/template')
@section('content')
<h1>Peru BookStore</h1>
<a href="{{url('/books/create')}}" class="btn btn-success">Create Book</a>
<hr>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="bg-info">
<th>Id</th>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Thumbs</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($books as $book)
<tr>
<td>{{ $book->id }}</td>
<td>{{ $book->isbn }}</td>
<td>{{ $book->title }}</td>
<td>{{ $book->author }}</td>
<td>{{ $book->publisher }}</td>
<td><img src="{{asset('img/'.$book->image.'.jpg')}}" height="35" width="30"></td>
<td><a href="{{url('books',$book->id)}}" class="btn btn-primary">Read</a></td>
<td><a href="{{route('books.edit',$book->id)}}" class="btn btn-warning">Update</a></td>
<td>
{!! Form::open(['method' => 'DELETE', 'route'=>['books.destroy', $book->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
Lets see how our book list are displayed; in the command like type php artisan serve, after open a browser and type
localhost:8888/books
<h1>Book Show</h1>
<form class="form-horizontal">
<div class="form-group">
<label for="image" class="col-sm-2 control-label">Cover</label>
<div class="col-sm-10">
<img src="{{asset('img/'.$book->image.'.jpg')}}" height="180" width="150" class="img-rounded">
</div>
</div>
<div class="form-group">
<label for="isbn" class="col-sm-2 control-label">ISBN</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="isbn" placeholder={{$book->isbn}} readonly>
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" placeholder={{$book->title}} readonly>
</div>
</div>
<div class="form-group">
<label for="author" class="col-sm-2 control-label">Author</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="author" placeholder={{$book->author}} readonly>
</div>
</div>
<div class="form-group">
<label for="publisher" class="col-sm-2 control-label">Publisher</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="publisher" placeholder={{$book->publisher}} readonly>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<a href="{{ url('books')}}" class="btn btn-primary">Back</a>
</div>
</div>
</form>
@stop
Modify app/Http/Controllers/BookController.php
public function show($id)
{
$book=Book::find($id);
return view('books.show',compact('book'));
}
Refresh the brower and click in Read action and we will see the book in detail:
Modify app/Http/Controllers/BookController.php
Modify app/Http/Controllers/BookController.php
public function edit($id)
{
$book=Book::find($id);
return view('books.edit',compact('book'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
$bookUpdate=Request::all();
$book=Book::find($id);
$book->update($bookUpdate);
return redirect('books');
}
refresh the brower, select the book and click Update button
Refresh the Brower and click in delete button for deleting one book and redirect to book list.
Jorge Serrano at 4:45 AM
Share
15
87 comments:
Traiano Welcome May 2, 2015 at 10:28 AM
Excellent learning resource! Thank you :-)
Reply
return redirect('books');
}
works with update too :)
courtesy of http://stackoverflow.com/questions/28573860/laravel-requestall (comment from shock_gone_wild)
Reply
Route::resource('Books','BookController')in
place
of
Replies
Ferry Chrisnandika July 22, 2015 at 1:19 AM
or if you still want to apply
use Illuminate\Http\Request;
just change your code a little bit,
from :
public function store()
{
$book=Request::all();
Book::create($book);
return redirect('books');
}
to:
public function store(Request $request)
{
$book=$request->all(); // important!!
Book::create($book);
return redirect('books');
}
works with update too :)
courtesy of http://stackoverflow.com/questions/28573860/laravel-requestall (comment from shock_gone_wild)
Reply
Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
Fatal error: Unknown: Failed opening required 'D:\xampp\htdocs\Laravel/server.php' (include_path='.;D:\xampp\php\PEAR') in
Unknown on line 0
Reply
Replies
anil March 28, 2016 at 10:33 AM
plz reply fast
Reply
Excellent Tutorial
Reply
Comment as:
Publish
riztboed (Google)
Sign out
Notify me
Preview
Home
About Me
Jorge Serrano
Follow
86
Software Developer with 10 years experience in full life-cycle development, including analysis, design, development, testing,
documentation, implementation, and maintenance of application software in web-based/desktop environment, distributed Ntier architecture, and client/server architecture.
Experience in designing and developing object-oriented software applications, ranging from e-business, asset management, and Internet and
intranet applications.
Sound knowledge of developing applications based on architectures such as Hibernate Framework, Spring Framework, and MVC architecture.
Experience in implementing core Java & J2EE design patterns, Solid understanding of business needs and requirements
Strong management, planning, architecting, analyzing, designing, and programming capabilities.
Excellent analytical, problem solving, communication, and team skills.
View my complete profile
Powered by Blogger.