1

I'm trying to get a single value from Query Builder in Laravel but the problem is I'm getting an array. this is my query result in Postman with dd($myVar):

[{"is_liked":1}]

and also with echo($myVar):

Collection {#976
  #items: array:1 [
    0 => NewsCommentLike {#970
      #fillable: array:3 [
        0 => "user_id"
        1 => "news_comment_id"
        2 => "is_liked"
      ]
      #connection: "mysql"
      #table: "news_comment_likes"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:1 [
        "is_liked" => 1
      ]
      #original: array:1 [
        "is_liked" => 1
      ]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
    }
  ]
}

and my code is:

$previous_is_liked = NewsCommentLike::query()->where(['user_id' => $user_id, 'news_comment_id' => $newsComment->id])->get(['is_liked']);

How to get a single value from is_liked not an array?

4
  • use first() method instead of get() Commented May 7, 2019 at 10:35
  • use first() then get() Commented May 7, 2019 at 10:36
  • This method didn't work. @jigs Commented May 7, 2019 at 10:43
  • This method didn't work and also I'm getting an array. @karan-sadana Commented May 7, 2019 at 10:46

2 Answers 2

4

This is why you have ->value('my_column') method. So you'll end up with:

NewsCommentLike::query()
    ->where(['user_id' => $user_id, 'news_comment_id' => $newsComment->id])
    ->value('is_liked');

The advantage is that the value is retrieved directly from the database. If you call first() before it, it can be null thus break your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Your suggested method also works. thank you. @oluwatobi-samuel-omisakin
2

$previous_is_liked = NewsCommentLike::query()->where(['user_id' => $user_id, 'news_comment_id' => $newsComment->id])->first()->is_liked;

Calling first returns 1 result instead of an array, then you can call the column you want.

I believe you could also call it like this:

$previous_is_liked = NewsCommentLike::query()->where(['user_id' => $user_id, 'news_comment_id' => $newsComment->id])->first()->value('is_liked')

2 Comments

Both of your proposed methods work. thanks a lot. @party-ring
No worries! The other answer has a good point about if ->first() returns a null value, you can do ->firstOrFail() so it will cause an exception if nothing is returned rather than the code 'breaking'. laravel.com/docs/5.8/eloquent#retrieving-single-models

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.