Laravel: Place for controller echo in blade view - php

First Laravel Project.
How to define a place in the view for the echo in controller?
I have sometimes "echoes" in my controller. For example if a MySQL statement has null output it echoes "MySQL select is empty" or after succesfull fileupload: "File upload is succesfull".
Right now it appears outside of the body tag at the top left corner of the screen. I want to place it inside the main section or a modall.

You want to redirect back with errors
return redirect()->back()->withErrors(['mysql' => 'MySQL select is empty']);
And then get it in the view (modal)
#if($errors->any())
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
#endif
You can watch this laracast for more about errors: https://laracasts.com/series/laravel-from-scratch-2017/episodes/12

Related

foreach a blade view component with dynamic data

i am trying to loop over blade component by passing an array data. it successfully compiled but it is not showing up in the browser and also not giving an error
here is card.blade.php file
<div>
<h1>{{$name}}</h1>
<h2>{{$age}}</h2>
<h3>{{$work}}</h3>
</div>
here is main file view file
#php
$arr = [
['name'=>'1','age'=>'2','work'=>'3'],
['name'=>'4','age'=>'5','work'=>'6']
];
#endphp
#foreach ($arr as $de)
<x-card :name={{$de['name']}} :age={{$de['age']}} :work={{$de['work']}}/>
#endforeach
it also shows attached data when i click view source file
<x-card :name=1 :age=2 :work=3/>
<x-card :name=4 :age=5 :work=6/>
but i is not showing up in the browser, browser is blank
You just need to delete {{}} syntax from the attributes
<x-card :name="$de['name']" :age="$de['age']" :work="$de['work']"/>
for more info check the Component-Attributes from the docs

Laravel blade `#auth` directive not working as expected

I am trying to pick the master blade template dynamically as per the current user roll logged in. (here it should go to the 'shopowner' auth block)
#auth('shopmanager')
#extends('theme::Admins.shopmanager.layout.master')
#endauth
#auth('shopowner')
#extends('theme::Admins.shopowner.layout.master')
#endauth
but this always gives error as it tries to compile the 'shopmanager' master template. It is not going into the 'shopmanager' #auth block because it's not printing anything if I print inside that block.
It only works if I completely comment out that line.
P.S.:
This is the master theme::Admins.shopmanager.layout.master template file
which must not be loaded.
#extends('theme::Admins.outline.layout.master')
#include('theme::Admins.shopmanager.layout.common.header')
#include('theme::Admins.shopmanager.layout.common.left-sidebar') // The error throws from inside this view.
#include('theme::Admins.shopmanager.layout.common.footer')
#section('title-head', __('Shop Manager'))
I can wrap the #auth check around #include lines but the point is, this complete file should be skipped from the compilation.
SOLVED
As per my learning, #extend(...) will always be compiled regardless of outer wrap conditions. so must be moved to dynamic variable based blocks.
#auth('shopmanager')
#php
$masterTemplate = 'theme::Admins.shopmanager.layout.master';
#endphp
#endauth
#auth('shopowner')
#php
$masterTemplate = 'theme::Admins.shopowner.layout.master';
#endphp
#endauth
#extends($masterTemplate)
Try below code ,i hope this ans help you:
#if(Auth::check())
#if(Auth::user()->role=='shopmanager')
#extends('theme::Admins.shopmanager.layout.master')
#else
#extends('theme::Admins.shopowner.layout.master')
#endif
#endif
If you have multi-authentication that time try below :
#if(Auth::guard('shopmanager')->user())
#extends('')
#else
#extends('')
#endif
In my case, I'm handling manager, customer and admin with this code.
Manager : Auth::guard('manager')->user()
Admin : Auth::user()
Customer : Auth::guard('customer')->user()

Noob Q: Laravel 5.4 - redirect()->with()

I'm currently learning Laravel 5.4. I'm following a beginner's tutorial (we're on the same Laravel version).
In the video tutorial, the guy uses the following line of code in a controller:
return redirect()->route('posts.index')->with('error','Unauthorised!');
We both have the following in the view:
#if(count($errors))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{ $error }}
</div>
#endforeach
#endif
The code works perfectly fine for the tutor on screen, however it doesn't for me - it redirects but doesn't pass the errors.
I used the following modified code in my controller and it worked:
return redirect()->route('posts.index')->withErrors(['error'=>'Unauthorised!']);
In order for me to learn, I need to know why the original code works for him - but not me? Like I said previously, we're both using the same version of Laravel.
Can anyone explain why?
I haven't seen the video, so can't say to as why his does work.
1.
return redirect()->route('posts.index')->with('error','Unauthorised!');
You redirect to the posts.index with a variable called error, and you check for a variable called errors so you could do it like this instead
#if(count($error))
<div class="alert alert-danger">
{{ $error }}
</div>
#endif
2.
You could support multiple errors like you other example and you could do it like
return redirect()->route('posts.index')->withErrors(['Unauthorised!', 'error_2', 'error_3', 'etc']);
and then you can loop through them as in your own first example (You don't need the keys in the array, but you can have them if you feel for it)
#if(count($errors))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{ $error }}
</div>
#endforeach
#endif
3.
A third option would also be to use flash sessions, which is a session that only lives for the next request
than you could do
return redirect()->route('posts.index')->session()->flash('error', 'Unauthorised!');
and in your views
#if(Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error') }}
</div>
#endif
which I personally prefer as it gives me the option to just include the if statement in my "main layout file", and it will be shown on all the including pages.

Using Sessions in Blade shows blank page

I have this in my blade file:
#if(Session::has('logged'))
#include('layouts.usernav')
#else
#include('layouts.publicnav')
#endif
When I run my page, it is just a blank page.
When I change it to look like this:
#if(1 == 1)
#include('layouts.usernav')
#else
#include('layouts.publicnav')
#endif
My page gets displayed. I don't know what I am doing wrong. What would/could be causing this?

Laravel 5. Translate errors from forms

I want to translate form errors to other language.
I created language folder: resources/lang/lt and insinde validation.php file with translated text.
In template show errors like this:
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
But every time I get errors from resources/lang/en/validation.php file, maybe I have somehow use trans function insinde errors loop?
For translation I use mcamara/laravel-localization package. Everything works except errors.

Categories