user7298892
user7298892

Reputation:

How to debug "Parse error: syntax error, unexpected '<', expecting ']'" in Laravel?

I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc). This form sends the data to a "orders" table.

I'm now getting the following error in orders.blade.php:

Parse error: syntax error, unexpected '<', expecting ']'

I don't understand why I'm getting '<'; I don't see this in the code!

This is my code so far:

CarController

function catalog() {
    $cars = DB::table('cars')->get();
    return view('catalog', compact('cars'));
}

function orders($modelesc=null) {
    $cars = DB::table('cars')->where('Model', '=', '$modelesc');
    return view('orders', compact('cars'));
}

Catalog.blade.php

@foreach($cars as $car)
    {!! Form::open(array('action' => 'CarController@orders', 'method' => 'GET')) !!}
    {!! Form::hidden('$modelesc', $car->Model) !!}
    {!! Form::submit($car->Model) !!}
    {!! Form::close() !!}                                       
@endforeach

Orders.blade.php

{!! Form::open(array('action' => 'index', 'method' => 'POST')) !!}
    {!! Form::text('Model', $car->Model) !!}
    {!! Form::hidden(users_id, Auth::user()->id) !!}
    {!! Form::hidden(Fabrication_date, date(Y-m-d)) !!}
    {!! Form::select('Colour', [
        @foreach($colours as $colour)
            '$colour->Colour' => '$colour->Colour'
        @endforeach
    ]) !!}
    {!! Form::hidden(Order_status_id, '1' !!}
{!! Form::close() !!}

This is the structure of the orders table. The *_id fields come from other tables, and I want to fill some values of the forms with the relevant entry (id, users_id, Model, Fabrication_date, Colour_id, Order_status_id).

Upvotes: 0

Views: 1477

Answers (3)

Chris Forrence
Chris Forrence

Reputation: 10094

Your orders method in your CarController doesn't set $cars to what you'd think; without calling get(), it'll be an instance of Builder instead of an array! Furthermore, you're only fetching cars where the model equals the string "$modelesc" instead of the value of $modelesc.

Furthermore, in your orders.blade.php file, you seem to reference a $colours variable, but that doesn't get passed down in your CarController.

Change your orders method to the one below:

public function orders($modelesc = null)
{
    $cars = DB::table('cars')
        ->where('Model', $modelesc)
        ->get();

    $colours = DB::table('colours')->get()->pluck('Colour');

    return view('orders', compact('cars', 'colours'));
}

There is also an issue with your Form::select call in your orders.blade.php file; the Blade template directives (@foreach, @endforeach) are only for the "HTML" portion of the Blade file. Since we called pluck() on the Collection of colours in the CarController, we can simply pass in $colours for that method.

{{-- Incorrect --}}
{!! Form::select('Colour', [
    @foreach($colours as $colour)
        '$colour->Colour' => '$colour->Colour'
    @endforeach
]) !!}

{{-- Correct --}}
{!! Form::select('Colour', $colours) !!}

You also appear to be using constants in your Form builders (instead of strings with quotes); these should be strings only.

{!! Form::open(['action' => 'index', 'method' => 'POST']) !!}
    {!! Form::text('Model', $car->Model) !!}
    {!! Form::hidden('users_id', Auth::user()->id) !!}
    {!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
    {!! Form::select('Colour', $colours) !!}
    {!! Form::hidden('Order_status_id', '1') !!}
{!! Form::close() !!}

Upvotes: 0

Karl
Karl

Reputation: 5463

Firstly you need to wrap the Form names within quotes, the Order Status ID was also missing a closing bracket:

{!! Form::hidden('users_id', Auth::user()->id) !!}

{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}

{!! Form::hidden('Order_status_id', '1') !!}

Next, if $colours is a collection you can do the following in Laravel 5.4 (I'm unsure which version you're using)

{!! Form::select('Colour', $colours->pluck('Colour')) !!}

If you're on Laravel 5.1 or prior, you'll do the following:

{!! Form::select('Colour', $colours->lists('Colour')) !!}

This is because the lists method was removed in 5.2.

@pseudoanime is also correct with his answer, the database call needs the get method adding

Upvotes: 2

pseudoanime
pseudoanime

Reputation: 1593

try changing $cars = DB::table('cars')->where('Model', '=', '$modelesc');

$cars = DB::table('cars')->where('Model', '=', $modelesc)->get();

Upvotes: 0

Related Questions