2

I'm passing value from laravel model to blade, am getting [object Object] as output. I want number or array.

Model:

public static function count($data){
  $post_id = $data->a;  
  $count_value=DB::select('select count(*) as count FROM pp_like WHERE  post_id= ?', [$post_id]); 
  return (array)$count_value[0];
}

blade-js:

res.success(function(data, status, headers, config) {
  alert(data);
});
3
  • 1
    I'm not sure what data argument represents. What you get if you replace alert(data); with alert(JSON.stringify(data));? Commented Aug 23, 2016 at 6:28
  • thaks bro its working. Commented Aug 23, 2016 at 6:32
  • If you find my help useful, consider upvoting and accepting my answer. Commented Aug 23, 2016 at 6:37

2 Answers 2

1

Your data argument is parsed JSON object. If you know the exactly name of the property you want to access, you can use data.property where property contains the value you are looking for. However, if you are not sure what data contains, you can replace alert(data); with alert(JSON.stringify(data)); to see the properties of your object.

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

Comments

0

change your query to

 $count =  DB::table('pp_like')->where('post_id','=',$post_id)->count();
 return $count;

now you will get number as result.

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.