2

I am new in laravel

[{
    "id":1,
    "name":"demo",
    "slug":"demo",
    "status":1,
    "get_total_record":{
        "store_id":1,
        "counts":2
    }
}]

This is my record from database.

how can I print counts value.

I am using $get_total_record->counts, but it give error.

8
  • what you did, that you are getting Json string ? can you share your Methods/code ? Commented Feb 24, 2017 at 4:57
  • More than one record geting in json string, json string is ok but onlyy displaying problem. i don't know to how to print. i also use foreach() loop in view. Commented Feb 24, 2017 at 4:59
  • are you writing API ? please share your method through which you are fetching records from DB, eg: User::where('active',1)->get(); Commented Feb 24, 2017 at 5:03
  • Provide me, full query/method, you shared results, I am asking for query Commented Feb 24, 2017 at 5:05
  • @Qazi public function getTotalRecord() { return $this->hasOne('App\Post') ->selectRaw('store_id, count(*) as counts') ->where('status',1) ->groupBy('store_id'); } Commented Feb 24, 2017 at 5:05

4 Answers 4

1

you can do this. actually you are using Relationship and getting count from relationship variable, directly which in not possible an incorrect way, Follow this. share your result please

$store=new Store;
$storeList=Store::with('getTotalRecord')->get();

//Edited, Now you have to loop through the store list, this for your understanding

foreach($storelist as $store){
  echo "Count is: ".$store->getTotalRecord->count()."<br />";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are returning a variable from your controller. Let's call it $post. You can display like this.

{{ $post->get_total_record->counts }}

1 Comment

It's give error Trying to get property of non-object
0

If you have an instance of a model:

{{ json_decode($demo->get_total_record, true)['count'] }}

If you have a collection, you need to iterate over it:

@foreach ($demos as $demo)
    {{ json_decode($demo->get_total_record, true)['count'] }}
@endforeach

Alternatively, you could use casting for the get_total_record field:

protected $casts = [
    'get_total_record' => 'array',
];

And access data with:

{{ $demo->get_total_record['count'] }}

3 Comments

I am also use this but not print count value
@AnkitVirani what exactly solution do you use? If the first one, please post the result of {{ dd($demo->get_total_record) }}
I used both solution.
0

If it is already a json then say you saved it in variable $tests, try:

foreach($storeLists as $sl){
    if(isset($sl->get_total_record)){
        if(isset($sl->get_total_record->counts)){
            echo $sl->get_total_record->counts;
        }
    }
}

Comments

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.