Wednesday, March 8, 2017

$errors not showing in Laravel 5.4

I am using a fresh install of Laravel 5.4 now and I also installed https://github.com/appzcoder/crud-generator. I generated a Tickets CRUD using the generator. I was able to publish the "tickets" table to mysql database with "php artisan migrate"

I am currently stuck trying to make the $errors show if there is a missing input to one of the text inputs.

TicketsController.php

    <?php

namespace App\Http\Controllers\Users;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Ticket;
use Illuminate\Http\Request;
use Session;

class TicketsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\View\View
     */
    public function index(Request $request)
    {
        $keyword = $request->get('search');
        $perPage = 25;

        if (!empty($keyword)) {
            $tickets = Ticket::where('user_id', 'LIKE', "%$keyword%")
                ->orWhere('subject', 'LIKE', "%$keyword%")
                ->orWhere('description', 'LIKE', "%$keyword%")

                ->paginate($perPage);
        } else {
            $tickets = Ticket::paginate($perPage);
        }

        return view('user.tickets.index', compact('tickets'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('user.tickets.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {

        $requestData = $request->all();

        Ticket::create($requestData);

        Session::flash('flash_message', 'Ticket added!');

        return redirect('tickets');
    }


}

View

        <div class="panel panel-default">
            <div class="panel-heading">Create New Ticket</div>
            <div class="panel-body">
                <a href="" title="Back"><button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                <br />
                <br />

                @if ($errors->any())
                    <ul class="alert alert-danger">
                        @foreach ($errors->all() as $error)
                            <li></li>
                        @endforeach
                    </ul>
                @endif

                {!! Form::open(['url' => '/tickets', 'class' => 'form-horizontal', 'files' => true]) !!}

                @include ('user.tickets.form')

                {!! Form::close() !!}

            </div>
        </div>

form.blade.php

<div class="form-group ">
    {!! Form::label('user_id', 'User Id', ['class' => 'col-md-4 control-label']) !!}
    <div class="col-md-6">
        {!! Form::number('user_id', null, ['class' => 'form-control']) !!}
        {!! $errors->first('user_id', '<p class="help-block">:message</p>') !!}
    </div>
</div><div class="form-group ">
    {!! Form::label('subject', 'Subject', ['class' => 'col-md-4 control-label']) !!}
    <div class="col-md-6">
        {!! Form::text('subject', null, ['class' => 'form-control']) !!}
        {!! $errors->first('subject', '<p class="help-block">:message</p>') !!}
    </div>
</div><div class="form-group ">
    {!! Form::label('description', 'Description', ['class' => 'col-md-4 control-label']) !!}
    <div class="col-md-6">
        {!! Form::text('description', null, ['class' => 'form-control']) !!}
        {!! $errors->first('description', '<p class="help-block">:message</p>') !!}
    </div>
</div>

<div class="form-group">
    <div class="col-md-offset-4 col-md-4">
        {!! Form::submit(isset($submitButtonText) ? $submitButtonText : 'Create', ['class' => 'btn btn-primary']) !!}
    </div>
</div>

If don't input the subject text input, I am prompted with page error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'subject' cannot be null (SQL: insert into `tickets` (`user_id`, `subject`, `description`, `updated_at`, `created_at`) values (1, , dsa, 2017-03-08 13:05:22, 2017-03-08 13:05:22))

Instead of the $errors functionality

Do i need to include something in my TicketsController? and Middleware?

Any help is appreciated.



via redshot

Advertisement