Laravel Query builder in many to many Relationship - php

Let's consider that I have a model called Sportman and another one Sport who are linked via a pivot table : many to many relationship.
A sample code for their migrations.
# Sportmans migration
Schema::create('sportsmans', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
});
# Sports migration
Schema::create('sports', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
});
Here is their relationship in models :
# Defining association in Sportsman model
public function sports(){
return $this->belongsToMany( Sport::class, 'sportsman_has_sports', 'person_id', 'sport_id' );
}
# Defining association in Sports model
public function sportsman(){
return $this->belongsToMany( Sportsman::class );
}
How can I using Laravel with Eloquent get the sportsman that play:
Only "Footbal"
Box or "Swimming"
Both "Tennis and Basket-ball
Here is what I tried to do for question 2 :
Sportsman::with(['sports:person_id,id,name'->whereHas('sports', function ($query) {
$query->whereIn('name', ['Box', 'Swimming'] );
});
Most difficult is the question 3

You put a subquery on whereHas function...
Sportsman::whereHas('sports', function ($query) {
$query->whereIn('name', ['Box', 'Swimming'] );
})
->with('sports')
->get();
// Below is your last question
Sportsman::where(function ($query) {
$query->whereHas('sports', function ($subquery) {
$subquery->where('name', 'Tennis');
});
$query->whereHas('sports', function ($subquery) {
$subquery->where('name', 'Basket-ball');
});
})
->with('sports')
->get();

Related

Laravel WhereHas only the latest record on the HasMany Relationship

I have Items table that has relation to Histories table.
I want to get count of items that has only latest history.status
I still can't get the exact same count result because its always count all of the histories not the latest one
Here is my code:
create_items_table.php
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('code');
$table->string('name');
$table->longText('description')->nullable();
$table->longText('picture')->nullable();
$table->timestamps();
});
create_histories_table.php
$table->foreignId('item_id')->constrained();
$table->string('status')->nullable();
$table->longText('description')->nullable();
$table->dateTime('date')->nullable();
model of Item.php
public function histories(){
return $this->hasMany(History::class);
}
public function latestHistory(){
return $this->hasOne(History::class)->latest();
}
model of History.php
public function item()
{
return $this->belongsTo(Item::class);
}
MyController.php
$items_status['good'] = Item::with('latestHistory')->whereHas('latestHistory', function ($q) {
$q->where('status', 'good');
})->count();
$items_status['broken'] = Item::with('latestHistory')->whereHas('latestHistory', function ($q) {
$q->where('status', 'broken');
})->count();
dd($items_status);
i guess you mean latestOfMany() ?
//Item.php
public function latestHistory() {
return $this->hasOne(History::class)->latestOfMany();
}
Also do you have any solution for count the items that doesn't have
history?
Check docs for doesntHave
$items_status['no_history'] = Item::doesntHave('history')->count();

Laravel: Order a model by "Has One Of Many" relationship

I have a customer model that has many contacts. I defined a relationship to get the most recent contact of the customer using the "Has One Of Many" relationship in Laravel 8:
Models
class Customer extends Model
{
use HasFactory;
public function contacts()
{
return $this->hasMany(Contact::class);
}
public function latestContact()
{
return $this->hasOne(Contact::class)->ofMany('contacted_at', 'max')->withDefault();
}
}
class Contact extends Model
{
use HasFactory;
protected $casts = [
'contacted_at' => 'datetime',
];
public function customer()
{
return $this->belongsTo(Customer::class);
}
}
Migration (contact model)
class CreateContactsTable extends Migration
{
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->softDeletes();
$table->foreignID('customer_id');
$table->string('type');
$table->dateTime('contacted_at');
});
}
}
In my view, I want to show all customers and order them by their latest contact. However, I can't figure out how to do that.
I tried to achieve it via the join method but then I obviously get various entries per customer.
$query = Customer::select('customers.*', 'contacts.contacted_at as contacted_at')
->join('contacts', 'customers.id', '=', 'contacts.customer_id')
->orderby('contacts.contacted_at')
->with('latestContact')
Knowing Laravel there must be a nice way or helper to achieve this. Any ideas?
I think the cleanest way to do this is by using a subquery join:
$latestContacts = Contact::select('customer_id',DB::raw('max(contacted_at) as latest_contact'))->groupBy('customer_id');
$query = Customer::select('customers.*', 'latest_contacts.latest_contact')
->joinSub($latestContacts, 'latest_contacts', function ($join){
$join->on([['customer.id', 'latest_contacts.customer_id']]);
})
->orderBy('latest_contacts.latest_contact')
->get();
More info: https://laravel.com/docs/8.x/queries#subquery-joins
I suspect there is an issue with your migration, the foreign key constraint is defined like this:
Check the documentation:
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
Method 1: define foreign key constraint
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->foreignId('consumer_id')->constrained();
$table->string('type');
$table->dateTime('contacted_at');
$table->timestamps();
$table->softDeletes();
});
}
Method 2: define foreign key constraint
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('customer_id');
$table->foreign('customer_id')->references('id')->on('customers');
$table->string('type');
$table->dateTime('contacted_at');
$table->timestamps();
$table->softDeletes();
});
}

Laravel 8 relationship of three models which belongs to Parent model

Hello everyone I'm currently working on a laravel project where I have a parent table that has the id's of three tables referenced to it. These table migrations also have their models respectively. Here are the table migrations files respectively:
create_products_table.php
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_id', 10);
$table->string('product_name');
$table->string('image');
$table->string('images');
$table->string('product_description');
$table->bigInteger('size_id')->unsigned();
$table->string('color');
$table->string('product_quantity');
$table->string('old_price');
$table->string('discount');
$table->string('product_price');
$table->bigInteger('user_id')->unsigned()->nullable();
$table->bigInteger('category_id')->unsigned();
$table->bigInteger('gender_id')->unsigned();
$table->timestamps();
$table->foreign('size_id')->references('id')->on('sizes')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->foreign('gender_id')->references('id')->on('genders')->onDelete('cascade');
});
create_genders_table.php
Schema::create('genders', function (Blueprint $table) {
$table->id();
$table->string('gender_class');
$table->timestamps();
});
create_categories_table.php
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('cat_name');
$table->timestamps();
});
create_sizes_table.php
Schema::create('sizes', function (Blueprint $table) {
$table->id();
$table->string('sizes');
$table->timestamps();
});
Also this is how I defined the relationships on their models respectively
Product.php
public function category()
{
return $this->belongsTo(Category::class);
}
public function gender()
{
return $this->belongsTo(Gender::class);
}
public function size()
{
return $this->belongsTo(Size::class);
}
Category.php
public function products()
{
return $this->hasMany(Product::class);
}
Gender.php
public function products()
{
return $this->hasMany(Product::class);
}
Size.php
public function products()
{
return $this->hasMany(Product::class);
}
I'm actually a laravel beginner and I studied eloquent model relationships at laravel.com so what I did was just based on my understanding of one to many relationships. When I check all my request with dd($request), category_id, gender_id, size_id all show null and I believe it's because I didn't define the relationship properly. Now this is where I seriously need your assistance.
So please my experienced developers I seriously need your help I'll really be grateful if I get your replies today. Thanks in advance.
=>Everything is right, just make changes in the products migration add this code.
$table->foreign('size_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('gender_id')->references('id')->on('products')->onDelete('cascade');
=>and migrate table

Laravel get which Categories belong to logged user in ManyToMany

in my simple blog web application i have some categories and users which stored in database, each category may belongs to one or many user and i try to get them to show each user logged, for implementing this scenario i have this tables:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->index()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->string('name')->nullable();
$table->string('family')->nullable();
$table->string('username')->unique();
$table->rememberToken();
$table->softDeletes();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
});
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id')->index()->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->string('category_name');
$table->softDeletes();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
});
and then, creating many to many table:
Schema::create('category_user', function (Blueprint $table) {
$table->unsignedBigInteger('user_id')->index();
$table->unsignedBigInteger('category_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->primary(['user_id','category_id']);
});
now how can i get categories of logged user like with this code:
$categories = Category::whereNull('category_id')
->with(['childrenCategories', 'users' => function ($query) {
//$query->where('id',auth()->user()->id);
}])
->withCount('posts')
->get();
You can query the relationship existence with whereHas to get only the categories which belongs to the authenticated user.
$categories = Category::whereNull('category_id')
->whereHas('users', function ($query) {
$query->where('id', auth()->user()->id);
})
->with([
'childrenCategories',
'users' => function ($query) {
$query->where('id', auth()->user()->id);
}
])
->withCount('posts')
->get();
inside Your User Model, add the relationship btw both Models:
public function categories()
{
return $this->belongsToMany('App\Models\Category');
}
inside Your Controller:
$user = App\Models\User::find(1);
now you can get the categories of your user
foreach ($user->categories as $category) {
//
}
https://laravel.com/docs/8.x/eloquent-relationships#many-to-many

Laravel query relationship based on name not id

Hi I am new laravel and struggling a bit on understanding how to query relationships. I am trying to make a basic restful api in laravel and have 3 models
class Book extends Model
{
public function author()
{
return $this->belongsTo(Author::class);
}
public function categories()
{
return $this->belongsToMany('App\Category', 'category_book')
->withTimestamps();
}
}
class Author extends Model
{
public function books(){
return $this->hasMany(Book::class);
}
}
class Category extends Model
{
public function books()
{
return $this->belongsToMany('App\Book', 'category_book')
->withTimestamps();
}
}
Table migrations:
Schema::create('books', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->string('ISBN', 32);
$table->string('title');
$table->integer('author_id')->unsigned();
$table->float('price')->default(0);
$table->timestamps();
});
Schema::create('authors', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigIncrements('id');
$table->string('name');
$table->string('surname');
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
Schema::create('category_book', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('category_id')->unsigned();
//$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->integer('book_id')->unsigned();
//$table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
$table->timestamps();
});
books is the main table and author has a one to many relationship with books. Category has a many to many relationship with books as a book can be in more than one category.
The books table has an author_id field to link it to the authors table. There is also a pivot table called category_books that contains category_id and book_id to link books to categories
But how do I query books so that it returns only books based on the authors name ?
I would also like to do the same thing based on the category name?
I my books controller i have the following but not sure how to do it correctly
public function index(request $request, Author $author, Category $category)
{
$author = $request->author;
$books = Book::find()->author()->where('name', $author);
$books = Book::with(['categories'])->where('name', $category);
return response()->json($books, 200);
}
By author:
$books = App\Book::whereHas('author', function ($query) use ($authorName) {
$query->where('name', $authorName);
})->get();
By category:
$books = App\Book::whereHas('categories', function ($query) use ($categoryName) {
$query->where('name', $categoryName);
})->get();
whereHas allows you to perform where queries on the specified relationship
Together:
$books = App\Book::whereHas('author', function ($query) use ($authorName) {
$query->where('name', $authorName);
})->whereHas('categories', function ($query) use ($categoryName) {
$query->where('name', $categoryName);
})->get();
To filter both author and book at the same query you may use both queries made by gbalduzzi combined:
$books = App\Book::whereHas('author', function ($query) use ($authorName) {
$query->where('name', $authorName);
})->whereHas('categories', function ($query) use ($categoryName) {
$query->where('name', $categoryName);
})->get();

Categories