Laravel, four model relationship - php

I have four models: Animal(which can only be "dog" or "cat"), Category, Breed and Post.
Table data:
Animal:
- id
- name
Category:
- id
- name
- animal_id
Breed:
- id
- name
- category_id
Post:
- id
- breed_id
What I need is to get all Posts that are about dogs. Have any ideas?
Update:
My models:
class Animal extends Model
{
public function categories()
{
return $this->hasMany(Category::class)->orderBy('name', 'ASC');
}
}
class Category extends Model
{
public function animal()
{
return $this->belongsTo(Animal::class);
}
public function breeds()
{
$this->hasMany(Breed::class)->orderBy('name', 'ASC');
}
}
class Breed extends Model
{
public function category()
{
return $this->belongsTo(Category::class);
}
public function posts()
{
return $this->hasMany(Posts::class);
}
}
class Posts extends Model
{
public function breed()
{
return $this->belongsTo(Breed::class);
}
}

First of all you need to properly set up the relations in the eloquent models,
here is an example of code,
For Post Eloquent Model
And post must have the column animal_id
public function animal(){
return $this->belongsTo(Animal::class);
}
Then you have to query like
$posts = Post::whereHas('animal',function($q){
return $q->where('name','dog');});
After that you will get the posts which belongs to animal data where animal name is dog. And if you want to query with your current database structure that is not recommended because of complex and long query.

Related

laravel relationship between 3 tables - many-to-many

I have 3 tables (Models)
Advertise :
id
title
...
Value:
id
title
amount
....
Field:
id
name
title
...
and my pivot tables is like:
advertise_id
value_id
field_id
how can I set relations in my models and do a crud(please give me an example)?
all the relations are many to many
3 model classes accordingly:
class Advertise extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class);
}
public function values()
{
return $this->belongsToMany(Value::class);
}
}
class Field extends Model
{
public function advertises()
{
return $this->belongsToMany(Advertise::class);
}
public function values()
{
return $this->belongsToMany(Value::class);
}
}
class Value extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class);
}
public function advertises()
{
return $this->belongsToMany(Advertise::class);
}
}

How to get only active element from many to many relationship with translation in laravel

I have a problem with a many to many relationship and the translations of the terms.
I have 4 tables:
products
- id, price, whatever
products_lang
- id, product_id, lang, product_name
accessori
- id, active
accessori_lang
- id, accessori_id, lang, accessori_name
I'm trying to assign accessories to products with an intermediate table named:
accessori_products
this is the model for Product:
class Product extends Model {
protected $table = 'products';
public function productsLang () {
return $this->hasMany('App\ProductLng', 'products_id')->where('lang','=',App::getLocale());
}
public function productsLangAll() {
return $this->hasMany('App\ProductLng', 'products_id');
}
public function accessori() {
return $this->belongsToMany('App\Accessori', 'accessori_products');
}
}
this is the model for productLng:
class ProductLng extends Model {
protected $table = 'products_lng';
public function products() {
return $this->belongsTo('App\Product', 'products_id', 'id');
}
}
Then I have the model for Accessori:
class Accessori extends Model {
protected $table = 'accessori';
public function accessoriLang() {
return $this->hasMany('App\AccessoriLng')->where('lang','=',App::getLocale());
}
public function accessoriLangAll() {
return $this->hasMany('App\AccessoriLng');
}
public function accessoriProducts() {
return $this->belongsToMany('App\Products', 'accessori_products', 'accessori_id', 'products_id');
}
}
And the model for AccessoriLng:
class accessoriLng extends Model {
protected $table = 'accessori_lng';
public function accessori() {
return $this->belongsTo('App\Accessori', 'accessori_id', 'id');
}
}
I get the results by this:
$products = Product::has('accessori')->with([
'productsLang ',
'accessori' => function ($accessori){
$accessori->with([
'accessoriLang'
]);
}
])->get();
return $products;
but I want to get only the active accessories something like where accessori.active = 1 but I really don't know where to put it. I've tried in different way but I'm stuck on it by 2 days.
IIRC you don't need a model for the intermediate table on your many to many relationships.
If you want to return Products where Accessori is active you can use whereHas on the Product model.
$prod = Product::whereHas('accessori', function($query) {
$query->where('active', 1);
})->get();
Where the $query param will be running on the Accessori model.
You can do the inverse as well with Accessori to Product.
$acessoris = Accessori::where('active', 1)->whereHas('accessoriProduct')->with(['accessoriLang', 'accessoriProducts.productsLang'])->get();

laravel - 3 steps relation by many to many

I have 3 tabels: users, teachers And posts.
users:
id - integer
name - string
teachers:
id - integer
teacher_id - integer
user_id - integer
name - string
posts:
id - integer
user_id - integer
title - string
User Model:
class User extends Model
{
public function teachers()
{
return $this->hasMany('App\Teacher');
}
}
Teacher Model:
class Teacher extends Model
{
public function posts()
{
return $this->hasMany('App\Post', 'user_id','teacher_id');
}
}
? Question is How can I use sth like this:
$user = User::find(1);
$teacher_posts = $user->teachers()->posts
I'm sort of new to laravel but here is my take.
What I have realized is that typically your many to many relationships will require a pivot table.
users:(id, name)
teachers:(id, teacher_id, name)
teacher_users:(id, user_id, teacher_id)
posts:(id, user_id, title)
And your Eloquent models will look like this.
class User extends Model
{
public function teachers()
{
return $this->belongsToMany('App\Teacher');
}
public function posts()
{
return $this->hasMany('App\Post');
}
}
class Teacher extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
}
$user = User::find(1);
$user_posts = $user->posts();
Check out the documentation here for more details

Picking the right type of relationship using Laravel and Eloquent

I have three tables in my database.
Posts
Authors
Categories
When viewing an Author page I want to be able to view all of the author's Posts and also the category of the post.
When viewing a Category index page I want to be able to view all of the Posts for that category and also include the Author with each Post.
When viewing a Post I want to be able to include the Category and Author
What type of relationship can I use to achieve this?
One to one, One to Many, Many to many, or polymorphic
Thanks in advance.
You can create your relations like this:
class Post extends Eloquent {
public funcion category()
{
return $this->belongsTo('Category');
}
public funcion author()
{
return $this->belongsTo('User');
}
}
class Author extends Eloquent {
public funcion posts()
{
return $this->hasMany('Post');
}
}
class Category extends Eloquent {
public funcion posts()
{
return $this->hasMany('Post');
}
}
And then use them this way:
$author = Author::find(1);
foreach($author->posts as $post)
{
echo $post->title;
echo $post->author->name;
}
$category = Category::find(1);
foreach($category->posts as $post)
{
echo $post->title;
echo $post->author->name;
}
$post = Post::find(1);
echo $post->category->title;
echo $post->author->name;
I have done something like this :
db table =
users : id name
debates : id post user_id
now in model
class User extends SentryUserModel {
public function debates()
{
return $this->hasMany('Debate', 'user_id');
}
}
and
class Debate extends Eloquent {
public function user()
{
return $this->belongsTo('User', 'id');
}
}
now in query
$debate= Debate::find(1);
echo $debates->user->name;
echo $debates->user->id;
it is giving a null result.
Changing this two solve the problem . (Do now know why we cant use foreign key here. If anyone know this please do inform ).
class User extends SentryUserModel {
public function debates()
{
return $this->hasMany('Debate');
}
}
and
class Debate extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}

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