Syntax error, unexpected '"', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) - laravel

I got this error in Laravel:
ErrorException thrown with message "syntax error, unexpected '"', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) (View: C:\xampp\htdocs\blog\resources\views\posts\view.blade.php)"
Stacktrace:
#0 Symfony\Component\Debug\Exception\FatalThrowableError in C:\xampp\htdocs\blog\storage\framework\views\84082f7931aeac789ccd4bfb975980dd81818f2a.php:16
And here is the code of view.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8 text-center">
<div class="card">
<div class="card-header">Post View</div>
<div class="card-body">
<div class="row">
<div class="col-md-4">
<!-- <ul class="list-group">
#if(count($categories) > 0)
#foreach($categories->all() as $category)
<li class="list-group-item"><a href='{{ url("category/{$category->id") }}'>{{$category->category}}</a></li>
#endforeach
#else
<p>No Category Found!</p>
#endif
</ul> -->
</div>
<div class="col-md-8">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection

It looks like you have an unmatched opening brace { here:
url("category/{$category->id")
I suspect this is being parsed by the Blade template system, and it is struggling to make sense of it. This is why the error comes out of the cached file, rather than your own.
This sort of error can be discovered by committing the view file to version control, and then snipping away parts of it until the error no longer happens. By a process of trial and error, you can find the section that raises the exception.

Related

Why can't I put <script> tags in my #section?

I have a #section('content') in a file home.blade.php which extends #extends('layouts.app'), for simplicity I'll use the generated Laravel scaffolding as an example.
home.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
You are logged in!
</div>
</div>
</div>
</div>
</div>
#endsection
I try and add a script tag to the section:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
You are logged in!
</div>
</div>
</div>
</div>
</div>
<script src="js/example.js"></script> <!-- Adding my script here -->
#endsection
But then I get the following error in the console:
app.js:38355 [Vue warn]: Error compiling template:
Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.
It says avoid placing script tags in templates? What exactly does this mean? How can I place a script within a #section, I don't want to put the script in my #extends('layouts.app'), I only want my script to work for this #section area.
You can use Blade stacks, they allow you to specifiy JavaScript for child views. In your home.blade.php add the following:
#push('scripts')
<script src="js/example.js"></script>
#endpush
This will push your script to a stack named scripts, you can name your stack to whatever you want. Then in your app.blade.php call the stack within your <head> tag as follows:
#stack('scripts')
Now your example.js file will only load for home.blade.php. Any other views that extend app.blade.php will not load this file.

How to get last child while parsing HTML with Goutte in Laravel

<div class="table">
<div class="table-head">
<div class="table-head-title">Ranking Equipos</div>
</div>
<div class="table-body">
<div class="table-body-row active">
<div class="col-key">Mark</div>
<div class="col-value">9233</div>
</div>
<div class="table-body-row">
<div class="col-key">Amanda</div>
<div class="col-value">7216</div>
</div>
<div class="table-body-row">
<div class="col-key">Mark</div>
<div class="col-value">6825</div>
</div>
<div class="table-body-row">
<div class="col-key">Paul</div>
<div class="col-value">6184</div>
</div>
<div class="table-body-row">
<div class="col-key">Amanda</div>
<div class="col-value">5866</div>
</div>
</div>
</div>
This is my HTML and I want to get last child of .table-body.
I tried to use JavaScript like logic and used indexing like this
$lastChild = $node->filter('.table-body .table-body-row')[4]; but it shows error. Cannot use object of type "Symfony\Component\DomCrawler\Crawler" as array
I was stuck in similar situation recently and I resolve this by using last() method. Syntax is here: $node->filter('.table-body .table-body-row')->last();

How to solve Route not found error in Laravel?

I am passing a route and trying to show on view page, but it's giving me route not found error, please check my code and let me know where i am mistaking.
here is my route.php file..
Route::get('amenities-view', 'PropertyController#amenitiesview');
Here are my controller.php file..
public function amenitiesview()
{
$amenities=Amenity::all();
return view('admin.amenities.amenity', compact($amenities));
}
here are my leftsidebar.blade.php file (Where i am clicking and going to amenity.blade.php file)
<li>
<a href="{{route('amenities.amenity')}}">
Amenities List
</a>
</li>
and here is my amenity.blade.php file..
#extends('layouts.adminPanel')
#section('body')
<body class="page-ecommerce-product">
#endsection
#section('admin-content')
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger" role="alert" style="display: none;">
<ul id="blogErrors">
</ul>
</div>
</div>
<div class="col-sm-12 col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-action">
<button class="btn btn-close"></button>
<button class="btn btn-min"></button>
<button class="btn btn-expand"></button>
</div>
<div class="panel-title">Add Amenities</div>
<p>Code Here</p>
</div><!-- /.panel-heading -->
</div><!--/.panel-->
</div>
</div><!-- /.row -->
</div>
#endsection
You are using a route without naming it. use like below
Route::get('amenities-view', 'PropertyController#amenitiesview')->name('amenities.amenity');
Laravel Documentation about named routing.

Laravel Blade double curly brace not working

By reading the Laravel documentation, I've understood that I should be able to use {{ }} to escape my strings. However, by doing this, I'm getting an error, but the string works fine if I run {!! !!}. Is there anything I'm missing in my code?
A complete view of the code blade file (login.blade.php)
#extends('layouts.app')
#section('content')
<div class="kt-grid kt-grid--ver kt-grid--root">
<div class="kt-grid kt-grid--hor kt-grid--root kt-login kt-login--v3 kt-login--signin" id="kt_login">
<div class="kt-grid__item kt-grid__item--fluid kt-grid kt-grid--hor" style="background-image: url({{ asset('assets/media//bg/bg-3.jpg);">
<div class="kt-grid__item kt-grid__item--fluid kt-login__wrapper">
<div class="kt-login__container">
<div class="kt-login__logo">
<a href="#">
<img src="assets/media/logos/logo-5.png">
</a>
</div>
<div class="kt-login__signin">
<div class="kt-login__head">
<h3 class="kt-login__title">Sign In To Account</h3>
</div>
<form class="kt-form" method="post" action="/login">
{!! csrf_field() !!}
<div class="input-group">
<input class="form-control" type="text" placeholder="Email" name="email" value="" autocomplete="off">
</div>
<div class="input-group">
<input class="form-control" type="password" placeholder="Password" name="password">
</div>
#if ($errors->any())
#foreach($errors->all() as $error)
<p>{!! error !!}</p>
#endforeach
#endif
<div class="row kt-login__extra">
<div class="col">
<label class="kt-checkbox">
<input type="checkbox" name="remember"> Remember me
<span></span>
</label>
</div>
<div class="col kt-align-right">
Forget Password ?
</div>
</div>
<div class="kt-login__actions">
<button id="kt_login_signin_submit" class="btn btn-brand btn-elevate kt-login__btn-primary">Sign In</button>
</div>
</form>
</div>
<div class="kt-login__account">
<span class="kt-login__account-msg">
Don't have an account yet ?
</span>
Sign Up!
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Error
ErrorException (E_ERROR) syntax error, unexpected 't' (T_STRING),
expecting ',' or ')' (View:
C:\xampp\htdocs\test\resources\views\auth\login.blade.php) Previous
exceptions syntax error, unexpected 't' (T_STRING), expecting ',' or
')' (0)
You need to make 'error' a variable with double brackets. It's considered an undefined constant otherwise.
#foreach($errors->all() as $error)
<p>{{ $error }}</p>
#endforeach
You also need to add the right curly brace and the missing right single quote for the URL in the asset() helper function.
<div class="kt-grid__item kt-grid__item--fluid kt-grid kt-grid--hor"
style="background-image: url({{ asset('assets/media/bg/bg-3.jpg') }}">

shoping cart data display in laravel

I want to display the order details form database. I store cart data into database using serialize method. When i want to display data i can not properly show it. only can print order object. And there give that a error.That is
(1/1) FatalErrorException syntax error, unexpected 'endforeach'
(T_ENDFOREACH) in 9ad30ab1db6ca668d8a75c428e65858dc800d045.php line 27
.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>User Profile</h1>
<hr>
<h2>My Oders</h2>
#foreach($orders as $order)
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group">
#forecah($order->cart->items as $product)
<li class="list-group-item">
<span class="badge">{{$product['product_price']}}
</span>
{{$product['item']['product_name']}} |
{{$product['qty']}} Units
</li>
#endforeach
</ul>
</div>
<div class="panel-footer">
<strong>Total Price : {{$order->cart->totalPrice}}</strong>
</div>
</div>
#endforeach
</div>
</div>
You have typo in your code:
#forecah($order->cart->items as $product)
Try to change into:
#foreach($order->cart->items as $product)e
Hope it works..as it does not detect the foreach loop before.
You have wrong spelling of forecah. you should correct the spelling.
Look at the second foreach and change the spelling.
You can copy code below as i have changed the spelling.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>User Profile</h1>
<hr>
<h2>My Oders</h2>
#foreach($orders as $order)
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group">
#foreach($order->cart->items as $product)
<li class="list-group-item">
<span class="badge">{{$product['product_price']}}
</span>
{{$product['item']['product_name']}} |
{{$product['qty']}} Units
</li>
#endforeach
</ul>
</div>
<div class="panel-footer">
<strong>Total Price : {{$order->cart->totalPrice}}</strong>
</div>
</div>
#endforeach
</div>

Resources