Laravel orderBy with same value result keep changing - php

I've try to sort my result within the updated_at value. Because I seed the value all the updated_at value is same:
// Amount Table
-----------------------------------------------------
| id | amount | updated_at |
+----------+--------------------+-------------------+
| 1 | 232319 |2016-02-02 13:17:29|
| 2 | 232319 |2016-02-02 13:17:29|
| 3 | 100000 |2016-02-02 13:17:29|
| 4 | 231111 |2016-02-02 13:17:29|
| 5 | 12345 |2016-02-02 13:17:29|
+----------+--------------------+-------------------+
The problem is everytime I query :
$lists = Amount::orderBy('updated_at', 'DESC')->lists('id')->toArray();
I expect the result to be like this: [1,2,3,4,5] but it appear the result is sometime like that but sometime [2,3,1,4,5]. it keep randoming the result.
As far as i know, yes i can put another orderBy statement to the query, but isn't it suppose to be default it will order by the id?
Thanks

You are only ordering by updated_at which appear to all have the same value. If you want consistency, include a second column
$lists = Amount::orderBy('updated_at', 'DESC')->orderBy('id', 'ASC')->lists('id')->toArray();

Related

How to get data from 3rd level table using joins [Laravel]

I want to get quantity of the product which is stored in bd_product_item_ledgers using the primary key (ID) of bd_products
bd_products is linked to bd_product_ledgers
Then, bd_product_ledgers is linked to bd_product_item_ledgers
table: bd_products
---------------------------------------------------------------------------------
| id | name | description | image | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | shoe | this is a shoe | 15243.jpg |
---------------------------------------------------------------------------------
table: bd_product_ledgers
---------------------------------------------------------------------------------
| id | product_id | cost | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | 1 | 500.00 |
---------------------------------------------------------------------------------
table: bd_product_item_ledgers
---------------------------------------------------------------------------------
| id | product_ledger_id | quantity | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | 1 | 200 |
---------------------------------------------------------------------------------
I can get the cost of the product because there is ID present there, but how can i get the quantity also?
Simple MySQL Query Or Laravel eloquent etc anything would be good.
Thanks!
create three models Product,Item,ledgers and define your relationship ledgers in product model and item in ledgers model than you can get data using with query like,
$data = Product::with('ledgers.item')->get();
I am not Sure but i have created demo project with thr following tables and enterd the data provided by you
ITS PURLY DB FACADE VERSION
[IF YOU NEED IN ELOQUENT WAY PLEASE SHARE
YOUR MODEL NAMES]
public function index()
{
$selctArray = [
'bd_products.name',
'bd_products.description',
'PRDLED.cost as productCost',
'PRDITLED.quantity as productQuantity',
];
$joinQuery = DB::table('bd_products')
->leftJoin('bd_product_ledgers as PRDLED','PRDLED.product_id','=','bd_products.id')
->leftJoin('bd_product_item_ledgers as PRDITLED','PRDITLED.product_ledger_id','=','PRDLED.id')
->select($selctArray)->get();
dd($joinQuery);
}
please comment below if it has not satisfied your requirement and improve your question

Laravel Eloquent GroupBy Name to Create array of ID's

I have a table in my Laravel application which I wish to query.
id | company_name | contact |
-----------------------------
1 | Tesco | Name 1 |
2 | Tesco | Name 2 |
3 | Asda | Name 3 |
4 | Tesco | Name 4 |
5 | Asda | Name 5 |
I'm trying to get an array of all unique company names with all ID numbers.
'Tesco' => [1,2,4]
'Asda' => [3,5]
I have tried
$companies = Contact::select('company_name','id')->groupBy('company_name')->get();
However this requests the 'id' to be included in the group by which defeats the purpose. I understand it's asking for this because it's not a SUM or COUNT etc.
The above table may seem unusual and I know I should have a relation to a companies table however this is necessary at this stage.
You could use GROUP_CONCAT()
$companies = Contact::select('company_name', DB::raw('GROUP_CONCAT(id) as ids'))
->groupBy('company_name')
->get();
This would return something like:
company_name | ids
Tesco | 1,2
Edit: if you want the ids in the form an array, you could just map over the collection to convert it:
$companies->map(function($column) {
$column->ids = explode(',', $column->ids);
});
That should do the trick.

How to display the previous row and next row from varchar value

I have hundreds rows of data. I want to get the previous row and the next row from the current item. I saw many examples here but none solve my problem.
My data key is a set of string (md5) which is unable to compare the greater or lower like the other does. Here's my resources.
---------------------------------------------------
| id| sid | name |
----+----------------------------------+----------|
| 1 | c81e728d9d4c2f636f067f89cc14862c + Mr.A |
----+----------------------------------+----------|
| 2 | eccbc87e4b5ce2fe28308fd9f2a7baf3 | Mr.B |
----+----------------------------------+----------|
| 3 | a87ff679a2f3e71d9181a67b7542122c | Mr.C |<current position
----+----------------------------------+----------|
| 4 | e4da3b7fbbce2345d7772b0674a318d5 | Mr.D |
----+----------------------------------+----------|
| 5 | 1679091c5a880faf6fb5e6087eb1b2dc | Mr.E |
--------------------------------------------------|
So, is there any way to get the previous row (Mr.B) and the next row (Mr.D) with mysql?
I've tried
SELECT * FROM table
WHERE sid < #sid
ORDER BY sid DESC
LIMIT 1
but it's not work because sid is uncomparable.

Sql – Insert value in first empty cell of column and show ID

I have an sql statement that will insert a value to the first empty cell. And if I ran the php script again, then it inserts into the next null cell etc.
Problem: I also want to find out the ID of that row, and value of another column in that row. In the Mysql table below, I want a value inserted in the first ‘null’ of COLUMN A, and also know the id and value in COLUMN B corresponding to that (ie id=3 and COLUMN B= 11).
My_TABLE
+---------+--------------+-------------+
| ID | COLUMN A | COLUMN B |
+---------+--------------+-------------+
| 1 | 6 | 78 |
| 2 | 7 | 90 |
| 3 | NULL | 11 |
| 4 | NULL | 5 |
| 5 | NULL | 123 |
+---------+--------------+-------------+
The following sql statement in PHP script will make it possible to insert value to the first empty cell in COLUMN A:
UPDATE My_TABLE
SET COLUMN A = 83
WHERE COLUMN A IS NULL
LIMIT 1;
Result will be:
+----+----------+------------+
| ID | COLUMN A | COLUMN B |
+----+----------+------------+
| 1 | 6 | 78 |
| 2 | 7 | 90 |
| 3 | 83 | 11 |
| 4 | NULL | 5 |
| 5 | NULL | 123 |
+----+----------+------------+
I also want to have an sql script that will print within PHP (echo) the values of ID and COLUMN B values corresponding to the first COLUMN A null value (ie ID= 3; COLUMN B= 11).
fetch the row by condition in this case you will have ID and COLUMN B
select *
from My_TABLE
where COLUMN A IS NULL
order by id
limit 1
Update by ID the selected row:
update My_TABLE
set COLUMN A = :SOME_VALUE
where ID = :ID_FROM_FETCH
Not sure if this case will fit what you are questioning.
mysqli_insert_id
From this function I'm pretty sure you will able to write the script for what you need.
Note* If it fits what you need, please read the warning as I'm not
sure if it will deprecated from the latest PHP version. Kindly take
note.

Finding a specific row in a PropelCollection

I have two tables. One for orders, and for their prefs. The tables look like this:
Order:
+----------+---------------+------------+
| orderID | orderNumber | clientID |
+----------+---------------+------------+
| 1 | abc123 | 2 |
| 2 | orderX | 7 |
| 3 | Joe9 | 2 |
| 4 | Order4 | 2 |
+----------+---------------+------------+
OrderPref
+----------+----------+-------------+
| orderID | prefID | prefValue |
+----------+----------+-------------+
| 1 | 1 | $100 |
| 1 | 2 | 123 |
| 1 | 3 | $35 |
| 2 | 1 | $600 |
| 2 | 2 | 876 |
| 2 | 3 | $44 |
+----------+----------+-------------+
What I want to is for each order, get the prefValue for a specific prefID. Currently, this is what I am doing:
$orders = OrdersQuery::create()->filterByClientID(2)->find();
foreach($orders as $o){
$prefs = $o->getOrderPrefs();
foreach($prefs as $p){
if($p->getPrefID() === 2){
echo $p->getPrefValue();
break;
}
}
}
This works, but there needs to be a better way to get the one row I want for each order without looping through all the prefs.
I know this doesn't work, but is there something like this?
$orders = OrdersQuery::create()->filterByClientID(2)->find();
foreach($orders as $o){
// This obviously doesn't work, so is there a short way to do this?
echo $o->getOrderPrefs()->filterByPrefID(2)->getPrefValue();
}
I was reading the docs and found a ->search() method, but I don't know how to use it.
$orders = OrdersQuery::create()->filterByClientID(2)->find();
foreach($orders as $o){
// How can I search for the row with the prefID I want?
echo $o->getOrderPrefs()->search()->getPrefValue();
}
Looking at some old Propel stuff I've written, I'm guessing something like:
$prefValue = OrdersQuery::create()->
joinOrderPref()->
where('OrderPref.prefID = ?', $prefId)->
filterByClientID($clientId)->
select('OrderPref.prefValue')->
find()
;
The issue is that you need to get a specific column (reference).
For these chainable queries, my view is that an auto-completing editor is pretty much mandatory - remembering the syntax is nigh-on impossible without it.
$prefValues = OrdersQuery::create()
->filterByClientId(2)
->joinOrderPref()
->withColumn('OrderPref.PrefValue', 'theValue')
// row above fetches ONLY the needed column from joined table and
// gives 'theValue' alias to it
->select('theValue')
->find();
With this you don't need to do any foreaches in PHP or do it in two steps.
About the ->select() part - it's there only to not get the fully bloated/hydrated object, but actually only an array (PropelArrayCollection to be precise) with the prefValue data you need.
I didn't test this live though, some syntax issues may arise.

Categories