In my Laravel 8 app I created a component with a command :
php artisan make:component Admin/Auth/loggedUserHasPermissions
and I call it in my blade file as :
<x-admin.auth.logged-user-has-permissions :logged-user="getLoggedUser()" :show-only-icons="true"/>
In app/View/Components/Admin/Auth/loggedUserHasPermissions.php I dd 1 more variable hasAdminRole
which must be calculabe hasAdminRole :
<?php
namespace AppViewComponentsAdminAuth;
use IlluminateViewComponent;
class loggedUserHasPermissions extends Component
{
public $loggedUser;
public $showOnlyIcons= false;
public $hasAdminRole= false;
public function __construct( $loggedUser, bool $showOnlyIcons= false )
{
Log::info( '-1 $showOnlyIcons ::' . print_r( $showOnlyIcons, true ) );
echo '<pre>$showOnlyIcons::'.print_r($showOnlyIcons,true).'</pre>';
$this->loggedUser = $loggedUser;
$this->showOnlyIcons = $showOnlyIcons;
$this->hasAdminRole = false;
}
public function render()
{
Log::info( '-1 $showOnlyIcons ::' . print_r( $this->showOnlyIcons, true ) );
return view('components.admin.auth.logged-user-has-permissions');
}
}
and in resources/views/components/admin/auth/logged-user-has-permissions.blade.php
<div>
hasAdminRole::{{ $hasAdminRole }};;<br>
$showOnlyIcons::{{ $showOnlyIcons }};;<br>
resources/views/components/admin/auth/logged-user-has-permissions.blade.php000
But when I want to use hasAdminRole in component template as above, I got error :
and I got error :
Undefined variable: hasAdminRole (View: /mnt/_work_sdb8/wwwroot/lar/AdsBackend8/resources/views/components/admin/auth/logged-user-has-permissions.blade.php)
Var $showOnlyIcons is rendered ok, if line with $hasAdminRole is commented.
- Why error and which is the valid way ?
- Inside of __construct and render methods I have some loggin commands, but none of them is shown
in log file or on the screen. Why so? I need some debugging tools
MODIFIED BLOCK :
In the official docs there is a “Component Methods” block with isSelected example, but whe I tried to use
it in my component as in app/View/Components/Admin/Auth/loggedUserHasPermissions.php file :
public function isSelected($option)
{
return $option === $this->selected;
}
But trying to call it in the template :
$isSelected::{{ isSelected(123) }};;<br>
I goit error:
Call to undefined function isSelected()
Also i pay attention that in the docs example isSelected is called as var with “$” prefix :
<option {{ $isSelected($value) ? 'selected="selected"' : '' }} value="{{ $value }}">
Is it error in the docs?
Which way is valid ?
MODIFIED BLOCK 2:
I uploaded example ; https://github.com/sergeynilov/Lar8Test
Please, open welocome page and in the file
resources/views/components/admin/auth/logged-user-has-permissions.blade.php
Uncomment lines with $hasAdminRole and $isSelected - I got errors that these vars are not defined
I used command
php artisan make:component Admin/Auth/loggedUserHasPermissions
for component creation
Thanks!
question from:
https://stackoverflow.com/questions/65830842/i-failed-to-create-a-calculable-variable-in-component