0

I've made a mail function with an try/catch situation. It checks if the mail template exists, if not it needs to return redirect / back to the page with a message(return redirect('dashboard')->with('status', "Mail doesn't exists");)

Is it possible to redirect with a variable using a twig page? Or is there just something that I am doing wrong?

This is my mail function(DashboardController.php)

 public function MailContact($date_id, $contact_id)
{
    try
    {
        $contact = Contact::find($contact_id);
        $date = Date::find($date_id);
        $mail = Mails::where('datetype_id', $date->datetype_id)->first();
        $html = $mail->html;

        Mail::to($contact->email)->send(new Anniversary($html));

        return redirect('dashboard');
        }
    catch(\Exception $ex )
    {
        return redirect('dashboard')->with("Mail doesn't exists");
    }
}

This is the twig page('dashboard.twig')

 <div class="box-body no-padding" style="width: 100%; padding-top:20px;">
  <ul class="telephoneKingList" style=" display: inline-block;
    width: 100%;">
        {% for dates in datestodayUser %}
           {% if dates is not empty %}
              <li data-id="{{ dates.id }}">
                 {{ dates.contact.fullname }}, {{ dates.contact.relation.company }} <br>
                 {{ dates.description }}<br>
                 {# Place where Error needs to be #}
                 <a role="button" id="edit-button" href="/dashboard/mail/{{ dates.id }}/{{ dates.contact.id }}" class="btn btn-primary btn-sm"><i class="fa fa-envelope"></i></a>
              </li>
              <hr>
           {% endif %}
           {% else %}
              <li>
                 Geen data beschikbaar.
              </li>
           {% endfor %}                         
  </ul>

1

2 Answers 2

1

You can do

return redirect('dashboard')->with(['message'=>"Mail doesn't exists"]);

and then in your twig

{{message}} will give you the message

Sign up to request clarification or add additional context in comments.

2 Comments

Works, thanks.{{ $message }} should be {{ message }} tho
Oops missed that one, also accept the same if worked
0

Use

redirect('dashboard')->withErrors(["Mail doesn't exists"])

and in your twig you need to access the errors using

{{ errors.first() }}

Laravel has a middleware included out of the box called ShareErrorsFromSession which shares the session errors with all views.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.