Laravel hasOne relationship where condition same table field - laravel

I have 5 tables retailers, doctors, softwareproviders, members and contactpersons. contactpersons table associated with other 4 tables using type and refid.
retailers
id
name
contactno
mem_type
2
karthik
855550
1
members
id
name
contactno
6
naveen
855550
contactpersons
id
type
refid
name
1
1
2
ruban
1
2
6
aron
type 1 - retailers, 2 - members, 3 - doctors, 4 - softwareproviders
In contactpersons model how to make hasone relationship with other tables
public function retailersinfo(){
return $this->hasOne('App\Retailers','id','refid');
}
we need to add type condition (where condition =1).

Related

How to create a lot of categories for card laravel voyager

image
image2
Now I have like this
image3
but I want have more categories
like this
How to select many category?
Let’s say in your case, we need to define the category for every features. So feature can have many categories and inverse category can have many features. Thus, it will be a Many To Many relationships.
To accomplish this we need to create 3 tables features, categories, and intermediate table feature_category. The feature_category table will have the feature_id and category_id column which connects both feature and category table and this intermediate table called the pivot table.
Here are the table structures:
features
id - integer
name - string
categories
id - integer
name - string
feature_category
feature_id - integer
category_id - integer
=> category
id name
-- -------
1 Category 1
2 Category 2
3 Category 3
4 Category 4
=> features
id name
-- -------
1 Feature 1
2 Feature 2
3 Feature 3
4 Feature 4
=> feature_category
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
3 3 2
4 3 3
4 3 4
===============================
Our feature_category table before sync() operation:
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
3 2 3
4 2 4
5 3 2
6 4 3
Laravel Sync() example:
<?php
use App\Models\Feature;
$user = Feature::find(2);
// Want to keep only "Category 2" (Id 2) category
$user->category()->sync([2]);
After performing the above operation, our feature_category table will look like below:
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
5 3 2
6 4 3
Checkboxes or dropdowns can be used from the frontend to select multiple categories for features and sync() method can be used to update feature cards accordingly.
First you need to create new table feature_category with two fields :
(same type of features.id) feature_id
(same type of categories.id) category_id
Second create belongsToMany relationship directly in Voyager.
Example :

Eloquent query relation laravel

I have this database structure, I trying to query all the redeems with stores and store's district, so far I can only get redeems with relation to stores
$redeems = Redeem::with('user', 'store')->get();
districts
id
district
1
District1
2
District2
stores
id
store
ditrict_id
1
Store1
1
2
Store2
2
redeems
id
store_id
1
1
2
2
How can I get the district querying from redeems?

Laravel Eloquent condition to fetch hierarchy level

Laravel Eloquent condition to fetch hierarchy level.
I have a table named routes and it is self-referencing
Sample data:
(PK)
Id title route_id
1 users null
2 home null
3 foo 1
4 bar 3
5 hoge 3
I would want to have a function to get routes according to its hierarchy
$this->routes->getByHierarchyLevel(1);
// results
Id title route_id
1 users null
2 home null
$this->routes->getByHierarchyLevel(2);
// results
Id title route_id
3 foo 1
$this->routes->getByHierarchyLevel(3);
// results
Id title route_id
4 bar 3
5 hoge 3
Is there a single chained query builder/eloquent builder possible for this one?
I already made a custom function of this one but it is a looped query or the other solution is fetch all and filter.
I would suggest to introduce a new column level in your routes table and at the time of insertion detect their nested level and save it in your table, this way you can perform your filters easily without any extra overhead , otherwise you would need complex/expensive queries to be able to detect their level and apply filter
id
title
route_id
level
1
users
null
1
2
home
null
1
3
foo
1
2
4
bar
3
3
5
hoge
3
3

Get all values from pivot table based on another column in same pivot table in Laravel

I was wondering how to get all the values from a pivot table based on another column from the same table. For example, let's consider we have two tables i.e
Product
User
product_user (Pivot table)
The pivot table column are: product_user 1. user_id 2. product_id 3. type (value can be 1 or 2)
Say I want to retrieve all the rows from the table where user_id is 1 and type is also 1
Example data:
user_id product_id type 1 1 1 1 3 1 1 76 1 2 2 2 1 21 1 1 33 2 1 23 2 1 25 1
Expected result is all the list of products based on user_id as 1 and type 1
Actually I'm answering my own question here, since I found the exact way to do it
$userId = 1;
$type = 1;
$user = User::where('id', $userId)->firstOrFail();
$products = $user->products() ->where('type', $type)->get();
Considering that products() many-to-many relation is set Inside User Model:
public function products() {
return $this->belongsToMany(Product::class)
->withPivot(['type'])
->withTimestamps();
}

How to get count from pivot table laravel?

I have two tables,
User cities user_cities
id name id name user_id city_id
1 aaa 1 cityA 2 1
2 bbb 2 cityB 2 3
3 ccc 3 cityC 1 2
3 3
3 2
Now i am filtering the user by cities. I am having the city_id = 2. So that i have to get the count of users belongs to the city_id 2.
How can i get that using laravel eloquent
As you describe in your answer you have the third table user_cities.
If you have table user_cities then count user like
DB::table('user_cities')->where('city_id', '2')->count();
There are different ways to do this, check https://laravel.com/docs/5.8/queries
But a simple way of doing it could be:
UserCity::whereCityId(2)->count();
Or:
UserCity::where('city_id',2)->count();
If you have set correctly the relationship between them, you can do something like that:
City::where('id', '=', 2)->withCount('users')->get();
It will place a {relation}_count column on your resulting model, and thus you can call it like a property: $city->users_count
Edit: For further information, you can check the official documentation.
Try this query :
$count = City::where('id', 2)->whereHas('users')->count();

Resources