I have three tables, Owner, Garages, Orders
Owner has "n" garages and each garage belongs to one owner,
Each garage can have many orders,
Now is it possible to only get orders from Owner model with relation instance?
Owner model:
public function garages()
{
return $this->hasMany(Garage::class);
}
Garage model:
public function owner()
{
return $this->belongsTo(Owner::class);
}
public function orders()
{
return $this->hasMany(Order::class);
}
First of all, you should elaborate on how your models are defined. Guessing about how your database schemas are, you must fix your relationships:
Owner.php
public function garages()
{
return $this->hasMany(Garage::class);
}
public function orders()
{
return $this->hasManyThrough(Order::class, Garage::class);
}
Garage.php
public function owner()
{
return $this->belongsTo(Owner::class);
}
public function orders()
{
return $this->hasMany(Order::class);
}
Order.php
public function garage()
{
return $this->belongsTo(Garage::class);
}
Saying that, you can access your Owner's orders by doing
$owner = Owner::findOrFail($owner_id);
$orders = $owner->orders;
Related
I have order and return tables which have a created_by and accepted_by field where the corresponding user id is stored.
But now I would like to have multiple types for created_by and accepted_by instead of only user type. Assume a company could also create or accept the order/return.
I was thinking of a polymorphic one to many relationship.
Let’s name this table participants.
Something like:
ID
created
participantable_id
participantable_type
1
1
1
user
2
0
11
company
This works either for order or return but not both.
Is it practically to add extra colums to participants like trx_id and trx_type (order/return)?
How would the realtionships looks like to perform queries like:
$order->createdBy // should give me either user or company model
$order->acceptedBy
$return->createdBy
$return->acceptedBy
Or is there even a cleaner solution I am overlooking?
Thanks!
It would be 1:N polymorphic relationships so no need for an extra table. The structure can be found in the documentation
Your Order and Return model need to have the columns created_by_id, created_by_type, accepted_by_id, accepted_by_type.
______ (1) Company
/
Order (N) ----<
\______ (1) User
_____ (1) Company
/
Return (N) ----<
\_____ (1) User
class Order extends Model
{
public function created_by()
{
return $this->morphTo(__FUNCTION__, 'created_by_type', 'created_by_id');
}
public function accepted_by()
{
return $this->morphTo(__FUNCTION__, 'accepted_by_type', 'accepted_by_id');
}
}
class Return extends Model
{
public function created_by()
{
return $this->morphTo(__FUNCTION__, 'created_by_type', 'created_by_id');
}
public function accepted_by()
{
return $this->morphTo(__FUNCTION__, 'accepted_by_type', 'accepted_by_id');
}
}
class User extends Authenticatable
{
public function created_orders()
{
return $this->morphMany(Order::class, 'created_by');
}
public function accepted_orders()
{
return $this->morphMany(Order::class, 'accepted_by');
}
public function created_returns()
{
return $this->morphMany(Return::class, 'created_by');
}
public function accepted_returns()
{
return $this->morphMany(Return::class, 'accepted_by');
}
}
class Company extends Model
{
public function created_orders()
{
return $this->morphMany(Order::class, 'created_by');
}
public function accepted_orders()
{
return $this->morphMany(Order::class, 'accepted_by');
}
public function created_returns()
{
return $this->morphMany(Return::class, 'created_by');
}
public function accepted_returns()
{
return $this->morphMany(Return::class, 'accepted_by');
}
}
I have a 3 model "User","Review","ReviewReplay"
in user Model
public function reviews()
{
return $this->hasMany('App\Models\Review');
}
public function reviewReplys()
{
return $this->hasMany('App\Models\ReviewReply');
}
In Review Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function reviewReplys()
{
return $this->hasMany('App\Models\ReviewReply');
}
in Review Replay Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function review()
{
return $this->belongsTo('App\Models\Reviews');
}
in My Controller
public function getReview($id)
{
$review= Review::where('product_id',$id)->with('user','reviewReplys')->paginate(10);
return response()->json($review);
}
Now here I run Review foreach loop and here I need review.reviewReplay.user information. How I Get The Review Replay User information inside Review Foreach loop.
You can do it as below.
$review= Review::where('product_id',$id)->with(['reviewReplys.user'])->paginate(10);
with(['reviewReplys.user']) --> it will make connection between Review model to reviewReplys model +
reviewReplys model to User model
I have the users table, where each user can be assigned to a manager. I have 3 types of Managers (Country Manager, Area Manager and Fleet Manager). One Area manager can have many Fleet Manager and Country Manager, and one Country Manager can have many Fleet Manager. My users table have the columns fleet_manager_id, country_manager_id,area_manager_id that reference on id of users table. The question is that i don't know how to set a polymorphic relationship on same table.
Migration:
Schema::table('users', function (Blueprint $table) {
$table->integer('fleet_manager_id')->unsigned()->nullable();
$table->foreign('fleet_manager_id')->references('id')->on('users');
$table->integer('country_manager_id')->unsigned()->nullable();
$table->foreign('country_manager_id')->references('id')->on('users');
$table->integer('area_manager_id')->unsigned()->nullable();
$table->foreign('area_manager_id')->references('id')->on('users');
});
My User Model:
public function fleetManagers()
{
return $this->hasMany(User::class, 'fleet_manager_id');
}
public function countryManager()
{
return $this->belongsTo(User::class, 'country_manager_id');
}
public function areaManager()
{
return $this->belongsTo(User::class, 'area_manager_id');
}
So, this is the solution i've come so far:
public function managedByArea()
{
return $this->hasMany(User::class, 'area_manager_id');
}
public function area_manager()
{
return $this->belongsTo(User::class, 'area_manager_id');
}
public function managedByCountry()
{
return $this->hasMany(User::class, 'country_manager_id');
}
public function country_manager()
{
return $this->belongsTo(User::class, 'country_manager_id');
}
public function managedByFleet()
{
return $this->hasMany(User::class, 'fleet_manager_id');
}
public function fleet_manager()
{
return $this->belongsTo(User::class, 'fleet_manager_id');
}
City.php
public function municipal_districts()
{
return $this->hasMany('App\Models\Municipal_district');
}
Municipal_district.php
public function province()
{
return $this->belongsTo('App\Models\City', 'city_id');
}
Where can be wrong?
City Model
public function municipal_districts()
{
return $this->hasMany('App\Models\Municipal_district');
}
Municipal_district Model
public function city()
{
return $this->belongsTo('App\Models\City', 'city_id');
}
And I don't knwo about cities() relationship here!
City has many ditricts and District belongs to one city. So, What is the cities() here?
So i'm a bit confused with this situation, I have the current relations:
User hasmany Favourite
favourites references to a Recipe model
favourites is a pivot table but when I retrieve the favourites for a given user like:
$user->favourites
i get the favourite with the pivot data, but I want to get the fields of the recipe
Recipe.php:
public function favourites()
{
return $this->hasMany('App\Favourite');
}
User.php:
public function favourites()
{
return $this->hasMany('App\Favourite');
}
Favourite.php:
public function user()
{
return $this->belongsTo('App\User','user_id');
}
public function recipes()
{
return $this->belongsTo('App\Recipe','recipe_id');
}
EDIT : there was no problem at all, just needed to call:
#foreach($user->favourites as $favourite)
{{ $favourite->recipe->name }}
#endforeach
You need to set your relation to Has Many Through instead of just has Many.
https://laravel.com/docs/5.2/eloquent-relationships#has-many-through
Recipe.php:
public function favourites()
{
return $this->hasManyThrough('App\Favourite', 'App\User');
}
User.php:
public function favourites()
{
return $this->hasManyThrough('App\Favourite', 'App\Receipe');
}
Favourite.php:
public function user()
{
return $this->hasManyThrough('App\User','App\Receipe');
}