Home > OS >  How to add data from controller to a blade file in laravel for table?
How to add data from controller to a blade file in laravel for table?

Time:04-05

I am trying to add data for a table from mysql to laravel blade file and the data is adding row by row, but i need the data to be entered in a single row. enter image description here

Here is my vscode(oneway.blade.php) file.I used for each and if else to display the data to the table. enter image description here.

I used foreach to get the data from RatesController.php file.enter image description here.

enter image description here

I need to display the rates as a single row like this.enter image description here

I tried to get all the data in a single row. But the data was adding one by one row wise. I used foreach to get the data from RatesController.php to my blade file oneway.blade.php file.

@foreach($rates as $rate)
@foreach($dummies as $dummy)

              if($dummy->passenger_category_name_start_value == $rate->passenger_category_name_start_value) 
    
                <td >{{$rate->places_id_start}}</td>
                <td >{{$rate->places_id_destination}}</td>
              
              
                  <td >
    
                  
                        @if ($rate->passenger_category_name_start_value == 1)
    
                          € {{$rate->rate}}
    
                        @elseif ($rate->passenger_category_name_end_value <= 3)
    
                          € {{$rate->rate}}
                            
                        @endif
                  
                  
                  </td>
                
              
                <td >
    
                  @if ($rate->passenger_category_name_start_value == 4)
    
                    € {{$rate->rate}}
    
                  @elseif ($rate->passenger_category_name_end_value <= 5)
    
                    €
                      
                  @endif
    
    
                </td>
    
                <td >
    
    
                  @if ($rate->passenger_category_name_start_value == 5)
    
                    € {{$rate->rate}}
    
                  @elseif ($rate->passenger_category_name_end_value <= 6)
    
                    €
                      
                  @endif
    
                </td>
    
                <td >
    
    
                  @if ($rate->passenger_category_name_start_value == 6)
    
                    € {{$rate->rate}}
    
                  @elseif ($rate->passenger_category_name_end_value <= 6)
    
                    €
                      
                  @endif          
    
    
                </td>
    
                <td >
    
    
                  @if ($rate->passenger_category_name_start_value == 7)
    
                    € {{$rate->rate}}
    
                  @elseif ($rate->passenger_category_name_end_value <= 7)
    
                    €
                      
                  @endif
    
    
                </td>
    
                <td >
    
    
                  @if ($rate->passenger_category_name_start_value == 8)
    
                    € {{$rate->rate}}
    
                  @elseif ($rate->passenger_category_name_end_value <= 9)
    
                    €
                      
                  @endif
    
    
                </td>
    
                <td >
    
    
                  @if ($rate->passenger_category_name_start_value == 9)
    
                    € {{$rate->rate}}
    
                  @elseif ($rate->passenger_category_name_end_value <= 10)
    
                    €
                      
                  @endif
    
    
                </td>
    
                <td >
    
    
                @if ($rate->passenger_category_name_start_value == 10)
    
                  € {{$rate->rate}}
    
                @elseif ($rate->passenger_category_name_end_value <= 11)
    
                  €
                    
                @endif
    
                <td >
    
    
                @if ($rate->passenger_category_name_start_value == 11)
    
                  € {{$rate->rate}}
    
                @elseif ($rate->passenger_category_name_end_value <= 12)
    
                  €
    
                @endif
    
    
                </td>
    
                <td >
    
    
                @if ($rate->passenger_category_name_start_value == 12)
    
                  € {{$rate->rate}}
    
                @elseif ($rate->passenger_category_name_end_value <= 13)
    
                  €
                    
                @endif
    
    
                </td>
    
                <td >
    
    
                @if ($rate->passenger_category_name_start_value == 13)
    
                  € {{$rate->rate}}
    
                @elseif ($rate->passenger_category_name_end_value <= 14)
    
                  €
                    
                @endif
    
    
                </td>
    
                <td >
    
    
                @if ($rate->passenger_category_name_start_value == 14)
    
                  € {{$rate->rate}}
    
                @elseif ($rate->passenger_category_name_end_value <= 15)
    
                  €
                    
                @endif
    
                </td>
    
                <td >
    
    
                @if ($rate->passenger_category_name_start_value == 15)
    
                  € {{$rate->rate}}
    
                @elseif ($rate->passenger_category_name_end_value <= 16)
    
                  €
                  
                @endif
    
    
                </td>
    
                <td >
    
    
                @if ($rate->passenger_category_name_start_value == 16)
    
                  € {{$rate->rate}}
    
                @else
    
                  €
                    
                @endif
    
    
                </td>
    
    
    
              </tr>
              
            @endforeach
          @endforeach

enter image description here

I used for each in controller RatesController.php to get from my backend file.

<?php

  
namespace App\\Http\\Controllers\\Frontend;  
use App\\Http\\Controllers\\Controller;

use App\\Models\\TourCategories;

use App\\Models\\PassengerCategories;

use App\\Models\\Rates;use App\\Models\\Place;

class RatesController extends Controller

{

    $rates =  Rates::where('deleted','=','0')->orderBy('rates.id')->get();
    
            // dd($rates);
    
            foreach($rates as $rate){
    
                $place = Place::where('id','=',$rate['places_id_start'])->first();
                $rate['places_id_start'] = $place['name'];
            }
    
            foreach($rates as $rate){
    
                $place = Place::where('id','=',$rate['places_id_destination'])->first();
                $rate['places_id_destination'] = $place['name'];
            }
    
            foreach($rates as $rate){
    
                $passenger_category = PassengerCategories::where('id','=',$rate['pasenger_categories_id'])->first();
                $rate['passenger_category_name_start_value'] = $passenger_category['start_value'];
            }
            foreach($rates as $rate){
    
                $passenger_category = PassengerCategories::where('id','=',$rate['pasenger_categories_id'])->first();
                $rate['passenger_category_name_end_value'] = $passenger_category['end_value'];
            }
            //dd($rates);
            //return $rates;
    
            $dummies =  Rates::where('deleted','=','0')->orderBy('rates.id')->get();
    
            // dd($rates);
    
            foreach($dummies as $dummy){
    
                $place = Place::where('id','=',$dummy['places_id_start'])->first();
                $dummy['places_id_start'] = $place['name'];
            }
    
            foreach($dummies as $dummy){
    
                $place = Place::where('id','=',$dummy['places_id_destination'])->first();
                $dummy['places_id_destination'] = $place['name'];
            }
    
            //dd($dummies);
            
    
            
    
    
            $tourCategories = TourCategories::where('active',1)->where('deleted',0)->get();
            return view('frontend.rates.oneway',compact('tourCategories','rates','dummies'));
    
            // dd($tourCategories);
        }
        public function paris(){
            $tourCategories = TourCategories::where('active',1)->where('deleted',0)->get();
            return view('frontend.transfers.paristransfers',compact('tourCategories'));
            //dd($tourCategories);
        }

CodePudding user response:

In oneway.blade.php on line 57 you need to prepend the if statement with an @

CodePudding user response:

Check this code:

@foreach($rates as $rate) @foreach($dummies as $dummy)

          @if($dummy->passenger_category_name_start_value == $rate->passenger_category_name_start_value) 

            <td >{{$rate->places_id_start}}</td>
            <td >{{$rate->places_id_destination}}</td>
          
          
              <td >

              
                    @if ($rate->passenger_category_name_start_value == 1)

                      € {{$rate->rate}}

                    @elseif ($rate->passenger_category_name_end_value <= 3)

                      € {{$rate->rate}}
                        
                    @endif
              
              
              </td>
            
          
            <td >

              @if ($rate->passenger_category_name_start_value == 4)

                € {{$rate->rate}}

              @elseif ($rate->passenger_category_name_end_value <= 5)

                €
                  
              @endif


            </td>

            <td >


              @if ($rate->passenger_category_name_start_value == 5)

                € {{$rate->rate}}

              @elseif ($rate->passenger_category_name_end_value <= 6)

                €
                  
              @endif

            </td>

            <td >


              @if ($rate->passenger_category_name_start_value == 6)

                € {{$rate->rate}}

              @elseif ($rate->passenger_category_name_end_value <= 6)

                €
                  
              @endif          


            </td>

            <td >


              @if ($rate->passenger_category_name_start_value == 7)

                € {{$rate->rate}}

              @elseif ($rate->passenger_category_name_end_value <= 7)

                €
                  
              @endif


            </td>

            <td >


              @if ($rate->passenger_category_name_start_value == 8)

                € {{$rate->rate}}

              @elseif ($rate->passenger_category_name_end_value <= 9)

                €
                  
              @endif


            </td>

            <td >


              @if ($rate->passenger_category_name_start_value == 9)

                € {{$rate->rate}}

              @elseif ($rate->passenger_category_name_end_value <= 10)

                €
                  
              @endif


            </td>

            <td >


            @if ($rate->passenger_category_name_start_value == 10)

              € {{$rate->rate}}

            @elseif ($rate->passenger_category_name_end_value <= 11)

              €
                
            @endif

            <td >


            @if ($rate->passenger_category_name_start_value == 11)

              € {{$rate->rate}}

            @elseif ($rate->passenger_category_name_end_value <= 12)

              €

            @endif


            </td>

            <td >


            @if ($rate->passenger_category_name_start_value == 12)

              € {{$rate->rate}}

            @elseif ($rate->passenger_category_name_end_value <= 13)

              €
                
            @endif


            </td>

            <td >


            @if ($rate->passenger_category_name_start_value == 13)

              € {{$rate->rate}}

            @elseif ($rate->passenger_category_name_end_value <= 14)

              €
                
            @endif


            </td>

            <td >


            @if ($rate->passenger_category_name_start_value == 14)

              € {{$rate->rate}}

            @elseif ($rate->passenger_category_name_end_value <= 15)

              €
                
            @endif

            </td>

            <td >


            @if ($rate->passenger_category_name_start_value == 15)

              € {{$rate->rate}}

            @elseif ($rate->passenger_category_name_end_value <= 16)

              €
              
            @endif


            </td>

            <td >


            @if ($rate->passenger_category_name_start_value == 16)

              € {{$rate->rate}}

            @else

              €
                
            @endif


            </td>



          </tr>
          
        @endforeach
      @endforeach
  • Related