syntax error, unexpected '{' - Laravel

---ERROR MESSAGE

FatalErrorException in BooksController.php line 66: syntax error, unexpected '{'

--LINE 66

$book = DB::table('books')->where('id', $id)->first();

---CODE

   public function request(Request $request, $id) {
        $book = DB::table('books')->where('id', $id)->first();
        $user_id = {{ Auth::guard('web')->user()->id }} 
        DB::table('book_requests')->insert(
            ['book_id' => $book->id, 'user_id' => $user_id ]
        );
    }

Thanks in advance

Answers


You can't use {{ }} construction in controller, only in a view.

Try this:

public function request(Request $request, $id) {
        $book = DB::table('books')->where('id', $id)->first();
        $user_id = Auth::guard('web')->user()->id;
        DB::table('book_requests')->insert(
            ['book_id' => $book->id, 'user_id' => $user_id ]
        );
    }

You cannot use {{ }} blade syntax in controller.

You can use it in .blade.php file only.

{{ 'string' }} means <?php echo 'string' ?>

In controller, just write

$user_id = Auth::guard('web')->user()->id;

and in view(blade template) file, print it via

{{ $user_id }} // equal to <?php echo $user_id; ?>

Refer below link for better understanding.

https://laravel.com/docs/5.1/blade


Why you are using blade syntax in controller? Remove the blade syntax from controller

public function request(Request $request, $id) {
    $book = DB::table('books')->where('id', $id)->first();
    $user_id = Auth::guard('web')->user()->id;
    DB::table('book_requests')->insert(
        ['book_id' => $book->id, 'user_id' => $user_id ]
    );
}

Need Your Help

what's standard web server downtime?

web-server database file-server

I want to know the standard of web server, file server, database server and print server downtime per day.

Linux & C: System() Command

c linux system iio

I am currently working on a project that requires me to call a Linux command during a C code. I have found on other sources that I can do this using the system() command and then save the value for...