D
D
Dmitry Kuznetsov2016-04-09 18:59:03
Laravel
Dmitry Kuznetsov, 2016-04-09 18:59:03

Why doesn't hasOne work?

There are 2 tables:
1. users
id_users
login_users
...
2. money
id
username
balance
Users model:

namespace App\Models\Users;
use Illuminate\Database\Eloquent\Model;

class Users extends Model{
    protected $table = 'users';
    protected $primaryKey = 'id_users';

    protected $guarded = ['id_users'];
    protected $hidden = ['password'];

    public function getMoney(){
        return $this->hasOne('App\Models\Users\Money', 'username', 'login_users');
    }
}

money:
namespace App\Models\Users;
use Illuminate\Database\Eloquent\Model;

class Money extends Model{
    protected $table = 'money';
}

I want to get the user's balance through hasOne, and in the form I output: "$user->getMoney()->balance".
$user is the user data received via the view (in the controller).
PS: I tried using the standard User::find(1) method... nothing changed.
It seems to work, but for some reason it doesn’t get a fig from the database.
What's wrong? Thanks in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-04-09
@dima9595

Because the connection needs to be taken like this:
When using $user->getMoney(), we get the Builder, not the result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question