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

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.

Related

Laravel Blade foreach combine

I have two separate divs, one where the images are loaded and the other where the data is displayed.
<div class="banner-slider-image">
<div class="swiper-container">
<div class="swiper-wrapper">
<!-- Slide Item -->
<div class="swiper-slide">
<div class="bg" style="background-image: url(include/assets/images/main-slider/1.jpg);">
</div>
</div>
</div>
</div>
</div>
<div class="banner-slider-content">
<div class="side-text">AVANT</div>
<div class="swiper-container banner-slider">
<div class="swiper-wrapper">
#if(count($listings) > 0)
#foreach($listings as $listing)
<!-- Slide Item -->
<div class="swiper-slide">
<div class="content-outer">
<div class="content-box">
<div class="inner">
<h5>{{$listing->first_name}} {{$listing->last_name}}</h5>
<h1><span>{{$listing->name}}</span> </h1>
<div class="text">{{$listing->property_description}}.</div>
<div class="link-box">View Listing</div>
</div>
</div>
</div>
</div>
#endforeach
#endif
</div>
</div>
</div>
So the first div will also be in a foreach loop, but I dont know how I can combine the two where they show the same related data.
Use 2 foreach loops . It is the easiest and desired solution for you. or you can define two variable and concat the contents using one forloop and then display them later like
#php
$first='';
$second='';
#endphp
#if(count($listings) > 0)
#foreach($listings as $listing)
#php
$first.='first div content';
$second.='second div content';
#endphp
#endforeach
#endif
<div class="banner-slider-image">
<div class="swiper-container">
<div class="swiper-wrapper">
{!!$first!!}
</div>
</div>
</div>
<div class="banner-slider-content">
<div class="side-text">AVANT</div>
<div class="swiper-container banner-slider">
<div class="swiper-wrapper">
{!!$second!!}
</div>
</div>
</div>
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
use for yield

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 form Submission confirm box

I am a beginner in Laravel, and was wondering if any way to confirm delete before it go to /delete/{id}?
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>List of Records:</h2></div>
</div>
<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
<p class="bg-primary">Record ID {{$record->id}}</p>
<p class="bg-success">Respond ID {{$record->col1}}</p>
<p class="bg-info">Start Date {{$record->col2}}</p>
<p class="bg-warning">End Date {{$record->col3}}</p>
<p class="bg-danger">Better Consumer Tools {{$record->col4}}</p>
<p class="bg-primary">More public education {{$record->col5}}</p>
<p class="bg-success">Strengthen energy efficiency {{$record->col6}}</p>
<p class="bg-info">Phase out inefficient stuff {{$record->col7}}</p>
<p class="bg-warning">Demand program {{$record->col8}}</p>
<p class="bg-danger">Lower business needs {{$record->col9}}</p>
<p class="bg-primary">Stronger standards {{$record->col10}}</p>
<p class="bg-success">Created at {{$record->created_at->format('d/m/y H:i')}}</p>
<p class="bg-info">Edit|Delete<p>
<button type="button">Go back to Main</button>
</div>
</div>
</div>
</div>
</div>
#endsection
if I use JavaScript functions, how could the function handle the anchor tag to href route?
use this in your anchor tag
<a onclick="return confirm('Are you sure?')" href="deleteRec/{{$record->id}}">Delete</a>

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

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.

Resources