You are on page 1of 11

quotes app

php artisan make:model Author -m

//migrations/authors_table

$table->increments('id');

$table->timestamps();

$table->string('name');

Schema::dropIfExists('authors');

//migrations/quotes_table

$table->increments('id');

$table->timestamps();

$table->text('quote');

$table->integer('author_id');

Schema::dropIfExists('quotes');

//models/author

public function quotes()

return $this->hasMany('App\Quote');

//models/quote

public function author()

return $this->belongsTo('App\Author');

//views/index.blade.php

@extends('layouts.master')
@section('title')

Trending Quotes

@endsection

@section('styles')

<link href="{{ asset('css/font-awesome-4.7.0/css/font-awesome.min.css')}}"


rel="stylesheet">

@endsection

@section('content')

<section class="quotes">

<h1> Latest Quotes </h1>

<article class="quote">

<div class="delete">

<a href="#">x</a>

</div>

Quote Text

<div class="info">Created by <a href="#"> Saurav </a> on 2017</div>

</article>

Pagination

</section>

<section class="edit-quote">

<h1> Add a Quote</h1>

<form >
<div class="input-group">

<label for="author">Your name</label>

<input type="text" name="author" id="author" placeholder="Your name"> </input>

</div>

<div class="input-group">

<label for="quote">Your name</label>

<textarea name="quote" id="quote" placeholder="Your quote"> </textarea>

</div>

<button type="submit" class="btn">Submit Quote</button>

<input type="hidden" name="_token" value = "{{ Session::token() }}">

</form>

</section>

@endsection

//views/layouts/master.blade.php

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>@yield('title')</title>

<link rel="stylesheet" type="text/css" href="{{ URL::to('css/main.css') }}">

@yield('styles')

</head>

<body>

@include('includes.header')

<div class="main">

@yield('content')

</div>

</body>
</html>

//css/main.css

body {

padding : 0;

margin : 0;

font-family: "Georgia", Times,serif;

font-size : 16px;

line-height: 20px;

.main{

width:80%;

margin:49px auto;

.edit-quote{

text-align: center;

.quotes{

text-align: center;

.quote{

position: relative;

display: inline-block;

border : 1px solid #ccc;


box-shadow: 2px 2px 2px #ccc;

width:calc(25% - 48px);

margin:32px;

padding:16px;

vertical-align: top;

background-color: #fff6e0;

.quote.first-in-line{

margin-left: 0;

.quote.last-in-line{

margin-right: 0;

.quote .info{

margin-top:8px;

font-size:12px;

font-family: "Roboto",sans-serif;

color:#ccc;

.quote .info a{

color : #ccc;

.quote .info a:hover,

.quote .info a:active {

color : #aaa;
}

.quote .delete{

position: absolute;

top : 0px;

right: 4px;

font-family: sans-serif;

//views/index

<form method="post" action="{{ route('create') }}">

//quotecontroller

public function getIndex(){

$quotes = Quote::all();

return view('index',compact('quotes'));

public function postQuote(Request $request){

$this->validate($request,['author' => 'required|max:60|alpha',

'quote' => 'required|max:500']);

$authorText = ucfirst($request['author']);

$quoteText = $request->quote;

$author = Author::where('name',$authorText)->first();

if(!$author){

$author= new Author();

$author->name = $authorText;

$author->save();

$quote = new Quote();


$quote->quote = $quoteText;

$author->quotes()->save($quote);

return redirect()->route('index')->with(['success' => 'Quote Saved !']);

//routes/web.php

Route::get('/',['uses' => 'QuoteController@getIndex','as' => 'index']);

Route::post('/new',[

'uses' => 'QuoteController@postQuote',

'as' => 'create'

]);

//index.blade.php

@section('content')

<section class="quotes">

<h1> Latest Quotes </h1>

@for($i=0; $i<count($quotes);$i++)

<article class="quote">

<div class="delete">

<a href="#">x</a>

</div>

{{ $quotes[$i]->quote }}

<div class="info">Created by <a href="#"> {{ $quotes[$i]->author->name }} </a> on


{{$quotes[$i]->author->created_at}} </div>

</article>

@endfor

<div class="pagination">

Pagination

</div>

</section>
//quotecontroller/postquote

$this->validate($request,[

'author' => 'required|max:60|alpha',

'quote' => 'required|max:500'

]);

//index.blade.php

@section('content')

@if(count($errors) > 0)

<section class="info-box fail">

<ul>

@foreach($errors->all() as $error)

{{ $error }}

@endforeach

</ul>

</section>

@endif

@if( Session::has('success') )

<section class="info-box success">

{{ Session::get('success')}}

</section>

@endif

<section class="quotes">
//index.blade.php

<div class="delete">

- <a href="#">x</a>

<a href="{{ route('delete', ['quote_id' => $quotes[$i]->id ] ) }}">x</a>

</div>

//routes

Route::get('/delete/{quote_id}',[

'uses' => 'QuoteController@deleteQuote',

'as' => 'delete'

]);

//quotecontroller

public function deleteQuote($quote_id){

$quote = Quote::find($quote_id);

$author_deleted = false;

dd($quote->author);

//$test = $quote->author()->get();

$test = $quote->author->id;

//dd($test);

if(count($quote->author->quotes) === 1){

$quote->author->delete();

$author_deleted = true;

$quote->delete();

$msg = $author_deleted ? 'Quote and author deleted' : 'Quote deleted ';


return redirect()->route('index')->with(['success'=> $msg]);

//quotecontroller

public function getIndex($author=null){

if(!is_null($author)){

$quote_author = Author::where('name',$author)->first();

if($quote_author){

$quotes = $quote_author->quotes()->orderBy('created_at','desc')->get();

else{

$quotes = Quote::orderBy('created_at','desc')->get();

return view('index',['quotes' => $quotes]);

//index.blade.php

@section('content')

@if( !empty(Request::segment(1)))

<section class="filter-bar">

A filter has been set !

<a href="{{ route('index') }}"> Show all quotes</a>

</section>

@endif

- <div class="info">Created by <a href="#"> {{ $quotes[$i]->author->name }} </a> on


{{$quotes[$i]->author->created_at}}

+ <div class="info">Created by <a href="{{ route('index',['author' => $quotes[$i]-


>author->name]) }}"> {{ $quotes[$i]->author->name }} </a> on {{$quotes[$i]->author-
>created_at}} </div>
//routes

Route::get('/{author?}',[

'uses' => 'QuoteController@getIndex',

'as' => 'index'

]);

//quotecontroller

- $quotes = $quote_author->quotes()->orderBy('created_at','desc')->get();

+ $quotes = $quote_author->quotes()->orderBy('created_at','desc')->paginate(6);

- $quotes = Quote::orderBy('created_at','desc')->get();

+ $quotes = Quote::orderBy('created_at','desc')->paginate(6);

//index.blade

<div class="pagination">

<center>{{$quotes->links()}}</center>

</div>

@endsection

You might also like