【问题标题】:get the attributes from a post request on a Laravel API从 Laravel API 上的 post 请求中获取属性
【发布时间】:2019-05-13 02:53:18
【问题描述】:

我有一个用于发布请求的路由,在控制器中我想知道我在发布请求中发送了哪些值,我是 Laravel 的新手。我该怎么做?

这是我的模型的代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OMeta extends Model
{
    protected $fillable= ['n_bandera','h_entrada','h_salida','locacion','fecha','ticket','so'];
}

我想从 post 请求中获取 'n_bandera' 属性。我怎么能在以下控制器功能中做到这一点:

public function store(Request $request){
    // get the 'n_bandera' attribute from the $request

}

另外,我不想使用all() 之类的方法,因为我希望请求中的内容转到数据库中的不同表。

【问题讨论】:

标签: php laravel rest post request


【解决方案1】:
public function store(Request $request) {
    /* your form needs to have an input field name as n_bandera*/
    $n_bandera = $request->n_bandera;
}

【讨论】:

    【解决方案2】:
    public function store(Request $request){
         $requestData = $request->all();
         $n_bandera = $requestData['n_bandera'];
    
      /* your form input field name */
    
    
    } 
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:
      public function store(Request $request){
          //get the 'n_bandera' attribute from the $request
          $n_bandera = $request->get('n_bandera');
      }
      

      【讨论】:

        【解决方案4】:

        您只需通过不同的方式在控制器方法中检索表单数据:

        如果你的表单字段名称是 n_bandera 那么控制器方法应该是这样的

        use Illuminate\Http\Request;
        public function store(Request $request){
          $n_bandera = $request->get('n_bandera'); //use get method in request object 
          OR 
         $n_bandera = $request->input('n_bandera'); //use input method in request object
          OR 
         $n_bandera = $request->n_bandera;   //use dynamic property 
        }
        

        【讨论】:

          【解决方案5】:

          为此,您可以使用 request->only() 而不是 $request->all(),如下所示:

          ModelOne::create($request->only('model_one_param_one'));
          ModelTwo::create($request->only('model_two_param_one','model_two_param_two));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-16
            • 1970-01-01
            • 2018-10-29
            • 2017-11-26
            • 2019-02-16
            • 2018-09-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多