首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将javascript的结果作为数量传递给laravel函数?

如何将javascript的结果作为数量传递给laravel函数?
EN

Stack Overflow用户
提问于 2022-07-13 14:00:14
回答 1查看 43关注 0票数 1

当我做console.log(结果)时,我想将这个结果保存为amount;我看到它知道我输入了什么数字,但是如何将它保存在Laravel函数中?

代码语言:javascript
复制
 function makeOffer(nftid) {
                    swal({
                        title: "Do you want to make offer?",
                        text: "Enter amount",
                        input: 'text',
                        type: 'warning',
                        showCancelButton: true,
                        showConfirmButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                    }).then((result) => {
                        if (result) {
    
                            axios.post("/myaccount/makeoffer/" + nftid).then(response => {
                                window.location.reload();
                            });
                        }
                    });
                }
    
    public function makeOffer($id, Request $request){
    
            $nft=NFT::where('id','=',$id)->first();
            if($nft->status=='pending') {
                $nft_auction = new NftAuctions();
                $nft_auction->nft_id = $nft->id;
                $nft_auction->owner_id = $nft->user->id;
                $nft_auction->buyer_id = Auth::id();
                $nft_auction->amount = "there should be amount";
                $nft_auction->status = 'pending';
                $nft_auction->save();
                return back();
            }
            else{
                abort(404);
            }
            
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-13 14:11:59

Axios的.post()方法采用两个参数;URL和要发送到后端的数据,因此将其调整为:

代码语言:javascript
复制
axios.post("/myaccount/makeoffer/" + nftid, {'amount': result})
.then(response => {
  window.location.reload();
});

然后,在后端,您可以以$request->input('amount')的形式访问它。

代码语言:javascript
复制
public function makeOffer($id, Request $request){
  $nft = NFT::find($id);

  if($nft->status == 'pending') {
    $nftAuction = new NftAuctions();
    // ...
    $nftAuction->amount = $request->input('amount');
    // ...
    $nftAuction->save();

    return back();
  }  
}

一些注意事项:

  1. Model::where('id', '=', $id)->first()可缩短为Model::find($id)
  2. 模型名为PascalCase和单数:NFT应该是NftNftAuctions应该是NftAuction

文档:

方法

拉威尔:检索输入

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

https://stackoverflow.com/questions/72967605

复制
相关文章

相似问题

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