Skip to content
Advertisement

Printing number of rows in Laravel

I am trying to print number of rows in Laravel from MySQL but i get this error: htmlspecialchars() expects parameter 1 to be string, object given (View: C:wamp64wwwkontresourcesviewsprofile.blade.php)

I have two function inside my model. First function returns information about user from one table and another function counts number of friends for that user from another table. Please view my code below:

HomeController.php

public function profile(Request $request,$id){
        $model = new UserModel();
 $podaciIzBaze = $model->userdata($id);
 $data = array();
 $data['dt'] = $podaciIzBaze;

$model2 = new UserModel();
 $podaciIzBaze2 = $model2->countfriends($id);

 $data['dt2'] = $podaciIzBaze2;

         return view('profile',$data);

    }

UserModel.php

  class UserModel extends Model
    {
        //
        public function userdata($id){
            $query = DB::select("SELECT first_name,last_name,email,website,birth_date FROM users where id = $id");
            return $query;
        }

        public function countfriends($id){

            $query=DB::SELECT("SELECT count(user_id) from user_friend where user_id = $id");
            return $query;

        }

profile.blade.php

 @foreach($dt as $d)
                                 <tr>
                                    <td>Ime:</td>
                                    <td>{{ $d->first_name }}</td>
                                 </tr>
                                 <tr>
                                    <td>Prezime</td>
                                    <td>{{ $d->last_name }}</td>
                                 </tr>
                                 <tr>
                                    <td>Datum Rodjenja</td>
                                    <td>{{ $d->birth_date }}</td>
                                 </tr>
                                 <tr>
                                 <tr>
                                    <td>Pol</td>
                                    <td>Musko</td>
                                 </tr>
                                 <tr>
                                    <td>Email</td>
                                    <td><a href="mailto:info@support.com">{{ $d->email }}</a></td>
                                 </tr>
                                 <td>Website</td>
                                 <td>
                                   {{ $d->website }}
                                 </td>
                                 </tr>
                                     @endforeach


                       @foreach($dt2 as $d2)
                     Number of friends: {{ $d }}
                     @endforeach

Advertisement

Answer

In the second line

@foreach($dt2 as $d2)
    Number of friends: {{ $d }}  // Here it should be $d2
@endforeach

It should be $d2 instead of $d.

Edit 1

Problem is with your second function countfriends($id). Use following code

$query=DB::select("SELECT count(user_id) as friends from user_friend where user_id = $id");

Then in view file use:

@foreach($dt2 as $d2)
    Number of friends: {{ $d2->friends }}
@endforeach

When you were using select query it was returning the key “count(user_id)” which could not be used with standard object to print.

But you should start using Eloquent for queries instead of writing manual SQL queries.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement