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.3k views
in Technique[技术] by (71.8m points)

view - remove duplicate for only title laravel

In laravel 5.7 in my web controller

public function index(Request $request){

    $plan = Plan::All();

    return view('web.plan.index', compact('plan'));
}

In my view file

                   @foreach($plan as $singlePlan)
                   
                  @if(CarbonCarbon::parse($singlePlan->date)->format('m')==10)
                        
                        <div class="row brow bg-gray ">
                            <div class="col-md-12">
                                <div class="row-title">
                                    <strong>12.31</strong>
                                </div>
                            </div>
                        </div>
                         
            
                    
                    <div class="m-departures">
                        <div class="row brow last-row" >
                            <div class="col-md-2 col-sm-2 col hidden-xs">
                                <div class="td center">Dec 21-22</div>
                            </div>
                            <div class="col-md-5 col-sm-5 col">
                                <div class="td"><a href="#" target="_blank" rel="noopener noreferrer">Fest</a></div>
                            </div>
                            <div class="col-xs-6 col visible-xs">
                                <div class="td">2 day</div>
                            </div>
                            <div class="col-md-1 col-sm-2 col hidden-xs">
                                <div class="td">2 day</div>
                            </div>
                            <div class="col-md-2 col-sm-3 col-xs-6 col">
                                <div class="td">
                                    <span class="orange schedule-status">
                                         4                                        
                                    </span>          
                                                                  
                                </div>                                   
                            </div>                               
                            <div class="col-md-2 col-xs-12 col tour-link hidden-sm hidden-xs">
                                <div class="td center"><a href="#" >view</a></div>
                            </div>
                        </div>
                    </div>
                 
                        
                @endif
                      @endforeach

and my web look like enter image description here

but i want it to look like second one enter image description here

how can i remove duplicate title? in laravel


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

1 Answer

0 votes
by (71.8m points)

It would be better to group your data by the month before passing data to blade template. By using collection method mapToGroups https://laravel.com/docs/8.x/collections#method-maptogroups

$planGroups = Plan::get()->mapToGroups(function($plan, $key) {
    $planDate = CarbonCarbon::parse($plan->date);

    return ["{$planDate->month} {$planDate->year}" => $plan];
})
->all();

You will get several groups of plans grouped by month, e.g.

[
    '11 2020' => [plan, plan, ...],
    '12 2020' => [plan, plan, plan, ...]
]

Then in your blade template you can loop through the months then loop through the plans.

@foreach ($planGroups as $month => $plans)
    // show month heading

    @foreach ($plans as $plan)
        // show each plan
    @endforeach
@endforeach

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

...