简体   繁体   English

如何在刀片模板的另一个视图中访问变量值?

[英]How to access a variable value in another view in blade template?

My Views Code in blade template is like this:我在刀片模板中的视图代码是这样的:

Views/Mainview.blade.php视图/Mainview.blade.php

@extends('layouts.app')
@section('content')
    @php 
        $baalgh = array();
    @endphp
    @include('tabs.familyview')
    <?php print_r($baalgh); //prints empty array ?>
    @include('tabs.childview') //need $baalgh value inside this view
@endsection

Views/tabs/familyview.blade.php视图/选项卡/familyview.blade.php

@foreach($membersdata as $key=>$onemember)
    @php 
        $age = calculateage($onemember->memberdob);
        $onemember->age = $age;
        ((18 <=> $age) === 1)?:$baalgh[] = $onemember;
    @endphp
@endforeach
<?php print_r($baalgh); //here it displays data ?>

my question is how to access values of $baalgh in mainview and childview ?我的问题是如何在mainviewchildview访问$baalgh的值?

Your code seems a bit unorganized, you should try to keep the calculations inside your controller, and use the views as the presentation of your calculated data.您的代码似乎有点杂乱无章,您应该尝试将计算保存在 controller 中,并使用视图作为计算数据的表示。 I suppose you can do something like this:我想你可以这样做:

In your controller:在您的 controller 中:

$baalgh = $membersdata->filter(function($total, $member){
    $member->age = calculateage($member->memberdob);
    return $member->age <=> 18;
});

return view("Mainview.blade.php", ["baalgh" => $baalgh, "membersdata" => $membersdata]);

Then in your Views/Mainview.blade.php :然后在您的Views/Mainview.blade.php

@extends('layouts.app')
@section('content')
    @include('tabs.familyview')
    @include('tabs.childview')
@endsection

And on your Views/tabs/familyview.blade.php :在您的Views/tabs/familyview.blade.php

<?php print_r($baalgh); //here it displays data ?>

It is much simpler this way.这种方式要简单得多。

Just follow simple steps for include array into child view:只需按照简单的步骤将数组包含到子视图中:

Views/Mainview.blade.php视图/Mainview.blade.php

@extends('layouts.app')
    @section('content')
    @php 
        $baalgh = array();
    @endphp
    @include('tabs.familyview', ['baalgh' => $baalgh])
    {{ var_dump($baalgh) }} //prints empty array

@endsection

Views/tabs/familyview.blade.php视图/选项卡/familyview.blade.php

@foreach($membersdata as $key=>$onemember)
    @php 
        $age = calculateage($onemember->memberdob);
        $onemember->age = $age;
        ((18 <=> $age) === 1)?:$baalgh[] = $onemember;
    @endphp
@endforeach
<?php print_r($baalgh);

 @include('tabs.childview', ['baalgh' => $baalgh])

Views/tabs/childview.blade.php视图/选项卡/childview.blade.php

@foreach ($baalgh as $baal)
    {{$baal}} // $baalgh array is updated.
@endforeach

I hope it will help.我希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Laravel 5的刀片模板中创建变量以供部分视图使用 - How to create a variable in a blade template in laravel 5 to be used by a partial view 如何将数组变量(刀片模板)传递给CodeIgniter视图? - How to pass Array Variable (Blade Template) to CodeIgniter View? 如何将值从视图传递到laravel刀片中的另一个视图 - How to pass value from view to another view in laravel blade 如何将 DB 中的内爆值分配到 Laravel 刀片模板视图中? - How to assign imploded value from DB into Laravel blade template view? 如何从一个刀片视图重定向到另一个刀片视图,并将动态生成的变量传递给它? - How to redirect from one blade view to another blade view, and pass dynamically generated variable to it? Laravel - 如何在视图(刀片)中传递值等于输入值的变量? - Laravel - how to pass variable that value is equal to input value in view(blade)? 从包含在 php 刀片模板中访问变量 - Access variable from include in blade template in php 在扩展刀片中声明变量,并在另一个刀片中获取值 - Declare a variable in the extended blade and get the value in another blade Laravel 8 – Blade 模板 – 从另一个模板文件读取变量 - Laravel 8 – Blade template – read variable from another template file 如何在laravel中使用数据库作为带有刀片模板的变量? - How To use Database as a variable with blade template in laravel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM