Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:获取表中字段的值_Php_Laravel_Laravel 4 - Fatal编程技术网

Php Laravel:获取表中字段的值

Php Laravel:获取表中字段的值,php,laravel,laravel-4,Php,Laravel,Laravel 4,我要做的是从users表和'credits'字段中获取可用信用,并在foreach循环中获取当前项目的价格,这是'items'表中的'normalprice'字段 当一个用户填写了他想要计入某个项目“正常价格”的金额时,我想从他的“积分”字段中的数字中减去,并为“这是您计入积分后的项目价格”创建一个新变量并显示它 users表和items表是多对多关系,我已经能够通过执行以下操作来显示用户项 $user->item 我所做的是bellow是错误的,因为“$item->normalpric

我要做的是从users表和'credits'字段中获取可用信用,并在foreach循环中获取当前项目的价格,这是'items'表中的'normalprice'字段

当一个用户填写了他想要计入某个项目“正常价格”的金额时,我想从他的“积分”字段中的数字中减去,并为“这是您计入积分后的项目价格”创建一个新变量并显示它

users表和items表是多对多关系,我已经能够通过执行以下操作来显示用户项

$user->item
我所做的是bellow是错误的,因为“$item->normalprice;”中的“normalprice”这不是一种财产。我试图找出如何使$itemprice等于当前@foreach($user->items as$item)中“normalprice”的值

控制器:

    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>
查看:

    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>
@foreach($user->items as$item)
{{$item->name}
分配学分
提交
{{Form::token()}}

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

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

然后使用Input::get('NormalPrice')访问控制器中的值。

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