-1

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:\wamp64\www\kont\resources\views\profile.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
Arkan Kalu
  • 403
  • 2
  • 4
  • 16

3 Answers3

1

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.

0

The DB::select returns an array of results. You should use

$data['dt'] = $podaciIzBaze[0];

However, a more elegant way of doing this is as follows:

$data['dt'] = UserModel::find($id);

The find method returns the first row of the model in which the primary key equals $id

JoeGalind
  • 3,545
  • 2
  • 29
  • 33
0

Number of friends: {{ $d }} should be Number of friends: {{ $d2 }}

I suggest the following changes to your code:

  1. Rename UserModel to just User
  2. Add Friend model
  3. Add eloquent relation of one to many between User model and Friend model
  4. Rename table name user_friend to user_friends - this way you can utilise laravel's magic
  5. dt and dt2 are not descriptive - I have no idea what they represent, I would suggest renaming them to: dt -> userData dt2 -> totalFriends
public function profile(User $user){
    $userData = $user->only([
        'first_name',
        'last_name',
        'email',
        'website',
        'birth_date',
    ]);

    return view('profile', [
        'userData' => $userData,
        'totalFriends' => $user->friends->count(),
    ]);

}

Blade file would then look similar to:

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


 Number of friends: {{ $totalFiends }}
Vlad Vladimir Hercules
  • 1,781
  • 2
  • 20
  • 37