How to fetch records from same table using group by with where condition in laravel? - laravel

I have a cartitem table I where there are two field named vendor_id and cookie_identifier I want to retrieve a specific cookie identifiers records group by vendor id..how can it possible. I write a query which is given bellow it does not work
$addTocartItems = CartItem::groupBy('vendor_id')
->where('cookie_identifier', $getCookieIdentifier)
->get();

Related

How to get specific columns from relation table in laravel?

I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well

laravel eloquent ORM Joint multiple table without Foreign key And Multiple model

I have two tables. One is user and the other is category, I have written this Eloquent ORM query to retrieves data from category
$category = Category::where(['status'=>'active'])->orderBy('id','asc')->get();
In category table here has a field called user_id. I want to join the user table and want to retrieve the user name where category table 'user_id' == user table 'id'.
How can I do this in Laravel eloquent ORM without Foreign key And Multiple models.

Query on Latest records out of multiple records in a Laravel Relation

I have two table users and users_logs .
The users_logs table is linked with users by user_id.
The users_logs table may have multiple entries against different users, like 100 records may have against one user_id.
I want to fetch all users from users table and then want to check if the last (the latest entry) in users_logs table has created_at smaller than the certain date, if yes, then fetch that records else not.
i have also created a hasMany relation in users table.
public function users_logs_details(){
return $this->hasMany('App\UsersLog', 'user_id', 'id');
}
The code i tried is
$users = User::with(['users_logs_details'])
->whereHas('users_logs_details', function($query) use ($certainDate) {
return $query->where('created_at', '<', $certainDate);
})->get();
But i am not getting any result because, this query is checking in all records in users_logs table, but i want to check only the latest record.
Please help, i would be highly thankful.

How To Delete Multiple Rows from Table Using Laravel Eloquent?

I want to delete multiple rows from my post_tags table where post_id is given .
table structure post_tags is : post_id , tag_id , created_at , updated_at
I am able to do this operation with DB query builder , but i want to
delete multiple rows with single elequent command without using forloop
I don't know how to do this ?
You should try this:
$post_id = [1,2,3];
DB::table('post_tags')
->whereIn('id',$post_id)
->delete();
You can pass Model::destroy($ids) an array. Alternatively, loop the records you want to delete and call delete on each: $model->postTags->each->delete().

Get list of joined rows with DAX

I have Supplier dimension table has 1:n relationship with InvoiceDetail fact table. I would like to get the list of active suppliers like below SQL, but in DAX language:
SELECT [Id]
,[Name]
,[Code]
,[CountryIso]
FROM [Supplier] s
WHERE EXISTS (SELECT 1 FROM [InvoiceDetail] id WHERE s.id = id.SupplierId)
I am not sure how I can do on Measure with DAX
Assuming that an active supplier means that the supplier has an invoice against them and that your data looks something like this..
Invoice Table
Supplier Table
Creating a relationship between the two tables will in effect, 'join' the two tables.
You can then use invoice number field from the invoice table and the name/code/countryiso from the supplier table.
Example being:
The value are only being drawn from the invoice table, so you'll only see active Invoices.
If being an active supplier means having a true bool value, join the tables and add a report/page wide filter on that bool value.

Resources