Laravel query builder with pivot table - php

I have two tables with a pivot table
Table tours
id | name | country_id | featured
Table countries
id | name
Pivot Table country_tour
id | country_id | tour_id
I want to to find the tour that has featured column of tours table set to 1 and country_id of country_tour table set to 1.

UPDATED:
You can do it like this using Laravel's query Builder method - whereHas():
Your models should look like this (Many to Many Relationships):
Tour Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tour extends Model
{
public function countries() {
return $this->belongsToMany('App\Country');
}
}
and Country Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
public function tours() {
return $this->belongsToMany('App\Tour');
}
}
and now you can fetch the desired results by using the below query:
Tour::where('featured', 1)
->whereHas('countries', function($q) {
$q->where('id', 1);
})
->get();
This will get you the collection of tours with featured = 1 and having country with id = 1.
Hope this helps!

With reference to Saumya Rastogi answer, change the id to countries.id to avoid "column 'id' in where clause is ambiguous" error.
i.e.
From:
Tour::where('featured', 1)
->whereHas('countries', function($q) {
$q->where('id', 1);
})->get()
To:
Tour::where('featured', 1)
->whereHas('countries', function($q) {
$q->where('countries.id', 1);
})->get();

Related

laravel withCount of linear nested relations in laravel

I have tables,
ec_products : id | brand_id (id from ec_brands) | store_id (id from mp_stores)
ec_brands: id
mp_stores: id
I am calculating total products belong to each brand and store using relations and withCount of Laravel, Like,
Brand model,
public function products()
{
return $this->hasMany(Product::class, 'brand_id')->where('is_variation', 0);
}
Stores model
public function products()
{
return $this->hasMany(Product::class, 'store_id')->where('is_variation', 0);
}
And in each model,
$data = $data->withCount(‘products’);
Now I introduced categories, One product belongs to multiple categories.
So used separate table to link category and product.
ec_product_category_product: id | category_id (from category table) | product_id (from our products table)
So final question I have, How to join all,
I want to list each of these (brand,store, category) and count products of each based on request parameters.
Like
if any brands selected, then count store and category related to that brand.
if stores selected, then count brands and category related to that store.
if any category selected, then count store and brand related to that category.
structure of UI
Suggestion or solution.
Thanks
You should always start you Eloquent query with the Object you want to manipulate, here, it's Product and THEN constraint your query,
so in your controller :
$product_count = Product::when($request->category_id, function($query) use ($request){
$query->where('category_id', $request->category_id);
})
[...] //do that for each of your constraints
->count();
if you want to do a search on the name of the category for example, use a whereHas :
$product_count = Product::when($request->search, function($query) use ($request){
$query->whereHas('category', function($query) use ($request){
$query->where('name', 'like', "%$request->search%");
});
})
[...] //do that for each of your constraints
->count();

Get results from joined tables

In my laravel app I track lesson enrollment and active subscriptions in different tables.
A lesson is defined by lesson_types table which has columns like this:
lesson_types
id | name | sub_plan
Enrollemnts are defined in enrolls table with columns like this:
enrolls
id | user_id | lesson_type_id
and my subscriptions table has columns like this:
subscriptions
id | name | sub_plan | user_id
Tables are connect like this:
enrolls has 'lesson_type_id' which is 'id' of lesson_types table
sub_plan column in lesson_types is the same as sub_plan in subscriptions
Sample database data in tables:
ennrolls
id user_id lesson_type_id
1 1 1
2 1 2
3 2 1
lesson_types
id name sub_plan
1 Lesson 1 plan_1337
2 Lesson 2 plan_1338
3 Lesson 3 plan_1339
subscriptions
id name sub_plan user_id
1 Sub 1 plan_1337 1
I tried to get all enrollments like this:
$enrolled = DB::table('enrolls')->where('user_id', $userId)
->leftJoin('lesson_types', 'id', '=', 'lesson_type_id')
->select('name');
But I need to get them without subscriptions...
What i want to achieve is:
Get all 'lesson_types' names where user is subscribed
Get all 'lesson_types' names where user has enrolled but not subscribed
Get all 'lesson_types' where user is not enrolled
You can Achieve the above scenario using Laravel Eloquent. Laravel has facilitated a lot of more in-built ORM methods. So We don't need to write manual SQL codes. Don't reinvent the wheel
Enrolls Model
namespace App;
use App\User;
use App\LessonType;
use App\Subscription;
use Illuminate\Database\Eloquent\Model;
/**
*
*/
class Enroll extends Model
{
public function lessonType()
{
return $this->belongsTo(LessonType::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
Lesson Types Model
namespace App;
use App\Enroll;
use App\Subscription;
use Illuminate\Database\Eloquent\Model;
/**
*
*/
class LessonType extends Model
{
public function enrolls()
{
return $this->hasMany(Enroll::class);
}
public function subscription()
{
return $this->belongsTo(Subscription::class, 'sub_plan','sub_plan');
}
}
Subscriptions Model
namespace App;
use App\User;
use App\LessonType;
use Illuminate\Database\Eloquent\Model;
/**
*
*/
class Subscription extends Model
{
public function lessonType()
{
return $this->hasMany(LessonType::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
In Constroller or Repository
LessonType::select('name')->has('subscription.user');
LessonType::select('name')->doesntHave('subscription.user');
LessonType::select('name')->doesntHave('enrolls.users');
FYI :
Laravel Eloquent Relationships
Follow this approaches. Take it as Best Practice

How to get ID from table only if it contains multiple values needed, from different rows

I have a channel_members table containing channel_id, user_id. I need to get the channel_id if there are multiple rows using that same channel_id which contains multiple user_id that I will provide.
Example:
If there are 5 rows in the table
CHANNEL_ID | USER_ID
2 | 2
2 | 3
2 | 4
3 | 2
3 | 4
I need to get the channel_id which are being used by user_id 2, 3, and 4. So based in the table above that I provided. I should get channel_id 2.
I assume you have already defined your relations uisng models, As per your provided description, channel has many to many relation with user
class Channel extends Model
{
public function members()
{
return $this->belongsToMany('App\User', 'channel_members', 'channel_id', 'user_id');
}
}
Using eloquent realtions you can check existance of related models as
$channels = App\Channel::whereHas('members', function ($query) {
$query->where('id', '=', 2);
})->whereHas('members', function ($query) {
$query->where('id', '=', 3);
})->whereHas('members', function ($query) {
$query->where('id', '=', 4);
})->get();
Above will return you only channels who have associations with all 3 users based on supplied id
Querying Relationship Existence
SELECT CHANNEL_ID
FROM channel_members
WHERE USER_ID in (2,3,4)
GROUP BY CHANNEL_ID
HAVING COUNT(CHANNEL_ID) > 0

Laravel 4.2 : How to use order by SUM in Laravel

I have 3 tables:
Posts
--id
--post
Points
--id
--user_id
--post_id
--points
User(disregard)
--id
--username
My models are like this.
Class Posts extends Eloquent {
function points(){
return $this->hasMany('points', 'post_id');
}
}
Class Points extends Eloquent {
function posts() {
return $this->belongsTo('posts', 'post_id');
}
How can I order it so that the returning results would be ordered by highest sum of points.I also need to know how I can get the sum of points per post.
Post_id | Post | Points<-- SumPoints
5 |Post1 | 100
3 |Post2 | 51
1 |Post3 | 44
4 |Post4 | 32
Here is my Code:
$homePosts = $posts->with("filters")
->with(array("points" => function($query) {
$query->select()->sum("points");
}))->groupBy('id')
->orderByRaw('SUM(points) DESC')
->paginate(8);
May I know how it would be solved by using query builder and/or model relationships
Eloquent way:
$posts = Post::leftJoin('points', 'points.post_id', '=', 'posts.id')
->selectRaw('posts.*, sum(points.points) as points_sum')
->orderBy('points_sum', 'desc')
->paginate(8);
The Query\Builder way is exactly the same, only the result won't be Eloquent models.
I think the following query-builder should get you started..
DB::table('posts')
->join('points', 'posts.id', '=', 'points.post_id')
->orderBy('sum(points)')
->groupBy('points.post_id')
->select('points.post_id', 'posts.post', 'sum(points.points) as points')
->paginate(8)
->get();

Four-to-Many Relationship with Eloquent

I am using Laravels Eloquent ORM and i ran into a little problem with a special relationship. Lets assume i have the following table:
Recipe:
id | ... | ingredient1 | ingredient2 | ingredient3 | ingredient4
Every recipe has exactly 4 ingredients and i get the data from an external source in this specific format, thats why i have the ingredients as columns and not as a normal many-to-many relation.
I could set these up as 4 one-to-many relations, but i want to be able to write $ingredient->usedInRecipes()->get().
With 4 one-to-many relations i would have to write $ingredient->usedInRecipesAsIngredient1()->get(), [...], $ingredient->usedInRecipesAsIngredient4()->get() and merge them after afterwards, which would result in 4 queries.
If you know a good way to join these before querying the database or how to make a 4-to-many relation work please answer!
From the question I can't tell if you have already attempted this or not although as I see it you just need to use a single many-to-many relationship.
Each ingredient presumably has a common set of properties that can all be handled in one table ingredients.
id name created_at updated_at
1 Paprika 01/01/1970 00:00:00 01/01/1970 00:00:00
1 Rosemary 01/01/1970 00:00:00 01/01/1970 00:00:00
1 Basil 01/01/1970 00:00:00 01/01/1970 00:00:00
1 Oregano 01/01/1970 00:00:00 01/01/1970 00:00:00
Then your recipes table
id name created_at updated_at
1 Herb Soup 01/01/1970 00:00:00 01/01/1970 00:00:00
To hold the relationships, a pivot table ingredient_recipe
id recipe_id ingredient_id
1 1 1
2 1 2
3 1 3
4 1 4
Now all you require is a belongsToMany relationship on both your Recipe and Ingredient model.
You can code safeguards to make sure one recipe only ever has 4 relationships with ingredient if you wish but to keep it simple:
Recipe::with('ingredients')->get();
Would retrieve all the ingredients along with the recipe.
You can read more about this relationship in the documentation here.
Without Pivots
If you kept the columns ingredient_1, ingredient_2 and so on in the recipes table you could add something like this to your Recipe model.
public function scopeGetWithIngredients($query)
{
$query->leftJoin('ingredients i1', 'recipes.ingredient_1', '=', 'i1.id')
->leftJoin('ingredients i2', 'recipes.ingredient_2', '=', 'i2.id')
->leftJoin('ingredients i3', 'recipes.ingredient_3', '=', 'i3.id')
->leftJoin('ingredients i4', 'recipes.ingredient_4', '=', 'i4.id')
->select('recipes.name', 'i1.name AS ing_1', 'i2.name AS ing_2');
}
You can then just get the ingredients in your model with
Recipe::getWithIngredients();
I found a solution that seems to work in all my use cases.
In the Recipe model i defined the 4 ingredients as One-to-Many relations and made two helper scope functions.
class Recipe extends Eloquent {
public function ingredient1()
{ return $this->belongsTo('Ingredient', 'ingredient1'); }
public function ingredient2()
{ return $this->belongsTo('Ingredient', 'ingredient2'); }
public function ingredient3()
{ return $this->belongsTo('Ingredient', 'ingredient3'); }
public function ingredient4()
{ return $this->belongsTo('Ingredient', 'ingredient4'); }
public function scopeHasIngredient( $query, Ingredient $ingredient ) {
return $query-> where( 'ingredient1', '=', $ingredient->id )
->orWhere( 'ingredient2', '=', $ingredient->id )
->orWhere( 'ingredient3', '=', $ingredient->id )
->orWhere( 'ingredient4', '=', $ingredient->id );
}
public function scopeWithIngredients( $query ) {
return $query->with('ingredient1', 'ingredient2',
'ingredient3', 'ingredient4');
}
}
class Ingredient extends Eloquent {
public function ingredientForRecipes() {
return Recipe::hasIngredient( $this )->withIngredients();
}
}
To get all recipes for an Ingredient i can now call $ingredient->ingredientForRecipes()->get() and use the ingredients without extra queries.

Categories