I'm newbie on laravel and lerning leftJoin.
I have table called "users" with row "steam_id".
I have another table called "matches". "matches" table have a unique row called
"match_id".
Last one table called "match_players" have row called "match_id" aswell and "steam_id.
"match_id" same as table row "matches -> match_id".
I need take out info by using user steam_id by match_id from matches.
Controller:
$user->LatestMatches = DB::table('match_players AS mp')
->leftJoin('matches AS ma', 'ma.match_id', '=', 'mp.match_id')
->where('mp.steam_id', '=', $user->steam_id)
->where('team', '=', 2)
->where('team', '=', 1)
->select("mp.*", "ma.map")
->get('');
Blade:
#foreach ($user->LatestMatches as $lastmatch)
{{ $lastmatch->map }}
#endforeach
But dont getting any info. Sorry if u don`t understand what i need todo. Thanks for replies and helping me understanding laravel!
->where('team', '=', 1)->where('team', '=', 2) doesn't work because it selects players who are in team 1 AND team 2 at the same time. No player can fulfill this constraint.
Use ->whereIn('team', [1, 2]) instead. This selects players who are in team 1 OR team 2.
Related
I am trying to compare two tables to check is user data already exits in another table. I show some posts they talk about whereRaw but details is not enough i was try to run query but i am not getting which i want.
UPDATED
I have 2 tables one is for challenges between two people
Challenge Table id,user_one, user_two, data_one, data_two,winner_id
And my second table is
vote Table id, user_id, battle_id, voted_user
I want to check is user already votes the table or not, If yes skip that challenge table and show remain table data to user.
$challenges = DB::table('challenges')->whereRaw([
['challenges.user_one', '!=', $uid],
['challenges.id', '!=', 'vote.id'],
])->orWhereRaw([
['challenges.user_one', '!=', $uid],
['challenges.id', '!=', 'vote.id'],
])->get();
You can achieve it by using crossJoin
$challenges = DB::table('challenges')->crossJoin('vote')
->where('challenges.user_one', '!=', $uid)
->where('challenges.id', '!=', 'vote.id')
->orWhere('challenges.user_one', '!=', $uid)
->orWhere('challenges.id', '!=', 'vote.id')->get();
Refer: https://laravel.com/docs/5.8/queries
I have three tables:tbl_borrower, tbl_clientandtbl_guarantor
tbl_Client:
id|name|address|email|
tbl_Guarantor:
id|clientid(fk)|Guaranteed_date|borrower_id(fk from borrower table)
I want to retrieve all the values of client table except the values which are present in guarantor table in the controller of Laravel 5.5.
Once you have the models and relationships set up, you should just do:
Client::doesntHave('guarantor')->get()
https://laravel.com/docs/5.6/eloquent-relationships#querying-relationship-absence
If you're using the query builder, it would be:
$clients = DB::table('tbl_clients')
->join('tbl_guarantor', 'tbl_clients.id', '=', 'tbl_guarantor.clientid')
->select('tbl_clients.*')
->whereNull('tbl_guarantor.clientid')
->get();
https://laravel.com/docs/5.5/queries
With the premise of using a left join and testing for NULL on the 2nd table id based on this answer. https://stackoverflow.com/a/4076157/3585500
Try this :
DB::table('tbl_Client')
->groupBy('tbl_Client.id')
->leftjoin('tbl_Guarantor', 'tbl_Client.id', '=', 'tbl_Guarantor.clientid')
->get([
'tbl_Client.*'
]);
I have two tables named 'favorites' and 'trails'. I want to show top 10 favorites trails for users. I made 'many to many' relationship between them. But, I am not sure how to make the query.What should be the right query.Would someone help me for the right one. Without relationship, I tried something like this-
$favorites = DB::table('favorites')
->join('trails', 'trails.id', '=', 'favorites.trail_id')
->select('favorites.trail_id', 'trails.name')
->get();
First one is 'trails' table and another one is 'favorites' bellow -
To get the top 10 trails you need to use aggregate function to count no of users for each trail and order your results based on the result of count and then select only 10
$favorites = DB::table('trails as t')
->select('t.id', 't.name')
->join('favorites as f', 't.id', '=', 'f.trail_id')
->groupBy('t.id')
->groupBy('t.name')
->orderByRaw('COUNT(DISTINCT f.user_id) DESC')
->limit(10)
->get();
Normally I know how to query and join three tables but here I can't figured it how to happen. The thing is that I have followed 3 tables:
category -> columns
id
name
image
sub-category -> columns
id
table1_id
name
image
Sub-sub-category -> columns
id
table2_id
name
image
So image can be added only to Category or to Category->Sub-Category or to third level Category->Sub-Category->Sub-Sub-Category
What I'm trying to do is when I show ALL images on page to show also to which category they are added.
I've made all relations in my models but I can't figured it out how to query exactly. Currently only querying all images like
SubSubCategories::all();
Example:
I have main category(Country) which has sub-category (district) which has sub-sub-category(city).
image1 is added to Sub-Sub-Category with name city. When images are listed I want to show
images1 added to Country->District->City.
Can someone show me example query or maybe my relations eg. columns in tables are wrong?
This is what I've tried so far
$subsubcategories = DB::table('sub_sub_category')
->join('sub_category', 'sub_sub_category.sub_cat_id', '=', 'sub_category.sub_cat_id')
->join('category', 'category.category_id', '=', 'sub_category.sub_cat_id')
->select('sib_sub_category.*', 'sub_category.*', 'category.*')
->get();
Then in my view
{{ $subsubcategory->sub_category->sub_cat_name }} > {{ $subsubcategory->sub_sub_cat_name }}
error
Undefined property: stdClass::$sub_category
$subsubcategories = DB::table('category')
->join('category', 'category.id', '=', 'sub_category.table1_id')
->join('sub_category', 'sub_category.id', '=', 'sub_sub_category.table2_id')
->select('category.*', 'sub_category.*', 'sub_sub_category.*')
->get();
Actually you are almost there. Just remove the sub_category from the view and will work.
{{ $subsubcategory->sub_cat_name }} > {{ $subsubcategory->sub_sub_cat_name }}
This is happen because your array currently is looks like
object(stdClass) {
["id"]=> int(1)
["table2_id"]=> int(1)
["table1_id"]=> int(1)
...
// all other columns
}
So actually they belongs to $subsubcategory and no need to bring sub_category and category in the view. Just display them in your $subsubcategory.
I have a query that will get the data from a joined tables. I successfully fetched the data from the 2 tables but for a long time, I did not notice that only one primary id of a certain table has been returned. I made adjustments but still never figure it out. What would I do? Please help. Thanks a lot guys. Here is my code.
$purchase = Purchase::where('purchases.purchase_order_id', $id)
->join('products', 'purchases.product_id', '=', 'products.id')
->select('purchases.*', 'products.*')
->get();
It only returns the primary id of a product, primary id of the table purchases is not included. What is the problem of the query above?
You can use select as:
->select('purchases.*', 'purchases.id as purchase_id', 'products.*', 'products.id as product_id')
This query returns the IDs of Survey Table as surveys_id as well as IDs of industries table along with all other data:
surveydata = Survey::select(
'surveys.*',
'surveys.id as surveys_id',
'industries.*',
'industries.id as industries_id'
)->where('surveys.active_status', '=','1')
-> join (
'industries',
'industries.id',
'=',
'surveys.survey_industry_id'
)->orderBy('surveys.id','desc')
->paginate(12);