1

I've got a User model as below:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    public function recruits()
    {
        return $this->hasMany('App\User','recruiters_id');
    }
    public function recruiter()
    {
        return $this->belongsTo('App\User','recruiters_id');
    }
}

There is a 'recruiters_id' field in the 'users' table, which was set up with:

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->foreign('recruiters_id')->references('id')->on('users');
        });
    }

So the idea is that users can recruit other users. But when I try something like {{$user->recruiter->id()}} I get the "Trying to get property of non-object" error

If I do {{print_r($user->recruiter)}} instead I get the output below, which does look like an object (to my inexperienced eyes at least, although it does seem like the "object" is repeated a few times even though I only use print_r once):

App\User Object ( [table:protected] => users [fillable:protected] => Array ( [0] => review_count [1] => review_sum [2] => review_avg [3] => display_name [4] => business_name [5] => setting1 [6] => referrer_id [7] => referrer_margin [8] => gender [9] => age [10] => title [11] => points [12] => credits [13] => bankacct_nr [14] => bankacct_name [15] => bsb [16] => save_payment [17] => newsletter [18] => first_name [19] => last_name [20] => phone [21] => email [22] => password [23] => subject [24] => credentials [25] => margin [26] => user_type [27] => url [28] => questions [29] => form_subject [30] => postcode [31] => recruiters_id [32] => internal_notes ) [hidden:protected] => Array ( [0] => password [1] => remember_token ) [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 240 [first_name] => Jane [last_name] => Recruit [title] => [phone] => [email] => [email protected] [user_type] => recruiter [margin] => 0.00 [credentials] => [password] => $2y$10$eTSX9vRGFr5eupAL/kyexeARVDjT.M/7xE2Yp.6PMTuH/dOsFd6QS [remember_token] => [created_at] => 2016-07-06 15:19:23 [updated_at] => 2016-07-06 15:19:23 [status] => [url] => [newsletter] => 0 [postcode] => [form_subjects] => [questions] => [save_payment] => 0 [bsb] => [bankacct_nr] => [bankacct_name] => [points] => 0 [credits] => 0 [gender] => [age] => 0 [referrer_id] => [referrer_margin] => 0.00 [setting1] => [business_name] => [display_name] => i recruit [following] => [followers] => [belts_id] => [recruiters_id] => [internal_notes] => [review_count] => 0 [review_sum] => 0 [review_avg] => 0.00 ) [original:protected] => Array ( [id] => 240 [first_name] => Jane [last_name] => Recruit [title] => [phone] => [email] => [email protected] [user_type] => recruiter [margin] => 0.00 [credentials] => [password] => $2y$10$eTSX9vRGFr5eupAL/kyexeARVDjT.M/7xE2Yp.6PMTuH/dOsFd6QS [remember_token] => [created_at] => 2016-07-06 15:19:23 [updated_at] => 2016-07-06 15:19:23 [status] => [url] => [newsletter] => 0 [postcode] => [form_subjects] => [questions] => [save_payment] => 0 [bsb] => [bankacct_nr] => [bankacct_name] => [points] => 0 [credits] => 0 [gender] => [age] => 0 [referrer_id] => [referrer_margin] => 0.00 [setting1] => [business_name] => [display_name] => i recruit [following] => [followers] => [belts_id] => [recruiters_id] => [internal_notes] => [review_count] => 0 [review_sum] => 0 [review_avg] => 0.00 ) [relations:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [dateFormat:protected] => [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] => ) 1 

This is my first time setting up Eloquent relationships within the same table, so maybe I made some mistakes there. Using Laravel 5.1

Any advice would be very much appreciated, thanks in advance

1
  • Can you provide your user table migration code ( the full ) Commented Jul 7, 2016 at 7:14

1 Answer 1

2

Without having the full migration I'd say the problem lies here

public function recruits()
{
    return $this->hasMany('App\User','recruiters_id');
}
public function recruiter()
{
    return $this->belongsTo('App\User','recruiters_id');
}

For Both relations your give recruites_id as foreign key, even though it's id recruits.

for hasMany you should have

return $this->hasMany('App\User', 'foreign_key', 'local_key');

Same for belongsTo

return $this->belongsTo('App\User', 'foreign_key', 'other_key');

Therefore

public function recruits()
{
    return $this->hasMany('App\User','recruiters_id', 'id');
}
public function recruiter()
{
    return $this->belongsTo('App\User');
}

Should be what you are looking for ( IF recruiters_id is correctly defined in your migration )

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

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.