1

The goal is if a specific card # is used the order submits and saves to the database (this part is working). However, if you put in a different specific number it should print "Your card has been declined" I can't get this part to work I am getting error Class 'App\Http\Controllers\Session' not found

Controller Use

<?php

namespace App\Http\Controllers;

use App\Item;
use App\Purchase;
use App\Http\Controllers\Session;
use Illuminate\Http\Request;

Controller Function

public function store(Purchase $purchase, Item $item)
{
  $this->validate(request(), [
  'fName' => 'required|min:3',
  'lName' => 'required|min:3',
  'address' => 'required',
  'city' => 'required',
  'state' => 'required',
  'zip' => 'required',
  'card' => ['required', 'regex:(5105 1051 0510 5100|4111 1111 1111 1111)']
  ]);

  $purchase->product = request('product');
  $purchase->fName = request('fName');
  $purchase->lName = request('lName');
  $purchase->address = request('address');
  $purchase->city = request('city');
  $purchase->state = request('state');
  $purchase->zip = request('zip');
  $purchase->card = request('card');

  if (request('card') == '5105 1051 0510 5100') {
    $purchase->save(); // Save to the database
    return redirect('/thanks/'.$item->id);
  } elseif (request('card') == '4111 1111 1111 1111') {
    Session::flash('error', "Special message goes here");
    return Redirect::back();
  }

  return back();
}

Edit now that it is working I am not getting the actual message to print.

Blade

@php
  if ( Session::has('error') ) {
    echo '<div class="alert alert-info">{{ Session::get("error") }}</div>';
  }
@endphp

1 Answer 1

1

You forgot the backslash

\Session::flash('error', "Special message goes here");
^^

Or replace

use App\Http\Controllers\Session;

With

use Session;

If you take a look at config/app.php you will see some aliases there.

And fix your code here:

@php
  if ( Session::has('error') ) {
    echo '<div class="alert alert-info">'. Session::get("error") . '</div>';
  }
@endphp
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome that is what I was missing. Quick question now that it's working it is printing "{{ Session::get("error") }}" instead of the actual error. Any ideas what I might be missing? I'll update my question with the blade.
I swear I tried that and it didn't work haha thanks dude! It worked when I tried.

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.