A
A
Alexey Verkhovtsev2017-07-14 19:16:18
Laravel
Alexey Verkhovtsev, 2017-07-14 19:16:18

How to bind models in Laravel 5.1?

Hello. There are 2 models: User (the table is called users), customerMfOptions (the table is called customer_mail_forwarding_informations). I get a user

$current_user = $this->user->where('email', $email)->where('role_id', 3)->first();

But I need to get more data from customerMfOptions (it has a customer_id column that is associated with the User table by id)
I tried to write the following lines in the User model
public function customermfoptions()
    {
        return $this->hasOne('App\Models\CustomerMfOptions', 'customer_id', 'id');
    }

And then in the code after this line
$current_user = $this->user->where('email', $email)->where('role_id', 3)->first();

I write like this But null comes up, what am I doing wrong? How to extract data. If there is a way for me, it would be possible to pull them out separately into a variable and the method as into one collection with the user. Thanks in advance! :)
dd(\App\User::find(1)->customermfoptions);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
giroskop grospop, 2017-07-15
@Limo

I think the point is that you do not have protected $table set in App\Models\CustomerMfOptions
As far as I remember, the default table names are not formed by camel-cas, so you need to set this property in CustomerMfOptions.
protected $table = 'customerMfOptions';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question