Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

laravel - I failed to create a calculable variable in component

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.

  1. Why error and which is the valid way ?
  2. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Component name should be in PascalCase. If you check the document https://laravel.com/docs/8.x/blade#components, component name of all the examples is in PascalCase. So, you should replace loggedUserHasPermissions with LoggedUserHasPermissions everywhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...