My database schema is
links:
id status user_id url
1 1 1 something
2 1 1 something
3 1 1 something
links_type:
id link_id links_type
1 1 external
2 1 external
3 1 internal
4 1 external
5 2 internal
6 2 external
7 2 internal
8 2 external
i want to take data of all links which status is 1 and user_id is 1
and count external and internal links and which external count is >2.
by using laravel 5.2 eloquent.
result should be like this from data given
id status user_id url external_links internal_links
1 1 1 something 3 1
Just define this relationship in Link Model
public function link_type()
{
return $this->hasMany('App\linkType');
}
and use this query
Link::where('status','=','1')->where('user_id','=','1')->has('link_type','>', '2')->with('link_type')->get();
If you already have the right migrations with fk's and Models following code should be working:
$links = Link::where('status','=','1')->where('user_id','=','1')
->whereHas('links_type',function ($query) {
$query->whereNotIn('external_count', [0,1]);
})->get();
Probably should add with('links_type') for eager_loading (N+1 problem):
$links = Link::with('links_type')->where('status','=','1')->where('user_id','=','1')
->whereHas('links_type',function ($query) {
$query->whereNotIn('external_count', [0,1]);
})->get();
maybe this could work.
before you must create hasMany relation for Link and name as type
$links = Link::where('status', 1)
->wherer('user_id', 1)
->whereHas('type', function($query) {
$query->where(
$query->where('links_type', 'external')->count(), '>', 2
);
})
->get();
Related
$device = SalesItem::where('type', '=', 1)->get()->groupBy('product_id');
There is a list of products in the database. Here I am storing the product id. The same product can be idsi. I want to get the id data of the product with the same id at most.
Sample:
ID type product_id
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
6 1 2
7 1 2
8 1 1
9 1 1
0 1 1
output: 1
here I want the output to give product id 1. I couldn't find what to do after group by. Can you guide me on this?
I don't think there is an elegant way to do this in, but this should work for you:
Note: I'm unsure of your table name.
$device = DB::table('sales_item')
->select('product_id', DB::raw('COUNT(product_id) AS magnitude'))
->where('type', 1)
->groupBy('product_id)
->orderBy('magnitude', 'DESC')
->limit(1);
It goes into the table and selected the id, type, and the COUNT() of product_id. Which it then groups by the product_id and orders by the count('product_id') from high to low.
->limit(1) is used to only select the first (read highest) value.
I have the following piece of code
$not_paid = Tenant::where('property_id', $property_id)
->whereNotExists(function ($query) use ($property_id) {
$query->select('user_id')
->from('rent_paids');
})
->get();
which is supposed to get all the tenants in a certain property, look them up in the rent_paids table and return the users who are not in the rent_paids table, as follows:
tenants table
Id
user_id
property_id
1
1
1
2
2
1
3
3
1
rent_paids
Id
user_id
property_id
amount_paid
1
1
1
3000
I want to be able to return the users in the tenants table and not in the rent_paids table. In this case, users 2 and 3. But the above code returns an empty array.
You're missing the where clause to tie it back to the original table.
$not_paid = Tenant::where('property_id', $property_id)
->whereNotExists(function ($query) use ($property_id) {
$query->select('user_id')
->from('rent_paids')
->whereColumn('tenants.user_id', '=', 'rent_paids.user_id');
})
->get();
I have two tables
field_values (with some data)
id field_id value label sort
1 1 1 Men 1
2 1 2 Women 2
3 2 3 Relationship 1
4 2 4 Chat 2
5 2 5 Friendship 3
user_interests (with some data)
user_id field_id value_id
1 1 1
1 2 4
1 2 5
I am trying to write a query where I will get user with id 1 and have field_id 2 and to be able to echo in my blade value_id 4 and 5 but not to echo those ids but to echo value of 'label' column that corresponds to value_id form user_interests table in this case 4,5 thus Chat, Friendship from field_values table in this example. Here is what I tried but I get array of six elements which are Relationship, Chat, Friendship x2. Any help is appreciated.
query:
public static function queryFunction($userId)
{
$results = DB::table('user_interests as uin')
->select(DB::raw("
fv.*,
uin.field_id, uin.value_id
"))
->join('field_values as fv', 'fv.field_id', '=', 'uin.field_id')
->where('uin.field_id', 2)
->where('uin.user_id', $userId)
->get();
dd($results);
return $results;
}
What about 2 clear steps, without join:
$user_interests = DB::table('user_interests')->select('value_id')->where('field_id', 2)->where('user_id', $userId)->get();
From this take values as array ($user_interests_values)
and than
$results = DB::table('field_values')->whereIn('value', $user_interests_values)->get();
I would like to get a data from a table to another table using it's primary key.
products_table:
id name type_id date_added
1 item1 2 1234
2 item2 2 5678
3 item3 1 0000
product_type_table:
id type_name
1 type1
2 type2
3 type3
I am querying the first table using this code:
Products::select('name','type_id')->where('id',$id)->get()
Now, how do I automatically get the type_name from the product_type_table using the type_id from the products_table?
As Jack Skeletron pointed out, the Laravel documentation has examples for joining 2 or multiple tables.
In your case
$products = DB::table('products_table')
->join('product_type_table', 'products_table.type_id', '=', 'product_type_table.type_id')
->select('products_table.name, products_table.type_id')
->where('id', $id)
->get();
you can use left join.
DB::table('product_type_table')
->leftJoin('products_table', 'product_type_table.id', '=', 'products_table.type_id ')->where('product_type_table.id',$id)
->get();
In Product Model add Below code for joining 2 tables:
use App\Models\ProductType;
public function type()
{
return $this->belongsTo(ProductType::class);
}
and change the line:
Products::select('name','type_id')->where('id',$id)->get()
to
Products::select('name','type_id')->with(['type' => function($q){
return $q->select('type_name');
}])->where('id',$id)->get()
Hope this works for you.
Say I have a reservations table
id - restaurant_table_id - guest_id
1 - 3 - 5
2 - 4 - 7
And then an orders table
id - reservation_id - item_id
1 - 1 - 2
2 - 2 - 4
How can I pull orders that belong to a restaurant table?
I have tried:
$orders = ReservationOrders::with(['reservation' => function ($query) use ($restaurant_table_id) {
$query->where('restaurant_table_id', $restaurant_table_id);
}])
->get();
But the results included orders for all restaurant tables.
You can filter on relation's attributes using Eloquent's whereHas() method - you can get more information here: http://laravel.com/docs/5.1/eloquent-relationships
In your case sth like the code below should help:
$orders = ReservationOrders::with('reservation')->whereHas('reservation', function($query) use($restaurant_table_id) {
$query->where('restaurant_table_id', $restaurant_table_id);
})->get();