Laravel Relationship tables passing extra "s" with id name - php

I am getting "'lessons.subjects_id" while there is "subject_id" in lesson table. dont know where is problem in my relationship. My relationship models are as under:
class Lessons extends Model
{
public function subject()
{
return $this->belongsTo('Lea\Subjects');
}
public function category()
{
return $this->belongsTo('Lea\Category');
}
}
Subject Model is:
class Subjects extends Model
{
public function category()
{
return $this->belongsTo('Lea\Category');
}
public function Lessons()
{
return $this->hasMany('Lea\Lessons');
}
}

If you didn't respect the Laravel convention you have to teach him about yours ;) by adding you foreign key name.
In the documentation you have :
Eloquent determines the foreign key of the relationship based on the
model name. In this case, the Phone model is automatically assumed to
have a user_id foreign key. If you wish to override this convention,
you may pass a second argument to the hasOne method:
class Lessons extends Model
{
public function subject()
{
// your foreign key
return $this->belongsTo('Lea\Subjects', 'subject_id');
}
public function category()
{
return $this->belongsTo('Lea\Category');
}
}
And
class Subjects extends Model
{
public function category()
{
return $this->belongsTo('Lea\Category');
}
public function Lessons()
{
// your foreign key
return $this->hasMany('Lea\Lessons', 'subject_id');
}
}

Related

Laravel whereDoesntHave union whereHas

I'm trying to query my models that don't have relation and union it with the ones that have relations but I get an error with memory being exhausted.
Post::whereDoesntHave('comments')
->union(Post::newQuery()->whereHas('comments')->getQuery())
->paginate();`
Anyone know a better way of using the union?
Update Your Post Model with below code.
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class); //Pass Foreign Key if other than id
}
}
Update Your Comment Model with this code.
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);//Pass Foreign Key if other than id
}
public function user()
{
return $this->belongsTo(User::class);//Pass Foreign Key if other than id
}
public function scopeNoComment($query)
{
return $query->count() == 0;
}
}
$comments = $post->NoComment()->with('user')->paginate(10);
Making a relationship would be easy to access data.

Laravel through model relationships

I have 3 tables with this order:
School it has -> id
SchoolSemester it has -> school_id
SemesterClass it has -> semester_id
Now I am trying to make relation between School and SemesterClass in order to get list of classes of each school as well as name of schools in each class.
based on documentation i have this relationships:
school model
class School extends Model
{
public function semesters() {
return $this->hasMany(SchoolSemester::class);
}
public function classes() {
return $this->hasManyThrough(SemesterClass::class, SchoolSemester::class);
}
}
SchoolSemester model
class SchoolSemester extends Model
{
public function school() {
return $this->belongsTo(School::class);
}
public function classes() {
return $this->hasMany(SemesterClass::class, 'semester_id', 'id');
}
}
SemesterClass model
class SemesterClass extends Model
{
public function school() {
return $this->hasOneThrough(School::class, SchoolSemester::class, 'school_id', 'id');
}
public function semester() {
return $this->belongsTo(SchoolSemester::class, 'semester_id', 'id');
}
}
Controller
public function show($id)
{
$class = SemesterClass::with(['school', 'teacher', 'teacher.user', 'students'])->findOrFail($id);
dd($class);
//return view('admin.Classes.show', compact('class'));
}
Results
Any idea?
Your intermediate table is school_semester, laravel will find the foreign_key school_semester_id by default, however, your foreign_key is semester_id, so you need to specify the foreign_key in hasManyThrough:
public function classes() {
return $this->hasManyThrough(
SemesterClass::class,
SchoolSemester::class
'school_id', // Foreign key on school_semesters table...
'semester_id', // Foreign key on classes table...
'id', // Local key on schools table...
'id' // Local key on school_semesters table...
);
}
And change your hasOneThrough code like this:
public function school() {
return $this->hasOneThrough(
School::class,
SchoolSemester::class,
'id',
'id',
'semester_id',
'school_id' );
}
Another Solution:
Because the reverse is just the concatenation of two BelongsTo relationships, so you can just put the belongsTo in SchoolSemester and School Model.
And get the relationship like this:
SemesterClass::with(['semester.school'])
Or you can define a mutator in SemesterClass Model:
protected $appends = ['school'];
public function getSchoolAttribute() {
return $this->semester->school;
}

Laravel Eloquent model JOIN a MAX record

I'm new to Laravel and I'd like to know how to use eloquent model to join a max(date) record...
App\SchemasGroup::find(7)->schemas()->get()->max('schemasVersion')->get();
I find a group of schemas (schemas_groups table) based on ID, then get the schemas from that group (schemas table), and I want to join the 'date' and 'version' field from schemas_versions table with the last version (so, max date or version field) ...
Relations are defined as:
class SchemasGroup extends Model
{
public function schemas() { return $this->hasMany('App\Schema'); }
}
class Schema extends Model
{
public function group() { return $this->belongsTo('App\SchemasGroup'); }
public function versions() { return $this->hasMany('App\SchemasVersion'); }
}
class SchemasVersion extends Model
{
public function schema() { return $this->belongsTo('App\Schema'); }
public function updatedBy() { return $this->belongsTo('App\User','updated_by'); }
}
Getting the user name who updated that last version would also be lovely...
Apparently it was easy with defining chaining models.
class Schema extends Model
{
public function group() { return $this->belongsTo('App\SchemasGroup'); }
public function versions() { return $this->hasMany('App\SchemasVersion'); }
public function latestVersion() { return $this->hasOne('App\SchemasVersion')->latest(); }
}
and then fetching the data with:
App\SchemasGroup::with('schemas.latestVersion.updatedBy')->find($schemaGroupId);

Eloquent model relationship for intermediate table

Consider the following table structure:
user table
id
name
lang_region_id
lang_region table
id
lang_id
region_id
lang table
id
name
region table
id
name
Fairly new to the Laravel framework, but trying to setup Eloquent models and relationships to an existing database. I want to establish the relationship between my user model and the lang and region models. The lang_region table defines what language and region combinations are available and then we can link each user to a valid combination.
I have read through the Laravel documentation several times looking for the proper relationship type, but is seems that the Many to Many and Has Many Through relationships are close, but since our user.id isn't used in the intermediate table I may be out of luck.
Sorry for the amateur question, but just getting used to Laravel and ORMs in general.
I would use the lang_region table as both a pivot table and a regular table with its own model.
class LangRegion extends model
{
protected $table = 'lang_region';
public function language()
{
return $this->belongsTo(Language::class, 'lang_id');
}
public function region()
{
return $this->belongsTo(Region::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
class User extends model
{
protected $table = 'user';
public function langRegion()
{
return $this->belongsTo(LangRegion::class);
}
}
class Language extends model
{
protected $table = 'lang';
public function regions()
{
$this->belongsToMany(Region::class, 'lang_region', 'lang_id', 'region_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'lang_id', 'lang_region_id');
}
}
class Region extends model
{
protected $table = 'region';
public function languages()
{
$this->belongsToMany(Language::class, 'lang_region', 'region_id', 'lang_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'region_id', 'lang_region_id');
}
}
If I understand what you want correctly:
class User extends Model {
private function lang_region() {
return $this->hasOne(LangRegion::class)
}
public function lang() {
return $this->lang_region()->lang();
}
public function region() {
return $this->lang_region()->region();
}
}
class LangRegion extends Model {
public function lang() {
return $this->belongsTo(Lang::class);
}
public function region() {
return $this->belongsTo(Region::class);
}
}

Retrieving relationships of relationships using Eloquent in Laravel

I have a database with the following tables and relationships:
Advert 1-1 Car m-1 Model m-1 Brand
If I want to retrieve an Advert, I can simply use:
Advert::find(1);
If I want the details of the car, I could use:
Advert::find(1)->with('Car');
However, if I also want the detail of the Model (following the relationship with Car), what would the syntax be, the following doesn't work:
Advert::find(1)->with('Car')->with('Model');
Many thanks
It's in the official documentation under "Eager Loading"
Multiple relationships:
$books = Book::with('author', 'publisher')->get();
Nested relationships:
$books = Book::with('author.contacts')->get();
So for you:
Advert::with('Car.Model')->find(1);
First you need to create your relations,
<?php
class Advert extends Eloquent {
public function car()
{
return $this->belongsTo('Car');
}
}
class Car extends Eloquent {
public function model()
{
return $this->belongsTo('Model');
}
}
class Model extends Eloquent {
public function brand()
{
return $this->belongsTo('Brand');
}
public function cars()
{
return $this->hasMany('Car');
}
}
class Brand extends Eloquent {
public function models()
{
return $this->hasMany('Model');
}
}
Then you just have to access this way:
echo Advert::find(1)->car->model->brand->name;
But your table fields shoud be, because Laravel guess them that way:
id (for all tables)
car_id
model_id
brand_id
Or you'll have to specify them in the relationship.
Suppose you have 3 models region,city,hotels and to get all hotels with city and region then
Define relationship in them as follows:-
Hotel.php
class Hotel extends Model {
public function cities(){
return $this->hasMany(City::class);
}
public function city(){
return $this->belongsTo('App\City','city_id');
}
}
City.php
class City extends Model {
public function hotels(){
return $this->hasMany(Hotel::class);
}
public function regions(){
return $this->belongsTo('App\Region','region_id');
}
}
Region.php
class Region extends Model
{
public function cities(){
return $this->hasMany('App\City');
}
public function country(){
return $this->belongsTo('App\Country','country_id');
}
}
HotelController.php
public function getAllHotels(){
// get all hotes with city and region
$hotels = Hotel::with('city.regions')->get()->toArray();
}
will adding the relation function just ask for the relation needed
public function Car()
{
return $this->belongsTo(Car::class, 'car_id')->with('Model');
}
but if you want a nested relation just use the period in the with
Advert::with('Car.Model')->find(1);
but for multi-relation use the array
Advert::with('Car','Model')->find(1);

Categories