How to create a lot of categories for card laravel voyager - laravel

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 :

Related

Laravel sum/count nested relation

i have a problem with simple relations.
Tables looks like that:
table players
id user_id game_id
1 2 1
2 5 1
3 3 1
4 4 2
5 2 2
table games
id result (win or lose)
1 1
2 0
What i need in result is:
Players Wins Losses
John 3 2
Philip 2 2
Jack 1 3
I tried alot of queries but i cant get proper result.
This one
`"select * from `players` inner join `games` on `players`.`game_id` = `games`.`id`"`
This one is best i can do, but its raw and no idea how to rewrite it to DB:: or Eloquent. And its not grouping anyway.
you can directly count from relation with model.
$category = Category::find($id);
$category->children->count();

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

Laravel hasOne relationship where condition same table field

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).

Distinct on two columns with same data type

In my game application I have a combats table:
id player_one_id player_two_id
---- --------------- ---------------
1 1 2
2 1 3
3 3 4
4 4 1
Now I need to know hoy many unique users played the game. How can I apply distinct, count on both columns player_one_id and player_two_id?
Many thanks.
By using union you can get unique distinct value.
$playerone = DB::table("combats")
->select("combats.player_one_id");
$playertwo = DB::table("combats")
->select("combats.player_two_id")
->union($playerone)
->count();

How do i resolve conflict when selecting productid whose product visit have same occurences

My Database Table Contain Records Like this:
ID ProductId Occurences
1 1 1
2 2 5
3 3 3
4 4 3
5 5 5
6 6 8
7 7 9
Now i want to get top 4 ProductId with the highest Occurences.
This is my Query:
var data = (from temp in context.Product
orderby temp.Occurences descending
select temp).Take(4).ToList();
Now here as because ProductId 2 and 5 have same occurences and ProductId 3 and 4 also have same occurences then here I am not getting that how to resolve this means which product id should i take as because they are having same occurences.
Basically i am selecting this productid to display this products on my website.i will display those products which are return by this query.
So can anyone please give me some idea like how to resolve this ???
Expected Output:
ID ProductId Occurences
1 7 9
2 6 8
so Now for 3rd position Which ProductId i should select as because both ProductId 2 and 5 have same Occurences and for 4th position which ProductId i should select among 3 and 4 as because they both too have same occurences.
can I suggest you to use group by technique and pick the first one
context.Product.GroupBy(p => p.Occurance)
.Select(grp => grp.FirstOrDefault())
.Take(4)ToList();
Sorry, I have not tested this but it should do the job. You just need to add OrderBy along with it.

Resources