2

When I echo this:

<?php echo $photo->votesCount ?>

I get

[{"photo_id":1,"votes":2}]

How do I display the votes (2)?

Here's a vardump of the object: http://pastebin.com/ddAkRk4c

If I type:

<?php print_r( $photo->votesCount )?>

I get this: http://pastebin.com/ZfE76WmB

The complete view:

2   @foreach($photos as $photo)                                                        
3   <li class='photo'>
4     <img src="{{URL::to('/')}}/uploads/{{$photo->name}}" width="150">                
5     <div>
6       <a href="{{route('photos.show',$photo->id)}}">{{ $photo->name }}</a>
7       <?php $photo_array = json_decode($photo);
8       echo $photo_array->votes; ?>
9     </div>
10   </li>
11   @endforeach

Part of the Controller logic:

 $photos = Photo::where('period_id', $currentPeriod)
                   ->with('votesCount')
                   ->get();
 return view('home', ['uploaded' => $uploaded, 'photos' => $photos, 'period' => $currentPeriod]);

Model:

  public function votes()
  {
      return $this->hasMany('App\Vote');
  }

  public function votesCount()
  {           
    return $this->votes()
      ->selectRaw('photo_id, count(*) as votes')
      ->groupBy('photo_id');
  }
Legionar
  • 7,472
  • 2
  • 41
  • 70
Alexander
  • 396
  • 1
  • 9
  • 19

8 Answers8

3

There is no need for your second method votesCount. You have defined a hasMany relationship for the votes and if you query them you only get the votes for the photo-id.

To get the count of the votes do the following in your view:

$photo->votes->count();

And your controller should look like this:

$photos = Photo::where('period_id', $currentPeriod)
                   ->with('votes')
                   ->get();
Tim
  • 5,893
  • 3
  • 35
  • 64
  • Is there an easy way to sort the photos by vote count ? Maybe in the query? – Alexander Oct 27 '15 at 12:36
  • Take a look at this answer on how to modify your collection like you described: http://stackoverflow.com/a/24208979/261713 – Tim Oct 27 '15 at 12:42
0

echo like this

<?php echo $photos->votesCount->votes ?>
Imtiaz Pabel
  • 5,307
  • 1
  • 19
  • 24
0
<?php echo $photos->votesCount->votes ?>

but you can also write

{{ echo $photos->votesCount->votes; }}
cre8
  • 13,012
  • 8
  • 37
  • 61
0

Try this:

Since it is a JSON array json_decode the array and echo the value

$photo_array = json_decode($photos->votesCount);
echo $photo_array['votes'];

Hope this is helpful.

ArtisanBay
  • 1,001
  • 11
  • 16
0

Your $photos->votesCount returns a string, you have to convert it to an array like so:

$str = $photos->votesCount //gets the string
$arr = json_decode($str, true)[0]; //convert it to an array
echo $arr['votes'];

OR as an object

$str = $photos->votesCount //gets the string
$obj = json_decode($str)[0]; //convert it to an object
echo $obj->votes;
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
0

i succesfully tested like this and had it echo 2:

$str = '[{"photo_id":1,"votes":2}]';
$arr = json_decode($str, true); //convert it to an array
echo $arr[0]["votes"];

so it shoud work for you like this

$str = '$photo->votesCount';
$arr = json_decode($str, true); //convert it to an array
echo $arr[0]["votes"];

if this doesnt work try to change the first line to

$str = (string)$photo->votesCount;
Silencio
  • 175
  • 8
0

Since it is a JSON array, json_decode the array and echo the value:

$photo_array = json_decode($photos->votesCount, true);
echo $photo_array[0]['votes'];

Don't forget to add second attribute true in json_decode - refer to manual.

So the whole code:

<?php
  foreach ($photos as $photo) {
    echo "<li class='photo'>";
      echo "<img src='{{URL::to(\'/\')}}/uploads/{{$photo->name}}' width='150'>";
      echo "<div>";
        echo "<a href='{{route(\'photos.show\', $photo->id)}}'>{{ $photo->name }}</a>";
        $photo_array = json_decode($photo, true);
        echo $photo_array[0]['votes'];
      echo "</div>";
    echo "</li>";
  }
?>
Legionar
  • 7,472
  • 2
  • 41
  • 70
  • I think, there is one more index with 0. What you get when you insert between that two lines this: `print_r($photo_array);`? – Legionar Oct 27 '15 at 11:45
  • Array ( [0] => Array ( [photo_id] => 1 [votes] => 2 ) ) – Alexander Oct 27 '15 at 11:47
  • Undefined offset: 0. Also there's a foreach so I'm changing $photos to $photo in your code (and in every other that answered here :P) – Alexander Oct 27 '15 at 11:49
  • If `print_r` returned that, then `$photo_array[0]['votes']` will work... you must have error elsewhere. – Legionar Oct 27 '15 at 11:50
  • If I remove that part of the code then my view loads normally without showing the vote count. You can come on my teamviewer if you don't believe me :) – Alexander Oct 27 '15 at 11:53
  • You had another error there: `@foreach($photos as photo)` has to be `@foreach($photos as $photo)` !! – Legionar Oct 27 '15 at 11:53
  • I don't think I typed that anywhere? :( I just copied your whole code block and I got this: Undefined offset: 0 – Alexander Oct 27 '15 at 11:56
  • I still get Undefined offset: 0 – Alexander Oct 27 '15 at 11:59
  • Of course you get, because in your question you wrote wrong `$photo` and `$photos`, now I can see that... – Legionar Oct 27 '15 at 12:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93486/discussion-between-legionar-and-lextoc). – Legionar Oct 27 '15 at 12:02
-1

The votesCount is an array. So just echo it like this

<?php echo $photos->votesCount['votes'] ?>
Toan Tran
  • 54
  • 3