condition does not work on blade laravel 5.2 - laravel

I want to load the panel if the user is logged in
and if the client is the master
But both files are loaded at runtime.
It does not run like a bet!
#if(Auth::check())
#extends('panel')
#else
#extends('master')
#endif

What do you want? To have different layouts for logged user and not logged one?
#extends(Auth::check() ? 'panel' : 'master')
You can't use two extends.
Two extends generate compiled views with code
<?php if(Auth::check()): ?>
<?php else: ?>
<?php endif; ?>
You can see, that extends are not here. But in the end of it -
<?php echo $__env->make('panel')... ->render();
<?php echo $__env->make('master')... ->render();
That's why use see them both.

Try use:
auth()->check(), \Auth::check(), or remove all views in /var/www/html/laravel-master/storage/framework/views/.
If Auth::check() does not work, use Illuminate\Support\Facades\Auth::check() instead:
#if(Illuminate\Support\Facades\Auth::check())
#extends('panel')
#else
#extends('master')
#endif

Edit your extends definition :
Try this
#extends(auth()->check() ? 'panel' : 'master')

You should try this:
#extends(isset(Auth::user()->id) ? 'panel' : 'master');

Related

Laravel - Route [companies] not defined

I developed a web application with Laravel-5.8 as shown below:
<?php
namespace App\Http\Controllers\Organization;
use App\Http\Controllers\Controller;
use App\Models\Organization\Company;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Exception;
class CompaniesController extends Controller
{
public function index()
{
$companies = Company::paginate(25);
return view('organization.companies.index', compact('companies'));
}
}
The CompaniesController is in a folder called Organization while the companies view is in a folder called organization.
routes/web.php
Route::get('/companies','CompaniesController#index');
navbar is as shown below:
<li class="nav-item">
<a href="{{ route('companies') }}l" class="nav-link">
<i class="nav-icon far fa-image"></i>
<p>
Company Info.
</p>
</a>
</li>
When I clicked on the navigation sidebar, I suppose to see the companies index being displayed but I got this error:
Route [companies] not defined
How do I resolve it?
Thanks
You're referencing an undefined named route. Try:
Route::get('/companies','CompaniesController#index')->name('companies');
This should solve your problem. You are having the error because you're referencing a named route which is not defined yet.
See the docs for more information.
Maybe you forgot to name your route?
Route::get('/companies','CompaniesController#index')->name('companies');
You forgot to name your route:
Route::get('/companies','CompaniesController#index')->name('companies');

Unable to Pass Variable from Controller to Drop Down

I'm trying to get data from a table called solutions and view the solution names in a dropdown list on a form called products.
I've created a provider called DynamicDropdown in App/Providers.
<?php
namespace App\Providers;
use App\Dropdown;
use Illuminate\Support\ServiceProvider;
class DynamicDropdown extends ServiceProvider
{
public function boot()
{
view()->composer('*', function ($view) {
$view->with('product_array', Dropdown::all());
});
}
}
Controller
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Providers\DynamicDropdown;
use App\Product;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
public function index(Request $request)
{
$select = [];
foreach ($view as $data) {
$select [$data->id] = $data->solutionname;
}
return view('products.products', compact('select'));
}
}
I've called the select variable in the view but unfortunately, I'm getting an undefined variable error.
Blade
<div class="form-group <?php echo e($errors->has('solution') ? 'has-error' : ''); ?>">
<?php echo Form::label('solution', 'Solution', ['class' => 'control-label']); ?>
<?php echo Form::select('solution', $select, null,
('' == 'required') ? ['class' => 'form-control', 'required' => 'required'] : ['class' => 'form-control']); ?>
<?php echo $errors->first('solution', '<p class="help-block">:message</p>'); ?>
</div>
Error Message is...
ErrorException thrown with message "Undefined variable: select (View:
E:\Laravel\IBMint\resources\views\products\products\form.blade.php)
I tried many ways to fix it. But still could not find a solution. Appreciate if someone could assist.
Thank You.
It is because you are actually calling the $select variable on the wrong form. You return a view
return view('products.products', compact('select'));
which means get the view products.blade.php on the folder products under the view folder.
and based on the error you put the $select variable on the form.blade.php.
Fix will be you return the view form.blade.php like this.
return view('products.products.form', compact('select'));

Laravel 5.4 Chaining to display Auth data

I have this in my master blade
<?php $user=auth()->user() ?>
{{ $user->id }}
Im using this code to display the ID of my user detail,
Can you suggest a way to remove <?php ?> or atleast a cleaner approach on this auth ?
Just use id() method:
{{ auth()->id() }}
If you need some other property or related data to display, use user() to get current user object:
{{ auth()->user()->name }}
Maybe you can do something like :
{{ auth()->user()->id }}
or if you have Laravel Auth
use Illuminate\Support\Facades\Auth;
Auth::id();
You can try above suggested answered as cleaner approach, but if you want to remove <?php ?>, you can try below code:
{{--*/ $user=auth()->user() /*--}}
I recommend to use above answered cleaner approach.
if you want to store user to variable initialize it on #php. For example
#php($user = auth()->user())
{{ $user->id }}

Check if session key exists in Laravel 5.1?

I'm trying to check it a session key has already been set inside a controller. The documentation states that it is possible to check if an item exists in an array and that's it.
http://laravel.com/docs/5.1/session
you can use
if($request->session()->has('key'))
{
}
Has pointed out by #DavidDomain probably the best way of doing it is to use
if(Session::has('...'))
Worked like a charm for me.
you can use Session::has('YOUR_SESSION_KEY') in both blade and controller
controller ex:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
class add_classController extends Controller
{
public function index(){
if (Session::has('YOUR_SESSION_KEY')){
// do some thing if the key is exist
}else{
//the key does not exist in the session
}
}
}
blade ex:
#if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}}
#else
{{-- session key dosen't exist --}}
#endif
You can do this
if(Session::has('your_key')){
return $next($request);
}
if($request->session()->exists('your_key'){
//
}
Or shorter, without using the Session Facade: if(session()->exists('key_here'))
This works for me
#if (session()->has('hasCoupon'))
[{{ Session::get('hasCoupon')['name'] }}]</span>
#endif
#if ( session::has('message')
{{ session::get('message') }}
#endif
or
#if (session->has->('message'))
{{session->
#endif
Try this new laravel syntax
#if(session()->has('success'))
//rest of code here
#endif

Zend fw, custom validation errors

I am creating a website with Zend_Form. In my controller, I assign a form object to the view. In the view, I use the following code to render the form:
<?php if ( isset( $this->success ) ): ?>
<div class="message success"><p>Thanks!</p></div>
<?php elseif ( sizeof( $this->form->getMessages( ) ) > 0 ): ?>
<div class="message success"><p>Something went wrong..</p></div>
<?php endif; ?>
<form>
<label>Name:</label>
<?php echo $this->form->name; ?>
<label>E-mail:</label>
<?php echo $this->form->name; ?>
</form>
Until now, this way of checking if there were form errors was good enough. But my client asked me if I could also specify the field that was not correct. So, for example: "Warning: you forgot to fill in your name". I've really got no idea on how to do this with Zend. Does anybody know where to start?
Thanks,
Martijn
You should be able to see it by requesting getErrors(); on the Zend_Form object.
It will return a array of errors where the key is the field name
returns something like this.
array(
'fieldname' => array('error1', 'error2'),
'username' => array('errors')
);
You could also call $form->getElements() and iterate over each element to see which one is throwing errors

Resources