【问题标题】:On get request why do I get back the blade view, when I should get data from the database instead?在获取请求时,为什么我应该取回刀片视图,而不是从数据库中获取数据?
【发布时间】:2019-09-15 17:51:25
【问题描述】:

我有以下 get 请求,它在 mounted() 上执行。 当我明确地从 database 请求一些数据时,我以一些奇怪而神秘的方式返回我的主视图 app.blade 作为响应。

有人能看出我搞砸了什么吗?

我在前端的get request

mounted() {
this.getProjectRequests();
},
methods: {
getProjectRequests: function() {
  var self = this;
  let clientId = this.$route.path.substring(
    this.$route.path.lastIndexOf("/") + 1
  );
  axios({
    method: "get",
    url: "/get-project-requests/" + clientId
  })
    .then(function(response) {
      console.log(response);
    })
    .catch(function(error) {
      console.log(error);
      // TODO error handling
    });
}
}

我的路线

Route::get('/get-project-requests/{client_id}', 
'SinglePageController@getProjectRequests');

还有我的控制器方法

public function getProjectRequests($clientId) {
  try {
         $projectRequests = ProjectRequest::where('client_id', 
         $clientId)->value('name');
         return response()->json( [
                    'success'=> true,
                    'projectRequests' => $projectRequests
                ]);
            } catch(\Exception $e){
                return ['success' => false, 'message' => 'getting 
                project requests failed'];
            }
        }

【问题讨论】:

    标签: laravel vue.js get axios


    【解决方案1】:

    我认为这 ProjectRequest::where('client_id', $clientId)->value('name'); 给出了例外。

    您可以检查 storage/logs 文件夹中的 laravel.log 或将该方法更改为

        // Not working on eloquent model
        $valueOject = ProjectRequest::where('client_id',$clientId)->value('name');
    
        // DB facade its working. Change to this method
        $valueOject = DB::table('{your_table}')->where('client_id', $clientId)->value('name');
    
        dd($valueOject);
    

    【讨论】:

    • 它不会抛出异常,它只是返回我唯一的视图作为响应,就好像我在请求return view('app');
    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    相关资源
    最近更新 更多