Laravel:将视图中的变量传递给控制器​​

时间:2016-07-31 14:24:33

标签: php laravel laravel-5

我想将两个变量从视图中的表单传递给我的控制器并在那里使用它们......

怎么做?

shop.blade.php:

<div class="mcitem">
                    <img src="/images/shop/Stein.png" alt="Stein">
                    <div class="mcunits">
                        {{ Form::open(array('action' => 'ShopController@add', $itemid, $bprice)) }}
                        {{ Form::number('units', '0') }}
                    </div>
                    <div class="mcbuy">
                        $itemid = 1;
                        $bprice = 3;
                        {{ Form::submit('3,00 Coins', array('name' => 'buybtn')) }}
                    </div>
                    <div class="mcsell">
                        {{ Form::submit('1,00 Coin', array('name' => 'sellbtn', $itemid='1', $sprice=1)) }}
                        {{ Form::close() }}
                    </div>
                </div>

ShopController:

public function add(Request $request){
    $this->validate($request, [
        'units' => 'required|min:1',
    ]);
    if(Input::Get('buybtn')) {
        $this->Buy(); //if Buy Button is pushed
    } elseif(Input::Get('sellbtn')) {
        $this->Sell(); //if Sell Button is pushed
    }
}

public function Buy(){
    $username = Auth::user()->name;
    $units = Input::Get('units');
    if((DB::table('users')->where('name', $username)->value('kontostand')) >= ($bprice*$units)){
        $check_entry = DB::table($username)->where('Item', '=', $itemid)->first();
        if(is_null($check_entry)){
            $hunits = DB::table($username)->where('Item', $itemid)->select('units')->get();
            DB::table($username)->where('Item', $itemid)->update([$itemid => $hunits + $units]);
        }
        else{
            DB::table($username)->where('Item', $itemid)->insert(
                [$itemid => $units]
            );
        }
    }
    else{
        echo "Zu wenig Geld auf dem Kontostand!";
    }
}

3 个答案:

答案 0 :(得分:0)

使用隐藏字段

<div class="mcitem">
    <img src="/images/shop/Stein.png" alt="Stein">
    {!! Form::open(array('action' => 'ShopController@add')) !!}
    <div class="mcunits">
        {!! Form::number('units', '0') !!}
    </div>
    <div class="mcbuy">
        {!! Form::hidden('itemid', '1') !!}
        {!! Form::hidden('bprice', '3') !!}
        {!! Form::submit('3,00 Coins', array('name' => 'buybtn')) !!}
    </div>
    <div class="mcsell">
        {!! Form::submit('1,00 Coin', array('name' => 'sellbtn')) !!}
    </div>
    {!! Form::close() !!}
</div>

控制器

$request->get('itemid); ...

答案 1 :(得分:0)

<div class="mcunits">
    {{ Form::open(array('action' => 'ShopController@add')) }}
    {{ Form::number('units', '0') }}
    {{ Form::hidden('itemid', 1) }}
    {{ Form::hidden('bprice', 3) }}
</div>
<div class="mcbuy">
    {{ Form::submit('3,00 Coins', array('name' => 'buybtn')) }}
</div>

$itemid = Input::Get('itemid');
$bprice = Input::Get('bprice');

解决以下错误:不支持的操作数类型

答案 2 :(得分:0)

ShopController:

public function add(Request $request){
    $this->validate($request, [
        'units' => 'required|min:1',
    ]);
    if(Input::Get('buybtn')) {
        $this->Buy(); //if Buy Button is pushed
    } elseif(Input::Get('sellbtn')) {
        $this->Sell(); //if Sell Button is pushed
    }
    return view('shop');
}

public function Buy(){
    $username = Auth::user()->name;
    $units = Input::Get('units');
    $itemid = Input::Get('itemid');
    $bprice = Input::Get('bprice');
    if((DB::table('users')->where('name', $username)->value('kontostand')) >= ($bprice*$units)){
        $check_entry = DB::table($username)->where('Item', '=', $itemid)->first();
        if(is_null($check_entry)){  
            //$hunits = DB::table($username)->where('Item', $itemid)->select('units')->first(); 
            //$hunits=(is_null($hunits))?0:$hunits->units; 
            //DB::table($username)->where('Item', $itemid)->update(['units' => (int)$hunits + $units]);


            $hunits = DB::table($username)->where('Item', $itemid)->select('units')->get();
            DB::table($username)->where('Item', $itemid)->update(['units' => (int)$hunits + $units]);
        }
        else{
            DB::table($username)->insert(['Item' => $itemid, 'units' => $units]);
        }
    }
    else{
        echo "Zu wenig Geld auf dem Kontostand!";
    }

}