0

I am trying to make a one-to-many relationship, but I get the following error

Undefined property: stdClass::$client (View: C:\wamp\www\intranet\resources\views\users\list.blade.php)

The problem is that I am working with an existing database that in the tables does not have id fields, and the foreign keys would also be the typical ones like client_id

My model Client.php

class Client extends Model
{
    protected $connection = 'dpnmwin';

    protected $table = 'nmundfunc';

    public function employee(){

        return $this->hasMany('App\Employee');

    }

}

My model Employee.php

class Employee extends Model
{
    protected $connection = 'dpnmwin';

    protected $table = 'nmtrabajador';

    public function client(){

        return $this->belongsTo('App\Client', 'COD_UND');

    }
}

In nmtrabajador COD_UND field would be the foreign key that relates to nmundfunc.

And I try to get the data out like this: {{$user->client->CEN_DESCRI}}.

but it does not throw me the error, how can I solve it?

My Controller where I send in sight

public function index(){

        $users = DB::connection('dpnmwin')->table('nmtrabajador')->where('CONDICION', '=', 'A')->get();

        return view('users.list',array(
            'users' => $users
        ));

    }
Edwin Aquino
  • 201
  • 2
  • 16

2 Answers2

1

You have to call basis on relations.

This code will return you data.

If you have id then you can find by id like below

$employee=Employee::find(1);

Or if you want to fetch all data then you can call all method.

Employee::all();

And then you can just get it by relation as you define in models.

$client=$employee->client->CEN_DESCRI;

Retrieving data from the instance is based on the methods which we have use. Here in this answer, you can get that

Property [title] does not exist on this collection instance

I hope it will work.

pspatel
  • 508
  • 2
  • 7
  • 18
  • I get this error SQLSTATE [42S22]: Column not found: 1054 Unknown column 'nmtrabajador.id' in 'where clause' (SQL: select * from `nmworker` where` nmworker`.`id` = 1 limit 1). I think you're looking for a field of type id, but these tables do not have it`. I think you're looking for a field of type `id` , but these tables do not have it – Edwin Aquino May 28 '18 at 18:31
  • @EdwinAquino You can get all the data of employee with all() function. Updating code – pspatel May 28 '18 at 18:32
  • Now i get `Property [client] does not exist on this collection instance.` – Edwin Aquino May 28 '18 at 18:40
  • I added I thought you have used, data retrieving is based on methods. I added link please check – pspatel May 28 '18 at 18:46
  • This is useful at the moment, but there are many relationships that I can do to get the data, in this case as I do because I am calling for a unique method. – Edwin Aquino May 28 '18 at 19:17
  • Because in another relationship that I did with another database, as I had done at the beginning, it served me well, and just changed, for example, `$user->client...` or `$user->role...` which is what what do i try to do – Edwin Aquino May 28 '18 at 19:17
0

If table doesn't have 'id' as primary key you should specify what the primary key is inside your model:

protected $primaryKey = 'your_primary_key';

Relation looks good, after that you must make sure $user is a defined instance of Employee, because your error probably means that your instance wasn't even defined, so for example if you are using list.blade.php, you need to change the return of your controller and indicate that you want to pass data to your view, for example you could do it like this:

return view('users.list', compact('user'));

Where user is an instance of Employee saved on '$user'

Update

First you should check your user is retrieved properly, you can check it by placing a dd($user) And when you return a view you can pass information to it, a cleaner way of doing what you are trying to do is what I wrote earlier so you would end up having something like this:

public function index()
{
    $users = DB::table('nmtrabajador')
                 ->where('CONDICION', '=', 'A')
                 ->get();
    // dd($user) for debugging you are retrieving the user properly
    return view('users.list', compact($users));
}
Pol Lluis
  • 1,152
  • 7
  • 17