Advertisement
kellykamay

QuoteController.php

Mar 24th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6.  
  7. // use App\Http\Requests;
  8.  
  9. use App\AuthorModel;
  10. use App\QuoteModel;
  11.  
  12. class QuoteController extends Controller
  13. {
  14.     public function getIndex()
  15.     {
  16.         $quotes = QuoteModel::all();
  17.         return view('index' ,[
  18.             'quotes' => $quotes
  19.         ]);
  20.  
  21.     }
  22.  
  23.     public function postQuote(Request $request)
  24.     {
  25.         $authorText = ucfirst($request['author']);
  26.         $quoteText = $request['quote'];
  27.  
  28.         $theAuthor = AuthorModel::where('name', $authorText)->first();
  29.         if(!$theAuthor){
  30.             $theAuthor = new AuthorModel();
  31.             $theAuthor->name = $authorText;
  32.             $theAuthor->save();
  33.         }
  34.         $theQuote = new QuoteModel();
  35.         $theQuote->quote = $quoteText;
  36.         $theAuthor->quotes()->save($theQuote);
  37.  
  38.         return redirect()->route('index')->with([
  39.             'success' => 'Quote saved!'
  40.         ]);
  41.  
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement