Laravel CRUD

@taylorotwell

If your method isn't index,create,store,edit,update,delete then make another controller where it CAN be one of those methods. Major

https://twitter.com/taylorotwell/status/735999833102680065

Official Laravel Docs

Basics

class BookController extends Controller
{
        public function index()
        {
                return Book::all();
        }
        public function store(Request $request)
        {
                return Book::create($request->all());
        }
        public function show($id)
        {
                return Book::findOrFail($id);
        }
        public function update(Request $request, $id)
        {
                Book::findOrFail($id)->update($request->all());
                return Response::json($request->all()); //response()->json()
        }
        public function destroy($id)
        {
                return Book::destroy($id);
        }
}

Sample Controller

<?php

class BookController extends BaseController {

        /**
         * Display a listing of the resource.
         *
         * @return Response
         */
        public function index()
        {
    $books = Book::all();
    return View::make('books.index')->with('books', $books);
        }

        /**
         * Show the form for creating a new resource.
         *
         * @return Response
         */
        public function create()
        {
                return View::make('books.create');
        }

        /**
         * Store a newly created resource in storage.
         *
         * @return Response
         */
        public function store()
        {
    $rules = array(
        'title'       => 'required',
        'author'      => 'required|email',
        'price'           => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if ($validator->fails()) {
        return Redirect::to('books/create')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } else {
        // store
        $book = new Book;
        $book->title       = Input::get('title');
        $book->author      = Input::get('author');
        $book->price = Input::get('price');
        $book->save();

        // redirect
        Session::flash('message', 'Successfully created book!');
        return Redirect::to('book');
    }
        }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return Response
         */
        public function show($id)
        {
    $book = Book::find($id);
    return View::make('books.show')->with('book', $book);
        }

        /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return Response
         */
        public function edit($id)
        {
    $book = Book::find($id);
    return View::make('books.edit')->with('book', $book);
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  int  $id
         * @return Response
         */
        public function update($id)
        {
             $rules = array(
        'title'       => 'required',
        'author'      => 'required|email',
        'price'           => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if ($validator->fails()) {
        return Redirect::to('books/' . $id . '/edit')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } else {
        // store
        $book = new Book;
        $book->title       = Input::get('title');
        $book->author      = Input::get('author');
        $book->price = Input::get('price');
        $book->save();

        // redirect
        Session::flash('message', 'Successfully updated book!');
        return Redirect::to('books');
    }
}
        }

        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return Response
         */
        public function destroy($id)
        {

    $book = Book::find($id);
    $book->delete();

    // redirect
    Session::flash('message', 'Successfully deleted book');
    return Redirect::to('books');
        }
}

Routes

ACTIONS HANDLED BY THE CONTROLLER

Verb Path (URL) Action (Method) Route Name GET /books index books.index GET /books/create create books.create POST /books store books.store GET /books/{id} show books.show GET /books/{id}/edit edit books.edit PUT/PATCH /books/{id} update books.update DELETE /books/{id} destroy books.destroy