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

时间:2014-10-07 11:49:31

标签: php mysql laravel laravel-4 eloquent

我对一切都相当新,我想知道是否可以将声明的变量存储到数据库中?

例如(这些放在 nameoffile controller.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');
 }}

我正在尝试save();我的数据库中的所有内容,从会话变量到用户输入的值,但问题是我不知道如何将$payment插入数据库表中。它甚至可能吗?如果是的话,这样做的方式是什么?

3 个答案:

答案 0 :(得分:1)

您需要使用以下内容将其放入数据库:

$reserve->payment = $payment;

但当然在您的表格中,您需要包含名称为payment

的列

显然这个:

  $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');

没有工作。在每列中,您只能插入一个数据。

答案 1 :(得分:0)

 <?php
class Test extends Eloquent
{
 public function insert()
{
 $a=1;
$b=2;
$c=$a+$b;
$data = array('column_1'=>$a , 'column_2'=>$b , 'column_3'=>$c);
$query = DB::table('table_name')->insert($data);
}
}
?>

这是一个扩展雄辩的ORM的模型函数。在控制器中调用此模型函数

答案 2 :(得分:0)

class TestController extends BaseController
{
 public function test()
{
 $data = Modelname::ModelFunction();   /* if the model function is static */
/* else you need to create an object for that class */
$t = new Modelname();
$data = $t->ModelFunction();
}
}