Laravel: syntax error, unexpected 'class' (T_CLASS) in Laravel Cart


#1

I am trying to install a this shopping cart using Laravel but I am getting an error and I cannot figure it out. I have triple checked all my routes and they all seem to be what they should be. I have been searching everywhere for this, but what I can find, it actually shows where the error is, like "unexpected error in (error location).

syntax error, unexpected 'class' (T_CLASS)

This is my QuoteController (some code removed for space)

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use Gloudemans\Shoppingcart\Facades\Cart;


class QuoteController extends Controller
{
public function index()
    {
        return view('quote');
    }
public function store(Request $request)
    {
        Quote::add($request->$id, $request->name)
          ->associate('App\Product');

        return redirect()->route('quote.index')->with('success_message', 'Item was add to quote!');
    }
}

My routes in web.php look correct.

Route::get('/quote.php', 'QuoteController@index')->name('quote.index');
Route::post('/quote.php', 'QuoteController@store')->name('quote.store');

Also, here is the code for the button.

<form action="{{ route('quote.store') }}" method="POST">
                {{ csrf_field() }}
                <input type="hidden" name="id" value="{{ $product->id }}">
                <input type="hidden" name="name" value="{{ $product->name }}">
                <button type="submit" class="btn btn-lg valBtn text-uppercase">Request Quote</button>
            </form>

Both the error and the word quote (the return view(‘quote’); ) are in light green so I believe that is the issue, but I cannot find what could be causing the error. I am just now learning laravel with the help of this video.

Laravel Video

Any help on this would be greatly appreciate.


#2

Try removing spaces between the curly braces:

https://schoolsofweb.com/php-curly-braces-how-and-when-to-use-it/


#3

The code you posted doesn’t seem to have any errors. Could you please post your entire QuoteController class?


#4

Tried - Didn’t work


#5
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use Gloudemans\Shoppingcart\Facades\Cart;


class QuoteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('quote');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Cart::add($request->$id, $request->name)
          ->associate('App\Product');

        return redirect()->route('quote.store')->with('success_message', 'Item was add to quote!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

On a side note, I also tried this

 public function store(Request $request)
    {
        Quote::add($request->$id, $request->name)
          ->associate('App\Product');

        return redirect()->route('quote.store')->with('success_message', 'Item was add to quote!');
    }

As well as

public function store(Request $request)
    {
        Quote::add($request->$id, $request->name)
          ->associate('App\Product');

        return redirect()->route('quote.index')->with('success_message', 'Item was add to quote!');
    }

#6

I took it through the linter on my machine and it gives the same error.

Looking at the code in vim I see some weird character at the end of the use Gloudemans\Shoppingcart\Facades\Cart; line.

Just put your cursor at the line after it and keep hitting backspace until you’ve removed the ; at the end of that line (so you can be sure you’ve also deleted the weird character that came after it) and then type the ; again.

After that it should work.


#7

Well that did get rid of one error (I appreciate that),

But now I am getting this

"Class 'App\Http\Controllers\Quote' not found"

Code highlighted is

public function store(Request $request)
    {
        Quote::add($request->name)
          ->associate('App\Product');

        return redirect()->route('quote.index')->with('success_message', 'Item was add to quote!');
    }

I do have a QuoteController.php (which is what the code is from)


#8

That means that PHP can’t find the Quote class. You’d need to use it just like the other classes at the top of your file. I can’t help with the entire syntax because I don’t know where your Quote class is located.


#9

I create the use App\Quote;

But of course now throws another error.

Method Illuminate\Database\Query\Builder::add does not exist.

public function store(Request $request)
    {
        Quote::add($request->name)
          ->associate('App\Product');

        return redirect()->route('quote.index')->with('success_message', 'Item was add to quote!');
    }


closed #10

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.