2

I want to display data from the user table linked with the profile table. I get the data in my vardump but I don't know how to display it one for one. This is my function :

public function show($id)
{
    //Get data from user
    $user = User::find($id);
    //Get the horse count connect to the user
    $horse_count = DB::table('horses')->where('user_id', $id)->count();

    var_dump($user->profile);
    die();
}

It returns this :

object(Illuminate\Database\Eloquent\Collection)#188 (1) {
  ["items":protected]=>
  array(1) {
    [0]=>
    object(App\Profile)#189 (24) {
      ["connection":protected]=>
      NULL
      ["table":protected]=>
      NULL
      ["primaryKey":protected]=>
      string(2) "id"
      ["keyType":protected]=>
      string(3) "int"
      ["perPage":protected]=>
      int(15)
      ["incrementing"]=>
      bool(true)
      ["timestamps"]=>
      bool(true)
      ["attributes":protected]=>
      array(10) {
        ["id"]=>
        int(1)
        ["user_id"]=>
        int(2)
        ["country"]=>
        string(11) "Netherlands"
        ["age"]=>
        string(2) "21"
        ["recent_online"]=>
        string(0) ""
        ["visitors"]=>
        string(4) "2143"
        ["profile"]=>
        string(17) "Hoi ik ben Martin"
        ["remember_token"]=>
        NULL
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["original":protected]=>
      array(10) {
        ["id"]=>
        int(1)
        ["user_id"]=>
        int(2)
        ["country"]=>
        string(11) "Netherlands"
        ["age"]=>
        string(2) "21"
        ["recent_online"]=>
        string(0) ""
        ["visitors"]=>
        string(4) "2143"
        ["profile"]=>
        string(17) "Hoi ik ben Martin"
        ["remember_token"]=>
        NULL
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["relations":protected]=>
      array(0) {
      }
      ["hidden":protected]=>
      array(0) {
      }
      ["visible":protected]=>
      array(0) {
      }
      ["appends":protected]=>
      array(0) {
      }
      ["fillable":protected]=>
      array(0) {
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
      ["dates":protected]=>
      array(0) {
      }
      ["dateFormat":protected]=>
      NULL
      ["casts":protected]=>
      array(0) {
      }
      ["touches":protected]=>
      array(0) {
      }
      ["observables":protected]=>
      array(0) {
      }
      ["with":protected]=>
      array(0) {
      }
      ["morphClass":protected]=>
      NULL
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      }
  }
}

But when I echo $user->profile->age; it shows nothing. For info, I linked the tables user and profile with eachother with return $this->belongsTo('App\User'); and return $this->hasMany('App\Profile');. I get the data but I just can't display it one for one.

5
  • What is a profile in this context? Commented Jul 16, 2016 at 12:06
  • You may want to use eager loading in this case. Look at laravel documentation for more details. Commented Jul 16, 2016 at 12:06
  • Profile is a table linked with the id from user table Commented Jul 16, 2016 at 12:10
  • @ClearBoth checking it out now! Commented Jul 16, 2016 at 12:11
  • @ClearBoth that is exactly what I am doing but it is not working.. Commented Jul 16, 2016 at 12:12

1 Answer 1

3

One per one:

foreach (User::find($id)->profile as $profile) {
     // Do what you want, e.g.
     echo $profile->age;
}

Remember, if a user has many profiles then the relation function name should be plural like public function profiles()

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

2 Comments

It seems to work, but I don't understand how I get this to blade? Can you help me?
Of course! You pass your $user = User::find($id) -object to the view (e.g. return view('home.index', compact('user'));. And in your blade file you use the blade functions: @foreach ($user->profile as $profile) {{ $profile->age }} @endforeach

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.