如何显示多个条目laravel 4

时间:2014-10-14 12:56:51

标签: php laravel laravel-4

如何显示新插入的条目?每当我尝试同时提交2个或更多条目,并将其显示在我的刀片上时,它只会显示我插入的最后一个条目。这里似乎有什么问题?

Controller.php这样

     public function onewayflightshow()
   {  
      $children=Session::get('children');
      $adult=Session::get('adult');
      $infant=Session::get('infant');
      $to=Session::get('destinationto');
      $from=Session::get('destinationfrom');
      $dept=Session::get('departure');
      $show = DB::table('oneways')->get();
        foreach ($show as $user)
        {  
            $adultFee = ($user->fare)*$adult;
            /*------------------------Child Fee------------------------*/
            $partialFee1 = ($user->fare)*.05;
            $partialFee2 = ($user->fare)-$partialFee1;
            $childFee = $partialFee2*$children;
            /*------------------------Infant Fee------------------------*/
            $partialFee3 = ($user->fare)*.10;
            $partialFee4 = ($user->fare)-$partialFee3;
            $infantFee = $partialFee2*$infant;
        }  
            $payment = ($adultFee+$childFee+$infantFee);

           $rules = array(
          'title'         => 'required',             
          'lastname'      => 'required',  
          'email'         => 'required',
          'cemail'        => 'required|same:email',    
          'firstname'     => 'required',
          'middlename'    => 'required',
          'birthday'      => 'required',
          'city'          => 'required',
          'streetadd'     => 'required',
          'zipcode'       => 'required|max:4',
          'country'       => 'required',
          'home'          => 'required|max:7',
          'mobile'        => 'required|max:12'
        );

      $validator = Validator::make(Input::all(), $rules);
      if ($validator->fails()) {

        $messages = $validator->messages();
        $guestcount= $children+$adult+$infant;
        return View::make('content.onewayflightfillup')->withErrors($validator)->with(['gcount'=>$guestcount]);
      }

      else{
          $c = count(Input::get('title'));

          $titles = Input::get('title');
          $lastnames = Input::get('lastname');
          $firstnames = Input::get('firstname');
          $middlenames = Input::get('middlename');
          $birthdays = Input::get('birthday');
          $citys = Input::get('city');
          $streetadds = Input::get('streetadd');
          $zipcodes = Input::get('zipcode');
          $countrys = Input::get('country');
          $homes = Input::get('home');
          $works = Input::get('work');
          $faxs = Input::get('fax');
          $mobiles = Input::get('mobile');
          $emails = Input::get('email');

          for( $i=0; $i < $c; ++$i ) {
            $reserve = new Reserves;
            $reserve->title = $titles[$i];
            $reserve->lastname = $lastnames[$i];
            $reserve->firstname= $firstnames[$i];
            $reserve->middlename = $middlenames[$i];
            $reserve->birthday = $birthdays[$i];
            $reserve->city = $citys[$i];
            $reserve->streetadd = $streetadds[$i];
            $reserve->zipcode = $zipcodes[$i];
            $reserve->country = $countrys[$i]; 
            $reserve->home = $homes[$i];
            $reserve->work = $works[$i];
            $reserve->fax = $faxs[$i];
            $reserve->mobile = $mobiles[$i];
            $reserve->email = $emails[$i];
            $reserve->children = $children;
            $reserve->adult = $adult;
            $reserve->infant = $infant;
            $reserve->destinationfrom = $from;
            $reserve->destinationto = $to;
            $reserve->departure = $dept;
            $reserve->payment = $payment;

            $reserve->save();  

        }
           $reserves = $reserve->id;
           $res = DB::table('reserves')->where('id', '=' ,$reserves)->get();  
           return View::make('content.onewayflightshow2')->with(['showing'=>$res]);
      }}

blade.php

@foreach($showing as $row)
        <tr>
            <td>Guest:</td><b> 
            <td>{{$row -> firstname}}</td>
            <td>{{$row -> middlename}}</td>
            <td>{{$row -> lastname}}</td></b>
        </tr><br>
        <tr>
            <td>Birthday:</td>
            <td>{{$row -> birthday}}</td>
        </tr><br>
        <tr>
            <td>Address:</td>
            <td>{{$row -> streetadd}}</td>,
            <td>{{$row -> city}}</td></tr>,
            <td>{{$row -> country}}</td> :
            <td>{{$row -> zipcode}}</td>
        <tr><br>
        <tr>
            <td>Contact Details:</td><br>
            <ul>
              <li>Home: {{$row -> home}}</li>
              <li>Work: {{$row -> work}}</li>
              <li>Fax: {{$row -> fax}}</li>
              <li>Mobile: {{$row -> mobile}}</li>
              <li>Email: {{$row -> email}}</li>
            </ul>  
        <tr><br>
        <tr>
            <td>Boarding with a # of</td><br>
            <ul>
              <li><b>{{$row -> children}}</b> Children</li>
              <li><b>{{$row -> adult}}</b> Adult/s</li>
              <li><b>{{$row -> infant}}</b> Infant/s</li>
            </ul> 
        </tr><br>
        <tr>
            <td>Enroute:</td>
            <td>from <b>{{$row -> destinationfrom}}</b> to <b>{{$row -> destinationto}}</b></td><br>
            <td>Departure Date:</td>
            <td><b>{{$row -> departure}}</b></td>
        <tr><br>
        <tr>
           <td>Total Expense: </td>
           <td>&#x20b1;<b>{{$row -> payment}}.00</b></td></tr>
@endforeach

1 个答案:

答案 0 :(得分:2)

你应该改变:

  for( $i=0; $i < $c; ++$i ) {
    $reserve = new Reserves;
    // ...
    $reserve->payment = $payment;

    $reserve->save();  

}

成:

  $resIds = [];
  for( $i=0; $i < $c; ++$i ) {
    $reserve = new Reserves;
    // ...
    $reserve->payment = $payment;

    $reserve->save();  
    $resIds[] = $reserve->id;

}

并改变:

$res = DB::table('reserves')->where('id', '=' ,$reserves)->get();  

成:

$res = DB::table('reserves')->whereIn('id', $resIds)->get();