dsfdfd1211 2014-10-07 11:49
浏览 63
已采纳

可以在数据库Laravel 4中插入声明的变量吗?

I am fairly new to everything and I am wondering if it is possible to store declared variables in to database?

For example(these are placed in the nameoffilecontroller.php),

public function onewayflightshow()
   {  $a=Session::get('children');
      $b=Session::get('adult');
      $c=Session::get('infant');
      $d=Session::get('destinationto');
      $e=Session::get('destinationfrom');
      $f=Session::get('departure');

      $results = DB::table('oneways')->get();
          if (!empty($results))
            foreach ($results as $user)
            {  
                $adultFee = ($user->fare)*$b;
                /*------------------------Child Fee------------------------*/
                $partialFee1 = ($user->fare)*.05;
                $partialFee2 = ($user->fare)-$partialFee1;
                $childFee = $partialFee2*$a;
                /*------------------------Infant Fee------------------------*/
                $partialFee3 = ($user->fare)*.10;
                $partialFee4 = ($user->fare)-$partialFee3;
                $infantFee = $partialFee2*$c;

                $payment = ($adultFee+$childFee+$infantFee);
                var_dump($payment);
              }


       $rules = array(
        'title'         => 'required',             
        'lastname'      => 'required',  
        'email'         => 'required|email',
        '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();
      return View::make('content.onewayflightfillup')->withErrors($validator);

    } else {

      $reserve = new Reserves;
      $reserve->title = Input::get('title');
      $reserve->lastname = Input::get('lastname');
      $reserve->firstname= Input::get('firstname');
      $reserve->middlename = Input::get('middlename');
      $reserve->birthday = Input::get('birthday');
      $reserve->city = Input::get('city');
      $reserve->streetadd = Input::get('streetadd');
      $reserve->zipcode = Input::get('zipcode');
      $reserve->country = Input::get('country');
      $reserve->home = Input::get('home');
      $reserve->work = Input::get('work');
      $reserve->fax = Input::get('fax');
      $reserve->mobile = Input::get('mobile');
      $reserve->email = Input::get('email');
      $reserve->children = Session::get('children');
      $reserve->children = Session::get('adult');
      $reserve->children = Session::get('infant');
      $reserve->children = Session::get('destinationfrom');
      $reserve->children = Session::get('destinationto');
      $reserve->children = Session::get('departure');
      $reserve->$payment; <-- IS THIS EVEN POSSIBLE?

      $reserve->save();
      var_dump($reserve);
      return View::make('content.onewayflightbooklist');
 }}

I am trying to save(); everything in my database, from the session variables to the inputed values from the user but the problem is I don't know how to insert $payment into the database table. Is it even possible? If yes, what are the way/s of doing it?

  • 写回答

3条回答 默认 最新

  • dton37910 2014-10-07 12:07
    关注

    You need to put it into database using:

    $reserve->payment = $payment;
    

    but of course in your table you need to have column with name payment

    And obviously this:

      $reserve->children = Session::get('children');
      $reserve->children = Session::get('adult');
      $reserve->children = Session::get('infant');
      $reserve->children = Session::get('destinationfrom');
      $reserve->children = Session::get('destinationto');
      $reserve->children = Session::get('departure');
    

    won't work. In each column you can insert only one data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程