Laravel-MongoDB CRUD

Create Controller

by Remy Pereira on 10th October 2016

Now we have to define a controller corresponding to our Book model. Following the Laravel convention, we name it BookController. Create the file manually or use artisan.

php artisan make:controller BookController --resource

This creates app/Http/Controllers/BookController.php

The controller will have CRUD methods index(), create(), store(), show($id), edit($id), update($id), destroy($id). These methods are blank and we have to define them according to our requirement.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;

class BookController extends Controller
{
    
    public function index()
    { //
    }

    public function create()
    { //
    }
    public function store(Request $request)
    { //
    }
    public function show($id)
    {
        //
    }
    public function edit($id)
    {
        //
    }
    public function update(Request $request, $id)
    {
        //
    }
    public function destroy($id)
    { //
    }
}

Add the following lines:

use App\book;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Collection;

Now you may define your custom functions for index, create, store, show etc. Each controller method returns a view and some information.

Below are sample functions you could use:

 public function index()
    {
    	// Get all the documents from books collection 
    	// and return the view 
      
      	$books = DB::collection('books')->get();
      
      return view('bookindex', compact('books'));
    }
    
 public function create()
    {
         // Return the view for adding books
      
      return view('bookadd');
    }
    
 public function store(Request $request)
    {
        // Create new book object and populate the fields 
        // with parameters from POST. 
        // Save it in database and return the view 
        
        $book = new book;
	$book->title =  $request->input('title');
	$book->isbn =  $request->input('isbn');
	$book->author =  $request->input('author');
	$book->category = $request->input('category') ;
	$book->save();
	$books = DB::collection('books')->get();
      
      return view('bookindex', compact('books'));
    }
    
 public function show($id)
    {
        // Get the document of given id and return the view

        $book = DB::collection('books')->where('_id', $id)->get();
	
      return view('bookview', compact('book'));
    }
    
 public function edit($id)
    {
        // Get the document of given id and return the view
 
 	$book = DB::collection('books')->where('_id', $id)->get();

      return view('bookedit', compact('book'));
    }
    
 public function update(Request $request, $id)
    {
         // Update the document of given id with parameters from 
         // the PUT or PATCH request and return to index view
         
        DB::collection('books')->where('_id', $id)
	  ->update([
		'title' => $request->input('title'),
		'isbn' => $request->input('isbn'),
		'author' => $request->input('author'),
		'category' => $request->input('category')
		] );
		
	$books = DB::collection('books')->get();
        
      return view('bookindex', compact('books'));
    }
    
 public function destroy($id)
    {
         // Delete the document of given id and return index view
        DB::collection('books')->where('_id', $id)->delete();
	$books = DB::collection('books')->get();
        return view('bookindex', compact('books'));
    }
    

Now we have all the methods for CRUD operations on MongoDB defined in our controller.

Post a comment

Comments

Nothing yet..be the first to share wisdom.