我想将两个变量从视图中的表单传递给我的控制器并在那里使用它们......
怎么做?
<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>
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!";
}
}
答案 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!";
}
}