I have the following tables:
user_details
id - PK
user_name
user_email
cities
id - PK
city_name
regions
id - PK
region_name
city_id - FK(cities)
user_region (Many to Many)
user_id
region_id
And these are the models:
User
public function regions()
{
return $this->belongsToMany('App\Regions', 'user_region', 'user_id', 'region_id');
}
Regions
public function cities()
{
return $this->belongsTo('App\Cities');
}
public function users()
{
return $this->belongsToMany('App\User', 'user_region', 'region_id', 'user_id');
}
Cities
public function regions()
{
return $this->hasMany('App\Regions', 'city_id', 'id');
}
I need to get the city name for a user. Which relation can I use here to do this?
According to your comment:
A city can have many regions. And a user can work in many regions. But, the user will only work in a single city.
As all regions which user belongs to will belong to only one city you can do as follow:
$user = User::find(1);
$city = $user->regions()->first()->cities;
echo $city->city_name;
Related
I have a problem showing data from 2 tables with an id, in laravel 6. My tables are "users" and "companies".
users
id
name
last name
companies
id
company
address
id_user
Model user
public function company()
{
return $this->hasOne('App\Company','id_user','id');
}
Model company
public function user()
{
return $this->belongsTo('App\User');
}
Controller
public function show($id)
{
$companies = Company::with('user')->find($id);
return view('clients.show', compact('companies'));
}
view
$companies->company
but the problem is that is not showing data from users table, can someone help me?
The foreign key name for user in companies table should be 'user_id' not 'id_user'
If you want to use custom column name make sure you pass it to the second argument of the relationship.
Model company:
public function user()
{
return $this->belongsTo('App\User', 'id_user');
}
I have users table which has a hasOne relationship with vendordetails table.
class User extends Authenticatable
{
public function vendor_details() {
return $this->hasOne('App\Vendordetail');
}
}
On the other side vendordetails table contains country_id, state_id and city_id and has relationship of belongsTo with Country, State and City model.
Vendordetail.php model is:-
class Vendordetail extends Model
{
public $timestamps = false;
public function this_country(){
return $this->belongsTo('App\Country', 'country_id');
}
public function this_state(){
return $this->belongsTo('App\Country', 'state_id');
}
public function this_city(){
return $this->belongsTo('App\Country', 'city_id');
}
}
How can i fetch records of country, state and city if i query on users table.
$user = User::with('vendor_details')->first();
In this query it only finds the result of vendordetails table, i also wants country, state and city.
Thanks
To access nested relationship
$user = User::with(['vendor_details.this_state','vendor_details.this_country','vendor_details.this_city'])->first();
I have tables:
users
- id
- name
- company_id
companies
- id
- company_name
watched_objects
- id
- user_id
- object_id
- type
Now I want to get all watched companies for a user.
So query should looks:
SELECT
companies.*
FROM companies
JOIN watched_objects ON watched_objects.object_id = companies.id
WHERE watched_objects.user_id = 1
How should I define relations?
I try this:
class User
{
public function watched()
{
return $this->hasManyThrough('App\Company', 'App\WatchedObject', 'user_id', 'id');
}
}
But query is:
SELECT
companies.*,
watched_objects.user_id
FROM companies
INNER JOIN watched_objects ON watched_objects.id = companies.id
WHERE watched_objects.user_id = 1
How I can change watched_objects.id to watched_objects.object_id.
If your treating object_id as company id, then the relation is considered to be many to many. Then table watched_objects will be the third table kept the relation of user and company.
class User {
public function watched() {
return $this->belongsToMany('App\Company', 'watched_objects', 'user_id', 'object_id');
}
}
In order to find the watched companies of user 1, you can use the following code.
$watched_companies = User::find(1)->watched;
To get all watched companies by a user you can do like this:
$watches=WatchedObject::where(['user_id'=>Auth::user()->id])->with('company')->get();
foreach($watches as $watch)
{
print_r($watch->company->company_name);
}
Relation:
Company hasMany watched objects:
public function watches()
{
return $this->hasMany('App\WatchedObject','object_id');
}
and belongsTo in watchedObject Model:
public function company()
{
return $this->belongsTo('App\Company','object_id');
}
I have 3 models:
Tournament, Category, Team
A Tournament hasMany Category
A Category hasMany Team
Tables:
Tournament: Only attributes
Category: id, tournament_id, name
Teams: id, category_id, name
I would like to get all teams from a tournament by: $tournament->teams
I tried :
public function teams()
{
return $this->hasManyThrough(Team::class,CategoryTournament::class);
}
Then I need an extra relation of my team: team->category->name;
but the result of this HasManyThrough has no relationship....
Any Idea???
In Category model :
public function team ()
{
return $this->hasMany('App\Teams');
}
In Teams model :
public function category ()
{
return $this->belongsTo('App\Category', 'category_id');
}
I think should be like this, Try.
According to laravel documentation :
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
With this you can add relationship :
public function posts()
{
return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
}
I have 3 sample models
customer
id
order
id
customer_id
deliver_address_id
customer_address
id
customer_id
street
country
A customer can save many addresses on his profile but he can only choose one address per order.
I would like to get all customer orders including the address in one query using laravel 5 eloquent. I'm new in laravel so having difficulty to query complex relationship tables.
class Customer extends Model {
$table = 'customers';
function addresses(){
return $this->hasMany(Address::class, 'customer_id');
}
function orders(){
return $this->hasMany(Order::class, 'customer_id');
}
}
class Order extends Model {
$table = 'orders';
function address(){
return $this->belongsTo(Address::class, 'address_id');
}
function customer() {
return $this->belongsTo(Customer::class, 'customer_id');
}
}
class Address extends Model{
$table = 'customer_addresses';
function customer(){
return $this->belongsTo(Customer::class, 'customer_id');
}
}
TABLES :
1 customers
id
2 orders
id
customer_id
address_id
3 customer_addresses
id
customer_id
street
country