Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You don't need a "relationship model" --- a model represents a table, and you don't have a pivot table for a one-many relationship. So delete that. To retrieve houses of a user, you'd do this:

$user = Users::find($id); /* or however you want to retrieve your user */
$houses = $user->houses;

That's it.

Your relationship methods look fine --- although I would change 'users' to 'user', since only one user owns a house (unless you want to do a many-many relationship, which this isn't).

Also, it's bad practice to have plural models -- most have singular models like "User" and "House", and you might run into some issues there because laravel assumes singular/plural in different situations.

Last updated 2 years ago.
0

Thanks for reply! So you recommend to delete UsersHouses model and relative table and put user_id into houses table?

Last updated 2 years ago.
0

Yes, remove the UsersHouses model, but don't put user_id into the Houses table. Use the Laravel pivot table implementation: http://laravel.com/docs/eloquent#working-with-pivot-tables

You don't need to have a model for every table, esp when the table itself is just a junction table (pivot table, many-to-many table) and doesn't convey any real meaning other than relationships.

Last updated 2 years ago.
0

Hi ddimaria, sorry but i still have problems:

when i try to do this

$user = Users::find($id); /* or however you want to retrieve your user */
$houses = $user->houses;

i get the following error

*Column not found: 1054 Unknown column 'houses.users_id' in 'where clause' (SQL: select * from houses where houses.users_id = 2) *

Last updated 2 years ago.
0

Do you have a "houses" method in your User model? You need to setup the relationship there. Once you do that, then your code should work.

public function houses()
{
    return $this->belongsToMany('UsersHouses');
}
Last updated 2 years ago.
0

I don't think you need a pivot table if you only want a 1-many relationship as hinted at in your original post. Make sure you have a column named exactly users_id in your houses table. If you need to you can override what column it looks for in the relationship in your Users model.

 public function houses()
  {
    return $this->hasMany('Houses', 'column_to_look_for');
  }
Last updated 2 years ago.
0

Ok now my models are:

Users Model

class Users extends Eloquent implements UserInterface, RemindableInterface {
  protected $table = 'users';
  public $timestamps = FALSE;

  public function houses()
  {
    return $this->belongsToMany('Houses');
  }
}

Houses Model

class Houses extends Eloquent {
  protected $table = 'houses';
  public $timestamps = FALSE;

  public function users()
  {
    return $this->hasOne('Users');
  }
}

the code you posted

$user = Users::find($id); /* or however you want to retrieve your user */
$houses = $user->houses;

Works fine!! Very Thanks. The last thing is...I want to delete houses...ho to do that? In my Houses controller i have the following function that delete only an house inside houses table but not in the relationship table

	public function destroy($id)
	{
	 $house = Houses::find($id);
 	 $house->delete();
	 return Redirect::action('HousesController@index'); 	 
	}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ocraton ocraton Joined 18 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.