【问题标题】:Laravel: Getting the Value of a Field In a TableLaravel:获取表中字段的值
【发布时间】:2014-05-04 02:47:31
【问题描述】:

我要做的是从 users 表和“credits”字段中获取可用积分,并在 foreach 循环中获取当前项目的价格,即“items”表中的“normalprice”字段。

当用户填写他想投入到一个项目的“正常价格”的金额时,我想从他的“信用”字段中的数字中减去,并为“这是价格”创建一个新变量在你把信用放在它上面之后的项目'并显示它。

用户表和项目表是多对多关系,我已经能够通过这样做来显示用户项目

$user->item

我所做的是下面的错误,因为 '$item->normalprice;' 中的 'normalprice'不是属性。我试图找出如何使 $itemprice 等于当前@foreach 中的“正常价格”的值($user->items as $item)

控制器:

    public function allocateCredit(){
    $validator = Validator::make(Input::all(),
    array(
        'puttoward' =>  'Integer',
    ));
    $user = Auth::user();
    $items  = Item::all();
    $userItems = Auth::user()->items;
    $itemprice = $item->normalprice;
    $available = $user->credits;
    $goodtogo = ($available > $itemprice) ? TRUE : FALSE;
    if($goodtogo === true){
        $howmuch = Input::get('puttoward');
        $newavailable =  $howmuch - $available;
        $itembalance = $itemprice - $howmuch;
        $user->credits = $newavailable;
    }
    if($user->save()){
        return Redirect::route('account')->with('global', 'Success.');
    }
}    

查看:

            @foreach ($user->items as $item)
        <div class="sep">
            <div class="nameIt">
                {{ $item->name }}
                <form action="{{ URL::route('allocate-credits-post') }}" method="post" role="form">
                    <div class="form-group">
                    <label for="exampleInputPassword1">Allocate credits</label>
                    <input type="intiger" class="form-control" name="puttoward" placeholder="$0.00">
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                    {{ Form::token() }}
                </form>

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    既然您使用表单提交来调用您的控制器,为什么不将 $item->normalprice 作为隐藏类型填充到表单中,例如:

    <input type="hidden" name="NormalPrice" value="{{$item->normalprice}}">
    

    然后使用 Input::get('NormalPrice')

    访问控制器中的值

    或者如果出于安全原因不希望这样做,那么您可以改为填充 $item->id 并在控制器中找出该 ID 的价格。

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      相关资源
      最近更新 更多