Error partial not showing messages - laravel

The following code is in a partial named errors.blade.php:
#if ($errors->any())
<div class="form-group">
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
</div>
#endif
It's being called in master.blade.php:
<body>
#include("layouts.nav")
<div class="container-fluid">
<div class="row">
#include("layouts.sidebar")
#yield("content")
#include("layouts.errors")
</div>
</div>
#include("layouts.footer")
</body>
After submitting a form and using the validate() function, nothing is shown on the page that a user is redirected to. If I take the code from errors.blade.php and put it directly into the products.index.blade.php file, the errors show as intended. This is not desirable as I want errors and partials of a similar nature to be shown across all views if applicable.
Why aren't they working?

I see what mean and I do the same but instead of in master I only include it in the page that need it like form pages, login and registration.

Related

How to change which template is extended based on user authentication

I am using Laravel 6.20.11, and I have published the Laravel blade error templates. What I need to have happen is that if the user is authenticated, the error message extends the internal-only layout, and if not, it extends the public layout like so:
#auth
#extends ('int-layouts.partials.wrapper')
#section ('error')
<div class="be-content">
<div class="main-content container-fluid">
<div class="container py-5">
<div class="row">
<div class="col-lg-8">
<div class="text-block">
<h1>404 - Not Found</h1>
<p class="text-muted text-uppercase mb-4">Sorry, we couldn't find the page you were looking for.
Please contact the administrator<br/>
Or go back to the <a class="navbar-brand py-1" href="/controlpanel/">Dashboard</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#endauth
#guest
#extends ('ext-layouts.master')
#section('error')
<div class="container py-5">
<div class="row">
<div class="col-lg-8">
<div class="text-block">
<h1>404</h1>
<p class="text-muted text-uppercase mb-4">Sorry,You are trying to reach the page that does not exist.<br>
Please contact the administrator <br/> Or go back to the <a class="navbar-brand py-1" href="/">Home Page</a></p>
</div>
</div>
</div>
</div>
#endsection
#endguest
The problem I am having is that when I test the 404 error using abort(404) or by trying to access a route that doesn't exist, the error message displays twice and the page inherits the int-layouts.partials.wrapper even when the user isn't authenticated.
Is this expected behavior or am I doing something wrong in my blade file?
You can try to use a conditional in one #extends directive:
#extends (auth()->check() ? 'int-layouts.partials.wrapper' : 'ext-layouts.master')
Then you can use a conditional after this inside your section.
#section('error')
#auth
#else
#endif
#endsection
The #extends directive is not actually returning output directly into the compiled result like most directives do. The compiled PHP for this is held in an array to be added later.
Everything inside the ( ) of the directive is being used as a literal to call make on the View Factory:
"<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"
If you found the compiled view for this Blade template you would see this:
<?php echo \$__env->make(auth()->check ? 'int-layouts.partials.wrapper' : 'ext-layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
The first argument to make ends up being your view name when this compiled view is executed.
In Controller function:
$layouts = Auth::user->admin ? 'view.admin' : 'view.student'; //
view.admin or view.student is the blade file which you want to render
dynamically.
return view('welcome',compact('layouts'));
Write in Blade File:
#extends($layouts)
#section('...')
...
#endsection

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.

Display nothing when insert third record laravel

I'm developing a web app project of my client where people can ask there questions, like stack-overflow, I inserted 1 record manually into database it showed on the page and then I inserted 2nd record, it showed two records on the page but when i inserted 3rd record into database, nothing is showing on the page I checked the console there is no error there then I inspected the page inside body tag this is showing only <!----> I viewed the page source there code is displaying fine, then I removed the 3rd record everything is fine after that I inserted third record again the content of page gone again, this is my code of page...
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-sm-8 offset-md-2">
#foreach ($questions as $question)
<div class="card mb-2">
<div class="card-header">
<h5 class="bold">{{ $question->question_title }}</h5>
</div>
<div class="card-body">
<p class="card-text">{{ (strlen($question->question_body) > 50) ? substr($question->question_body, 0, 50) : $question->question_body }}</p>
Continue Reading
<p class="text-muted float-right">{{ date('M j, Y h:ia', strtotime($question->created_at)) }}</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
#endsection
and here is the QuestionController:
public function index()
{
$questions = Question::all();
return view('questions.index', compact('questions'));
}
To find out the issue, do this two things:
use dd() function in your controller and check the data
dd($questions)
check if there are any error in the Laravel log
storage/logs/laravel.log
Hope that help you find the problem and possible fix.
I think there is an issue with your html.Take your #foreach statement out and place it just before <div class="row">. If that works, you'll know that its in html.
Oh and adjust #endforeach accordingly.

Fetching errors on a page with multiple forms in Laravel

I am using Laravel 5.2 (although solutions for later versions are also okay).
I have a page, which contains BOTH the login page and registration page.
These forms use the AuthController as usual.
I display the errors like so:
#if (count($errors) > 0)
<div class="callout alert">
<strong>Whoops! Something went wrong!</strong>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
The issue is, the $errors variable does not make it clear which form the errors come from (i.e. Is it errors in the registration form or login form?).
How can I do this?
A way to handle this is to return flash messages. In your controller you can use something like:
For the login form
public function postLogin() {
// your code here
return redirect('/login')->with('login', 'Enter valid details');
}
For the sign up form
public function signUp() {
// your code here
return redirect('/login')->with('signup', 'SignUp has been successful');
}
And in order to display them in the view:
<div class="clearfix">
#if(Session::has('login'))
<div class="toast">
{{ Session::get('login') }}
</div>
#endif
</div>
<div class="clearfix">
#if(Session::has('signup'))
<div class="toast">
{{ Session::get('signup') }}
</div>
#endif
</div>

Laravel 5.0 paginate jump to page section

I'm using Laravel 5.0 and paginate along with the render() function in my view. Rather than go back to the top of the screen after I select the next page I want it to jump to #msgs.
Here is some of my code:
<h1 class="centered" id="msgs">Announcements</h1>
#foreach($messages->getCollection()->all() as $message)
<div class="row">
<div class="row">
<div class="col-sm-9">
<h3>{{$message->title}}</h3>
</div>
<div class="col-sm-3">
<h4>From: {{$message->author_name}}</h4>
</div>
</div>
<div class="row">
<p>{{$message->text}}</p>
</div>
</div>
<hr class="featurette-divider">
#endforeach
<div class="centered">
{!! $messages->render() !!}
</div>
I'm still getting a grasp on the flow of things in laravel so I'm not really sure what to do. I tried to use append->('#msgs') but realized that its appending an array in the parameters, not appending a path to the url. I there a simple way to achieve this without overriding anything?
You might want to use fragment() method:
{!! $messages->fragment('msgs')->render() !!}
Here is the link to the documentation: Pagination - Laravel

Resources