vendredi 11 décembre 2015

Laravel Modeling join three Table in single line

i have 3 table/Model such as Users, CurrentCurrency and CurrencyType, in CurrentCurrency 2 column are relation with CurrencyType and Users, as user_id and currency_id

i can use this code to fetch CurrentCurrency user :

$all_records = CurrentCurrency::with('user')->orderBy('id', 'DESC')->paginate(50);

this code return all records with users, now i want to create simple related with CurrencyType by Modeling, unfortunately for this table i get null

CurrentCurrency :

class CurrentCurrency extends Model
{
    protected $table = 'current_currency';
    protected $fillable = ['currency_id', 'current_money', 'user_id'];
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function currency_type()
    {
        return $this->belongsTo('App\CurrencyType');
    }
}

CurrencyType:

class CurrencyType extends Model
{
    protected $table = 'currency_type';
    protected $fillable = ['currency_type', 'currency_symbol', 'user_id'];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

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

User:

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

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * @param $value
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function currentCurrency()
    {
        return $this->belongsToMany('User', 'CurrentCurrency', 'user_id', 'currency_id');
    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function currencyType()
    {
        return $this->hasMany('App\CurrencyType');
    }
}

By this code i can get user information:

$all_records = CurrentCurrency::with(['user', 'currency_type'])->orderBy('id', 'DESC')->paginate(50);

foreach ($all_records as $key => $contents) {
    echo $contents->user;
}

But i can not get currency_type, thats return null

enter image description here

RESULT:

{"id":3,"current_money":"333","user_id":1,
"created_at":"\u0622\u0630\u0631 20\u060c 1394",
"updated_at":"\u0622\u0630\u0631 20\u060c 1394",
"currency_id":1,"user":{"id":1,"name":"\u0645\u0647\u062f\u06cc",
"family":"\u067e\u06cc\u0634\u06af\u0648\u06cc","username":"mahdi","token":"",
"email":"pishguy@gmail.com",
"image_file_name":"","mobile_number":"09373036569",
"status":1,"created_at":"\u0622\u0630\u0631 20\u060c 1394",
"updated_at":"\u0622\u0630\u0631 20\u060c 1394"},
"currency_type":null}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire