doudao1282 2018-11-21 01:03
浏览 119

从laravel控制器返回json时,尝试获取非对象的属性“id”

I am trying to get the id of the latest record I stored to the database when using vue to submit a form. It's kind of a round about thing, but I can't seem to find a better solution.

After the record is stored I'm querying the database for this record. I get the id of the record from the database no problem when I go directly to route that handles this get request. The issue I have is when I attempt to pass this value back to the vue component, I don't get the id of the record as I expect, instead I get this in the laravel.log file:

Trying to get property 'id' of non-object

I got around this by checking if the value of the id is null and handle that accordingly, but the problem is I still can't get the id of the record which I need for associated a file with the post.

I've tried all the variations I can think of to access the data, such as: $id->id and $id['id']. I tried to turn it in to an associative array by creating a variable to hold the result of json_decode($id->id, true), but either I get the error of property of non-object or the variable is null. I can't seem to figure out what I'm doing wrong. Thank you in advance for any insight or help!

Here is all of the relevant code:

ActionLogComponent.vue

getLatestAction(company) {
          let url = '/latest-action/' + company;
          let id = 'error';

          axios.get(url)
            .then(response => {
              console.log(response);
              // id = response.data.id;
            })
            .catch(error => {
              console.log("error = " + error);
            });

          return id;
        },

ActionLogController.php

public function getLatestAction($company)
{
    // $userName      = Auth::user()->name;
    $userCompanyId = Auth::user()->company_id;
    $userCompany = Company::where('id', '=', $userCompanyId)->first(['name']);
    $companyViewingId = Company::where('name', '=' , $company)->first(['id']);

    if($userCompanyId == $companyViewingId->id)
    {
        $id = ActionLog::where('activity_key', '=', "1," . $companyViewingId->id)
                ->orderBy('created_at', 'desc')
                ->first(['id']);

        // outputs id as expected when going directly to the route
        // dd($id->id); 

        // returns hard coded integer as expected
        // return 3; 
        if(!is_null($id))
            return response()->json(['id' => $id->id], 200); //This is line 557
            // outputs json when going directly to the route and errors in component
        else
            return response()->json(['error' => 'No record found.'], 404);
            // This gets returned every time
    }

    // user is attempting to view something they don't have permission to see
    return response()->json(['error' => -1], 403);
    // this only gets returned if the user is not logged in or they are
    // attempting to force a different company name in the url
}

The console output is:

error = Error: Request failed with status code 404

laravel.log

[2018-11-21 00:02:31] development.ERROR: Trying to get property 'id' of non-object {"userId":1,"email":"frank@******.com","exception":"[object] (ErrorException(code: 0): Trying to get property 'id' of non-object at /Users/frankaddelia/Sites/ast_portal/app/Http/Controllers/ActionLogController.php:557)
  • 写回答

1条回答 默认 最新

  • douxu2081 2018-11-21 01:48
    关注

    I would change the ->first(['id']) to ->value('id')

    This will get the ID as an integer and allow for easy comparison and cut down your code.

    This would mean that you you would change $companyViewingId->id to $companyViewingId in our if statement and $id->id to $id in your json response

    Hope this helps

    Update

    I would use the url() or route() helper in creating the url for your get url

    If you want to use route() the you can use a place holder such as ':id' and the use:

    url = url.replace(':id', company)';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入