2

Im really new to laravel, and im sure im doing something wrong

I have 2 tables. Cities And users_metadata

My cities table look like this

id | City        |
1  | New York    |
1  | Los Angeles |

users metadata

user_id | firt name | last name | cities_id |
1       | Jonh      | Doe       |         2 |

So my problem is when i create a relation i get New York, becaus the city id is matched with the user id

City model

class City extends Eloquent 
{
    public static $table = "cities";

    public function profile()
    {
        return static::has_many('Profile');
    }
}   

profile model

class Profile extends Eloquent 
{
    public static $timestamps = false;

    public static $table = "users_metadata";

    public static $key = "user_id";

    public function city()
    {
        return static::has_one('City');
    }
}   

error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'profile_id' in 'where clause'

SQL: SELECT * FROM `cities` WHERE `profile_id` = ? LIMIT 1

Bindings: array (
  0 => 1,
)

If i dont pass the id to has one i get the following error

Okay i understand this.

So my questions is am i able to pass the foreign key cities_id somehow in my relation to match? Or im doing it all wrong? Can someone give me a basic example?

thank you folks

1
  • @w0rldart please give a descriptive reason for your edits when the reason is not obvious - and there's no need for the "Edit" header.
    – AD7six
    Jan 15, 2013 at 12:11

4 Answers 4

2

Try this:

City model

class City extends Eloquent 
{
    public static $table = "cities";
    public function profile()
}   

Profile model

class Profile extends Eloquent 
{
    public static $timestamps = false;
    public static $table = "users_metadata";
    public static $key = "user_id";
    public function city()
    {
        return static::belongs_to('City');
    }
}

I ran into the same problem thinking I should use has_one, but I needed to use belongs_to. user1808639's probably didn't work because you still had a 'has_many' in the city model.

1

Try this:

class Profile extends Eloquent
{
    public function city()
    {
        return $this->belongs_to('City'); // city_id in the table
    }
}
0
0

Laravel has a way of detecting foreign keys automatically. So for your database schema... this models should work fine Try the following

user_id | firt name | last name |   city_id | //city not cities
1       | Jonh      | Doe       |         2 |

-

class Profile extends Eloquent 
{
    public static $timestamps = false;
    public static $table = "users_metadata";

    public function city()
    {
        return $this->has_one('City');
    }
}

/

class City extends Eloquent 
{
    public static $timestamps = false;

    public function profiles()
    {
        return $this->has_many('Profile');
    }
}
3
  • thank you for your reply, but its still the same, i get back the SQLSTATE[42S22]: Column not found: 1054 Unknown column 'profile_id' in 'where clause', and i still need to pass the id to the cities relation, and still matches the id with user_id instead of id with city_id SQL: SELECT * FROM cities WHERE profile_id = ? LIMIT 1
    – Levi
    Jan 15, 2013 at 11:17
  • Just a small typo fix, profiles() not profile() as it has_many. Post your code where you call the models
    – Alex
    Jan 15, 2013 at 11:23
  • I highly recommend you reading laravel.com/docs/database/eloquent, lines as public static $table = "cities"; is completely useless as Laravel does that automatically. Do you have any other users table? if not, set users_id in that table to id and post the controller where you call the models.... not only the models
    – Alex
    Jan 15, 2013 at 11:53
0

The table rows shouldn't contain whitespaces. So rename "firt name" | "last name" in "firstname" | "lastname". The SQL-error shows me that you're calling the model in the reverse way like City::find(1) in this case Laravel expects inside the "Cities" table the foreign key for the profiles that would be usually "profile_id".

I guess you're looking for something like this:

$user_id = 1; 
$user = Profile::find($user_id);

echo $user->firstname." ".$user->lastname." lives in ".$user->city->city;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.