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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行