向控制器请求方法视图在Laravel中不起作用?

时间:2019-02-14 07:46:29

标签: php laravel

我从walletrefer的表单中获得$request->wallet$request->refer的价值,我仅得到钱包的价值,而不是引用路径为{{1} }。我还添加了csrf,但无法正常工作

控制器代码

POST route

查看:

$acc['wallet'] = $request->wallet;
$acc['balance'] = 0;
$acc['uqid'] = rand(10000000,99999999);
$acc['ref'] = $request->refer;
$ck = Account::where('uqid', $acc['uqid'])->first();

if(isset($ck))
{
     $acc['uqid'] = rand(10000000,99999999);
}
Account::create($acc);

Ajax:

@if(session('CurrentAccount')=='')
<form id="checkForm">
   @csrf
   <input type="text" class="wallet-input" name="wallet" placeholder="Your Address...">

   <input type="text" class="wallet-input" name="refer" value="{{ app('request')->input('ref') }}">
   <button type="submit">Enter</button>
</form>
@endif

3 个答案:

答案 0 :(得分:1)

似乎您的value属性ID无法从请求中获取正确的值。

具体检查{{ app('request')->input('ref') }}是否在中得到任何值: <input type="text" class="wallet-input" name="refer" value="{{ app('request')->input('ref') }}">

第二个在控制器内部:

$account = Account::create([
    'wallet' = $request->input('wallet'),
    'balance' = 0,
    'uqid' = rand(10000000,99999999),
    'ref' = $request->input('refer'),
    'uqid' = Account::where('uqid', $acc['uqid'])->exists() ? rand(10000000,99999999) : null

]);

(假设您在$fillable =[]模型中的Account数组中有这些列)

登录浏览器的network tab,查看要发布的数据。如果没有输入值,则控制器将无法获得任何信息。

答案 1 :(得分:0)

代替

$request->wallet;
$request->refer;

更改

$request->get('wallet');
$request->get('refer');

答案 2 :(得分:0)

当您需要创建uuid时,请使用laravel Str :: uuid()中提供的帮助函数,而无需查询数据库以确保它不存在以前。

尤其是可以使用您现在拥有的代码仍然将其复制

您没有定义新的帐户对象。

$acc = new App\Account;
$acc->wallet = $request->wallet;
$acc->balance = 0;
$acc->uqid = Str::uuid();
$acc->ref = $request->refer;

$acc->save();

以及表单方法和动作未定义:

@if(session('CurrentAccount')=='')
<form id="checkForm" action="{{ route('method/route') }}" method="POST">
   @csrf
   <input type="text" class="wallet-input" name="wallet" placeholder="Your Address...">

   <input type="text" class="wallet-input" name="refer" value="{{ app('request')->input('ref') }}">
   <button type="submit">Enter</button>
</form>
@endif