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);
}
Related
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;
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');
}
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?
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');
}
I have a courses table, a course hasMany sections and sections hasMany lectures and lectures hasMany comments. How should I define the relationship in the LectureComment model, if I have a comment id and I want to know its course name?
table structures
courses: id|title
sections: id|course_id|name
lectures: id|section_id|name|description
lecture_comments: id|lecture_id|user_id|comment_body
In course model:
public function sections()
{
return $this->hasMany('App\Section');
}
Section model:
public function course()
{
return $this->belongsTo('App\Course');
}
public function lectures()
{
return $this->hasMany('App\Lecture');
}
Lecture model:
public function section()
{
return $this->belongsTo('App\Section');
}
public function lectures_comments()
{
return $this->hasMany('App\LecturesComment');
}
LecturesComment model:
public function lecture()
{
return $this->belongsTo('App\Lecture');
}
To receive required data, you must walk through relations.
If you have correctly written foreign keys, this code will return a course title:
$comment = LecturesComment::find(1);
$courseName = $comment->lecture->section->course->title
Hope it will be helpful :)