在同一 table Laravel 5.6 中返回 parents 的 children 时在嵌套循环中遇到问题
Getting trouble in nested loop when returning children of parents in same table Laravel 5.6
我有一个 table account_heads
在我返回 children 的地方 parents 同样 table。到现在为止一切都很好,但是当我在表格中显示这些时遇到了问题。我有 4 或 5 个级别 children of parents 在相同的 table。
我的礼物 loop
就像下面这样-
@foreach ($accountHeads as $accountHead)
@foreach ($accountHead->children as $children)
<option value="{{ $children->id }}">{{ $children->name }} @if($children->code) ({{ $children->code }})@endif</option>
@foreach($children->children as $c1)
<option value="{{ $c1->id }}"> -- {{ $c1->name }} @if($c1->code) ({{ $c1->code }})@endif</option>
@foreach($c1->children as $c2)
<option value="{{ $c2->id }}"> ---- {{ $c2->name }} @if($c2->code) ({{ $c2->code }})@endif</option>
@foreach($c2->children as $c3)
<option value="{{ $c3->id }}"> ------ {{ $c3->name }} @if($c3->code) ({{ $c3->code }})@endif</option>
@foreach($c3->children as $c4)
<option value="{{ $c4->id }}"> -------- {{ $c4->name }} @if($c4->code) ({{ $c4->code }})@endif</option>
@foreach($c4->children as $c5)
<option value="{{ $c5->id }}"> ---------- {{ $c5->name }} @if($c5->code) ({{ $c5->code }})@endif</option>
@foreach($c5->children as $c6)
<option value="{{ $c6->id }}"> ------------ {{ $c6->name }} @if($c6->code) ({{ $c6->code }})@endif</option>
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
我想最小化上面的循环。
我想得到的结果就像下面这样-
我的数据库table结构如下-
我的账户AccountHeadsController
的create
方法如下-
public function create()
{
$accountHeads = AccountHead::with('children')->where('parent_id', 0)->get();
return view('account.account-head.create', compact('accountHeads'));
}
而AccountHead
模型如下-
class AccountHead extends Model
{
use SoftDeletes;
use Updater;
protected $table = 'account_heads';
protected $fillable = [
'name', 'code', 'memo', 'opening_balance', 'parent_id', 'status', 'created_by', 'updated_by', 'deleted_by'
];
public function parent()
{
return $this->belongsTo('App\Model\AccountHead','parent_id')->where('parent_id',0)->with('parent');
}
public function children()
{
return $this->hasMany('App\Model\AccountHead','parent_id')->with('children');
}
}
我自己是这样做的:
已创建帮助文件:App/Helpers/MyHelper.php
MyHelper.php 文件:
<?php
namespace App\Helpers;
class MyHelper
{
public static function dd($array, $step = 0)
{
$output = '';
foreach($array as $arr)
{
$output .= '<option value="'.$arr->id.'">'.str_repeat(' ',$step). $arr->name.'</option>';
if($arr->children)
{
$output .= self::dd($arr->children, $step+1);
}
}
return $output;
}
}
并将其添加到 /config/app.php 别名下。
'MyHelper' => App\Helpers\MyHelper::class,
那么在你看来你可以使用:
<select>
{!! MyHelper::dd($accountHeads) !!}
</select>
我有一个 table account_heads
在我返回 children 的地方 parents 同样 table。到现在为止一切都很好,但是当我在表格中显示这些时遇到了问题。我有 4 或 5 个级别 children of parents 在相同的 table。
我的礼物 loop
就像下面这样-
@foreach ($accountHeads as $accountHead)
@foreach ($accountHead->children as $children)
<option value="{{ $children->id }}">{{ $children->name }} @if($children->code) ({{ $children->code }})@endif</option>
@foreach($children->children as $c1)
<option value="{{ $c1->id }}"> -- {{ $c1->name }} @if($c1->code) ({{ $c1->code }})@endif</option>
@foreach($c1->children as $c2)
<option value="{{ $c2->id }}"> ---- {{ $c2->name }} @if($c2->code) ({{ $c2->code }})@endif</option>
@foreach($c2->children as $c3)
<option value="{{ $c3->id }}"> ------ {{ $c3->name }} @if($c3->code) ({{ $c3->code }})@endif</option>
@foreach($c3->children as $c4)
<option value="{{ $c4->id }}"> -------- {{ $c4->name }} @if($c4->code) ({{ $c4->code }})@endif</option>
@foreach($c4->children as $c5)
<option value="{{ $c5->id }}"> ---------- {{ $c5->name }} @if($c5->code) ({{ $c5->code }})@endif</option>
@foreach($c5->children as $c6)
<option value="{{ $c6->id }}"> ------------ {{ $c6->name }} @if($c6->code) ({{ $c6->code }})@endif</option>
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
@endforeach
我想最小化上面的循环。
我想得到的结果就像下面这样-
我的数据库table结构如下-
我的账户AccountHeadsController
的create
方法如下-
public function create()
{
$accountHeads = AccountHead::with('children')->where('parent_id', 0)->get();
return view('account.account-head.create', compact('accountHeads'));
}
而AccountHead
模型如下-
class AccountHead extends Model
{
use SoftDeletes;
use Updater;
protected $table = 'account_heads';
protected $fillable = [
'name', 'code', 'memo', 'opening_balance', 'parent_id', 'status', 'created_by', 'updated_by', 'deleted_by'
];
public function parent()
{
return $this->belongsTo('App\Model\AccountHead','parent_id')->where('parent_id',0)->with('parent');
}
public function children()
{
return $this->hasMany('App\Model\AccountHead','parent_id')->with('children');
}
}
我自己是这样做的:
已创建帮助文件:App/Helpers/MyHelper.php
MyHelper.php 文件:
<?php
namespace App\Helpers;
class MyHelper
{
public static function dd($array, $step = 0)
{
$output = '';
foreach($array as $arr)
{
$output .= '<option value="'.$arr->id.'">'.str_repeat(' ',$step). $arr->name.'</option>';
if($arr->children)
{
$output .= self::dd($arr->children, $step+1);
}
}
return $output;
}
}
并将其添加到 /config/app.php 别名下。
'MyHelper' => App\Helpers\MyHelper::class,
那么在你看来你可以使用:
<select>
{!! MyHelper::dd($accountHeads) !!}
</select>