Skip to content

Send Email Views

JP Barbosa edited this page Mar 11, 2016 · 3 revisions

Send Email Views

Add create method and HTML returns to recommendations controller
nano app/Http/Controllers/RecommendationsController.php
...

class RecommendationsController extends Controller
{
    public function create(Article $article)
    {
        return view('recommendations.create', compact('article'));
    }

    public function store(RecommendationRequest $request, Article $article, ArticleMailer $mailer)
    {
        $mailer->recommendTo($request->input('email'), $article);
        session()->flash('flash_message', 'Your recommendation was sent.');

        if (Request::wantsJson()) {
            return ['Your recommendation was sent.'];
        } else {
            return redirect('articles');
        }
    }
}
Create recommendations views
mkdir resources/views/recommendations/
nano resources/views/recommendations/create.blade.php
@extends($layout)

@section('content')
    @include('shared.alert')
    <h4>Recommend "{{ $article->title }}" by e-mail</h4>
    <p>
        {!! Form::open(['route' => ['articles.recommendations.store', $article->id]]) !!}
            <div class='form-group'>
                {!! Form::label('email', 'Email:') !!}
                {!! Form::text('email', null, ['class' => 'form-control']) !!}
            </div>
            {!! Form::submit('Send Recommendation', ['class' => 'btn btn-primary']) !!}
        {!! Form::close() !!}
    </p>
@endsection
Add link to recommend article
nano resources/views/articles/index.blade.php
    ...
    <table class="table">
        <tr>
            <th>Edit</th>
            <th>Delete</th>
            <th>Recommend</th>
            <th>Title</th>
            <th>Author</th>
        </tr>
        @foreach ($articles as $article)
            <tr>
                <td>{!! link_to_route('articles.edit', 'Edit', $article->id, ['class' => 'btn btn-default']) !!}</td>
                <td>
                    {!! Form::open(['method' => 'DELETE', 'route' => ['articles.destroy', $article->id]]) !!}
                        <button type="submit" class="btn btn-warning">Delete</button>
                    {!! Form::close() !!}
                </td>
                <td>{!! link_to_route('articles.recommendations.create', 'Recommend', $article->id) !!}</td>
                <td>{!! link_to_route('articles.show', $article->title, $article->id) !!}</td>
                <td>{!! $article->author->name !!}</td>
            </tr>
        @endforeach
    </table>
    ...
Before running verify if you have this article
open http://localhost:8000/articles/1/recommendations/create
Check your mailbox or laravel.log
tail storage/logs/laravel.log
Add mailer views to Git
git add .
git commit -m "Add mailer views"
Next step: Jobs Queue