Codeigniter query top store by price and and field by today - codeigniter

I am the beginner of codeigniter.
I have a query like this, I want to use this query in codeigniter
I would like to get top store_name by total_price
+---+------------+-------------+-------------+---------------------+
| id| store_name | quality | total_price | time |
+---+------------+-------------+-------------+---------------------+
| 1 | store_a | 2 | 300 | 2019-06-19 08:20:57 |
| 2 | store_b | 1 | 100 | 2019-06-20 08:30:57 |
| 3 | store_c | 4 | 50 | 2019-06-20 08:33:57 |
| 4 | store_b | 2 | 300 | 2019-06-20 08:35:57 |
+---+------------+-------------+-------------+---------------------+
I have try
$this->db->select('store_name, SUM(total_price) AS t_price', false);
$this->db->group_by('store_name');
$query = $this->db->get($this->table_name);
return $query->result();
i need output by date time today
+------------+-------------+-------------+---------------------+
| store_name | quality | total_price | time |
+------------+-------------+-------------+---------------------+
| store_b | 3 | 400 | 2019-06-20 08:30:57 |
| store_c | 4 | 50 | 2019-06-20 08:33:57 |
Update
work for me
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('store_name, SUM(total_price) AS total_price ,SUM(quality) AS quality', false);
$this->db->group_by('store_name');
$this->db->where('DATE(time)',$curr_date);
$this->db->order_by('total_price', "DESC");
$query = $this->db->get($this->table_name);
return $query->result();

You can try this solution for your problem :
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('store_name, SUM(total_price) AS total_price ,SUM(quality) AS quality', false);
$this->db->where('DATE(time)',$curr_date);
$this->db->group_by('store_name');
$this->db->order_by('total_price', "DESC");
$query = $this->db->get($this->table_name);
return $query->result();
It will helpfull to you.

Related

Laravel filter date range with date range columns

If the selected dates ($postFr, $postTo) are within defined periods, I want to list the related products. So can I do something like the following example?
example:
$q->whereBetween('start_date', array($postFr, $postTo));
$q->orWhereBetween('end_date', array($postFr, $postTo));
//Could there be a similar use like this???;
$q->whereBetween(array(start_date, end_date), $postFr);
$q->orWhereBetween(array(start_date, end_date), $postTo);
Detailed explanation:
I have a periods table like this
periods
| id | start_date | end_date | product_id |
|------|--------------|------------|------------|
| 1 | 2021-02-19 | 2021-03-21 | 1 |
| 2 | 2021-02-19 | 2021-03-21 | 2 |
| 3 | 2021-02-19 | 2021-03-21 | 3 |
| 4 | 2021-02-19 | 2021-03-21 | 2 |
and I have a products table like this
products
| id | name |
|-------|-----------|
| 1 | pro1 |
| 2 | pro2 |
| 3 | pro3 |
The relevant codes on my model page are as follows
public $belongsTo = [
'relationPeriod' => [
'Model->periods table',
'key' => 'id',
'otherKey' => 'product_id',
]
]
$query->whereHas('relationPeriod', function($q) use ($postFr, $postTo){
$q->whereBetween('start_date', array($postFr, $postTo]));
$q->orWhereBetween('end_date', array($postFr, $postTo));
});
it works
$q->whereDate('start_date', '<=', $postFr);
$q->whereDate('end_date', '>=', $postFr);
$q->orWhereDate('start_date', '<=', $postTo);
$q->whereDate('end_date', '>=', $postTo);
This will work:
$start_date = '2021-01-01';
$end_date = '2021-02-01';
$filter = Atom_juice_data_history::whereBetween('created_at',[$start_date, $end_date])
->where('atom_juice_data_id',$product_id)
->orderBy('created_at', 'ASC')
->get();
This is reference url
https://medium.com/#sectheater/how-to-use-laravel-wherebetween-method-elegantly-20a50f3b0a2a

Laravel - Get Data from Table where clause in other table

I have this following 2 tables:
Table: Products
+----+---------+-------------+
| id | fn_id | created_at |
+----+---------+-------------+
| 1 | 4 | SOME TIME |
| 2 | 5 | SOME TIME |
| 3 | 6 | SOME TIME |
| 4 | 10 | SOME TIME |
| 5 | 10 | SOME TIME |
+----+---------+-------------+
Table Fn
+----+---------+-------------+
| id | fn_id | created_at |
+----+---------+-------------+
| 1 | 10 | SOME TIME |
| 2 | 11 | SOME TIME |
| 3 | 12 | SOME TIME |
| 4 | 14 | SOME TIME |
+----+---------+-------------+
And a User Input which is giving me a timestamp ($user_timestamp).
Now I need to get all produtcs, where
products.fn_id is 10
fn.fn_id is 10
fn.created == $user_timestamp
The products model has this relation:
public function fn() {
return $this->hasMany('App\FN', 'fn_id', 'fn_id');
}
Now I've tried multiple things like a where query where I want to check if the fn_id on both are "10" and the created_at value of the fn table is equal to $user_timestamp.
However, I wasn't able to do it.
$products = Products::with('fn')->where('fn.fn_id', function ($query) use ($user_timestamp) {
$query->where([['fn_id', 10],['created_at', $user_timestamp]]);
})->get();
You would have to use whereHas to constrain Products based on fn.
$products = Products::with('fn')->whereHas('fn', function($q) use($user_timestamp){
$q->where('fn_id', 10)->where('created_at', $user_timestamp);
})->get();
try this
$id_to_search=10;
$products = Products::with('fn')->where('fbn_id',$id_to_search)->whereHas('fn', function($q) use($user_timestamp,$id_to_search){
$q->where('fn_id', $id_to_search)->where('created_at', $user_timestamp);
})->get();

How to show filed in distinct codeigniter

Hello guys I would like to distinct field product_code and I would like to show all field
Table Transaction :
---------------------------------------------------
| id | product_code | price | datetime |
---------------------------------------------------
| 1 | 001 |20 | 2018-18-12 09:09:09 |
| 2 | 002 |30 | 2018-18-12 08:09:09 |
| 3 | 001 |20 | 2018-18-12 08:08:08 |
---------------------------------------------------
This is my model :
$this->db->distinct('product_code');
$this->db->select('id','product_code','price','datetime');
$this->db->from($this->table);
return $this->db->get()->result_array();
You can try this solution for you problem :
$this->db->distinct();
$this->db->select('id','product_code','price','datetime');
$this->db->from($this->table);
return $this->db->get()->result_array();
Than
echo $query->num_rows();
OR
$this->db->select('DISTINCT(product_code), ');
$this->db->from($this->table);
$query=$this->db->get();
print_r($this->db->get()->result_array());
echo $query->num_rows();exit;
I hope it will help you.

I need to write eloquent query that return messages having specific tag

messages Table
+----+----------+
| id | text |
+----+----------+
| 1 | message1 |
| 2 | message2 |
+----+----------+
tags table
+----+-----------+
| id | name |
+----+-----------+
| 1 | valentine |
| 2 | funny |
| 3 | santa |
+----+-----------+
message_tag
+----+------------+--------+
| id | message_id | tag_id |
+----+------------+--------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
+----+------------+--------+
I have written this query so far to get all messages but i cant return the
message where tag_id = 1;
Message::with([
'tags'=>function($q){
$q->select("name","tag_id");
}
])->select("id", "text")->paginate(10);
i have used where clause inside function($q){$q->where('tag_id',1)}. but it didn't worked.
You have to include use Illuminate\Support\Facades\DB; at the top of class
$tagId = 1;
$return = DB::table('message_tag')
->join('messages', 'messages.id', '=', 'message_tag.message_id')
->join('tags', 'tags.id', '=', 'message_tag.tag_id')
->where("message_tag.tag_id", "=", $tagId)
->select('messages.text', 'tags.text')
->get();
Try using whereHas(), assuming you set a many-to-many relationship properly:
$tag = 'Madhab452';
$messages = Message::whereHas('tags', function ($query) use ($tag) {
$query->where('name', '=', $tag)
->select('name', 'tag_id');
})->get();

Codeigniter Datamapper save as new id

I'm new to datamapper. I have a problem on trying to duplicate a result into a new id.
This is a simplified table for my database:
Job Table
| id | property_id | name | type |
| 1 | 1 | abc | i |
| 2 | 2 | def | ii |
Property Table
| id | job_id | size |
| 1 | 1 | 90 |
| 2 | 2 | 40 |
How can I automatically duplicate a new job based on job id 1 into new job/property id like
Job Table
| id | property_id | name | type |
| 1 | 1 | abc | i |
| 2 | 2 | def | ii |
| 3 | 3 | abc | i |
Property Table
| id | job_id | size |
| 1 | 1 | 90 |
| 2 | 2 | 40 |
| 3 | 3 | 90 |
Thanks for helping! :)
In the documentation for DataMapper Overzealous Edition: http://datamapper.wanwizard.eu/pages/clonecopy.html There's clone and copy, copy will clear the id. Here's their example, just skip the part of making changes:
// Let's save a new hosting plan
$p = new Plan();
$p->name = 'The 100GB Plan';
$p->storage = 1000;
$p->bandwidth = 2000;
$p->databases = 5;
$p->domains = 5;
$p->emails = 50;
$p->save();
// Now, lets make a copy of that saved plan and base a new one off of it
$p = $p->get_copy();
// Change only what we need to
$p->name = 'The Big 150GB Plan';
$p->storage = 1500;
$p->bandwidth = 2500;
// And now save a new record
$p->save();
You can also just modify the object you retrieve, and then use save_as_new() to save it as a new record.

Resources