首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel:将视图中的变量传递给控制器

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

Stack Overflow用户
提问于 2016-07-31 14:24:33
回答 3查看 122关注 0票数 1

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

怎么做?

shop.blade.php:

代码语言:javascript
运行
复制
<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:

代码语言:javascript
运行
复制
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!";
    }
}
EN

回答 3

Stack Overflow用户

发布于 2016-07-31 14:58:50

使用隐藏字段

代码语言:javascript
运行
复制
<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);...

票数 0
EN

Stack Overflow用户

发布于 2016-07-31 15:31:48

代码语言:javascript
运行
复制
<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');

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

票数 0
EN

Stack Overflow用户

发布于 2016-07-31 17:10:28

ShopController:

代码语言:javascript
运行
复制
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!";
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38684955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档