Call to undefined relationship [cities] on model [App\Models\Municipal_district] - laravel-5

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?

Related

Access to relation on another model

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;

Polymorphic BelongsTo relationship in Laravel

How could I set relationships to use just one table (model_types) in Laravel to store types for cars and bikes?
Car model
public function carTypes()
{
return $this->hasMany(CarType::class);
}
CarType model (inverse relationship):
public function car()
{
return $this->belongsTo(Car::class);
}
Bike model
public function bikeTypes()
{
return $this->hasMany(BikeType::class);
}
BikeType model (inverse relationship):
public function bike()
{
return $this->belongsTo(Bike::class);
}
There are 2 options I can think of to solve this problem, the first being a simple table using a type column and the other is using polymorphic relations which is a little overkill.
The first option is to have a type column on your model_types table which you could use to determine the type and adding constants in your ModelType class like this:
const TYPE_CAR = 1;
const TYPE_BIKE = 2;
Then you can easily access the data like so, so from the Car model it's
public function modelType()
{
return $this->belongsTo(ModelType::class)->where('type', ModelType::TYPE_CAR);
}
If you wanted to access it from the model_types table it would look like this:
public function cars()
{
return $this->hasMany(Car::class)
}
public function bikes()
{
return $this->hasMany(Bike::class)
}
You have it reversed.
A car can belong to one car type, but one car type can apply to many cars.
The same goes for bikes.
You don't need a polymorphic relationship.
Car model
public function carType()
{
return $this->belongsTo(ModelType::class);
}
Bike model
public function bikeType()
{
return $this->belongsTo(ModelType::class);
}
ModelType model
public function cars()
{
return $this->hasMany(Car::class);
}
public function bikes()
{
return $this->hasMany(Bike::class);
}
Not sure about inverse relationship, but in your Car model you should use
public function carTypes()
{
return $this->hasMany(ModelType::class, 'foreign_key', 'local_key');
}
Car Model:
public function carTypes() {
return $this->hasMany(ModelType::class);
}
Bike Model:
public function bikeTypes() {
return $this->hasMany(ModelType::class);
}
ModelType Model:
public function car() {
return $this->belongsTo(Car::class, 'modeltype_car_id');
}
public function bike() {
return $this->belongsTo(Bike::class, 'modeltype_bike_id');
}

laravel eloquent: Get all groups for a member

I have 3 tables:
user: id,name
group: id,name,parent_id made with this) // this is a nested category
members: id,user_id,group_id,admin
and group_member: group_id,member_id
I want get all groups (include sub groups) for a member. Is it possible do it via laravel?
Group Modal:
public function parent()
{
$parent = $this->belongsTo('App\Group', 'parent_id');
return $parent;
}
public function children()
{
$children = $this->hasMany('App\Group', 'parent_id');
//$children->wherePublish(1);
return $children;
}
public function members()
{
return $this->hasMany(Member::class);
}
public function member()
{
return $this->belongsToMany(Member::class);
}
And Member model:
public function groups()
{
return $this->belongsToMany(Group::class);
}
public function group()
{
return $this->belongsTo(Group::class);
}
And user model:
public function members()
{
return $this->hasMany(Member::class);
}
I want something like this (to return all related groups):
auth()->user()->members()->groups()->get();
I got this error:
"Call to undefined method
Illuminate\Database\Eloquent\Relations\HasMany::groups()"
Please use this below query:
App/User::where('id',auth()->user()->id)->with('members.groups')->get();

Call to undefined relationship [person] on model [App\Note]

i have a notes, cards and person table wich notes table has card_id and person_id
now once i want load card data i alse want load person name's name but laravel give this error: Call to undefined relationship [person] on model [App\Note].
sorry my english is not good.
this my relationships:
notes:
public function card()
{
return $this->blongsTo(Card::class);
}
public function person()
{
return $this->blongsTo(Person::class);
}
cards:
public function notes()
{
return $this->hasMany(Note::class);
}
persons
public function notes()
{
return $this->hasMany(Note::class);
}
and this my action:
public function showCard(Card $card)
{
$card->load('notes.person');
return view('cards.card',compact('card'));
}
whats wrong?
You have a spelling mistake. Check the relationship, it says: ->blongsTo instead of belongsTo.
public function card()
{
return $this->belongsTo(Card::class);
}
public function person()
{
return $this->belongsTo(Person::class);
}

lumen/laravel Eloquent hasManyThrough 3 models relation

I try to acomplish a relation with 3 models:
Cities.php //table cities
id
name
Neighbourhoods.php //table neighbourhoods
id
name
city_id
Blocks.php //table blocks
id
name
neighbourhood_id
My models looks like this:
Cities.php
public function neighbourhoods()
{
return $this->hasManyThrough('App\Neighbourhoods', 'App\Blocks', 'neighbourhood_id', 'city_id', 'id');
}
Neighbourhoods.php
public function blocks()
{
return $this->hasMany('App\Blocks', 'neighbourhood_id', 'id');
}
Blocks.php
public function neighbourhoods()
{
return $this->belongsToMany('App\Neighbourhoods', 'neighbourhood_id', 'id');
}
The result shoud be:
results
city1:
neighbourhoods:
neighbourhood1:
block1
block2
block3
neighbourhood2
block1
block2
city2:
neighbourhoods:
neighbourhood1:
blocks:
block1
block2
block3
neighbourhood2:
blocks:
block1
block2
Calling the results:
return Blocks::with('neighbourhoods')->get();
I know that my models are not properly named. City (singular), Neighbourhood (singlar), Block (singular) but passing parameters shoud work.
I just can't figure our why it does not work.
RELATIONSSHIP SOLUTION based on #Gaurav Rai's response
First of all, my models are wrong named. Please condider naming your database using plurals for example: cities, neighbourhoods, blocks and your models singular for example: City.php, Neighbourhood.php and Block.php
Based on my problem, the solution is:
Cities.php
public function neighbourhoods()
{
return $this->hasMany('App\Neighbourhoods', 'city_id', 'id');
// because my model is called Cities.php,
// the function will look by default
// for the column cities_id in neighbourhoods table,
// thats why we need to specifiy city_id column
}
public function blocks()
{
return $this->hasManyThrough('App\Blocks', 'App\Neighbourhoods', 'city_id', 'neighbourhood_id', 'id');
}
Neighbourhoods.php
public function cities()
{
return $this->belongsTo('App\Cities', 'city_id', 'id');
}
public function blocks()
{
return $this->hasMany('App\Blocks', 'neighbourhood_id','id');
}
Blocks.php
public function neighbourhoods()
{
return $this->belongsTo('App\Neighbourhoods', 'neighbourhood_id');
}
Calling the relation:
return Cities::with(['neighbourhoods', 'blocks'])->get();
I think your relationships are not well defined:
Cities.php
public function neighbourhoods()
{
return $this->hasMany('App\Neighbourhoods');
}
public function blocks()
{
return $this->hasManyThrough('App\Neighbourhoods', 'App\Blocks');
}
Neighbourhoods.php
public function blocks()
{
return $this->hasMany('App\Blocks');//by default it will consider id
}
public function city()
{
return $this->belongsTo('App\City');
}
Blocks.php
public function neighbourhoods()
{
return $this->belongsTo('App\Neighbourhoods');
}

Resources