[英]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 ?我的问题是如何在mainview和childview中访问
$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.