I am trying to access a SINGLE VALUE from a row but the dd(); is getting the whole row and showing it in the Collection array. The code:
$last_id = \App\Cat::limit(1)->orderBy('cat_id','desc')->get(['cat_id']);
dd($last_id);
So when I need the JUST "55" value I get :
"cat_id" => "55"
Same happens with others columns, when I need JUST the "Eletronics" I get:
"cat_name" => "Eletronics"
I have already tried lots of stuff like Limit, List, First and nothing happens, when I try to call something like dd($lastId->cat_id ); it gives me a "Undefined property" error. SO now I am really out of options since I am using the documentation example and even that way it does not works fine. So any help would be great, thank you.
I think you don't understand that the Model represents entire row of table.
That means that $last_id in your code:
$last_id = \App\Cat::limit(1)->orderBy('cat_id','desc')->get(['cat_id']);
represents entire row of cats table. If you want to get id, you have to do this:
$cat = \App\Cat::first(1)->orderBy('cat_id','desc')->get();
dd($cat->id);
What more, if you write
$cat = \App\Cat::first();
you will get first cat from cats table, and then you can access every column of this row as property of $cat object
If Laravel says you that property cat_id is undefined, propably your table don't contain cat_id column.
Already got it, i change the get for the "value" method.
\App\Cat::orderBy("id","desc")->value("id");
Related
I have a custom table in my wordpress install, and I want to get an average out of a column on the table. I'm using the following PHP:
$latavg = $wpdb->get_results("SELECT AVG(stop_lat) FROM stops_txt");
However, when I want to use the average later on as a string, it returns as 'ARRAY', yet attempting to use PRINT_R to view the array reveals nothing. The Column in the table is a DECIMAL data-type, what am I missing?
When you just want a single variable instead of a whole row or column in wpdb, use get_var() instead:
$latavg = $wpdb->get_var("SELECT AVG(stop_lat) FROM stops_txt");
Here is the complete wpdb reference.
I have an external database that I am trying to access from within a Drupal page, I have successfully queried the database and output data to the page using fetchAssoc(), however this only returns the first row in the database. I would like to return all rows into an array for processing, so I'm attempting to use fetchAllAssoc(), this however results in an exception. The database has the following SQL fields:
id, model, manufacturer, url, date_modified
My test code is as follows:
<?php
db_set_active('product_db');
$query = db_select('product', 'p')->fields('p');
$sqlresults = $query->execute()->fetchAllAssoc('id');
foreach($sqlresults as $sqlresult)
{
printf($sqlresult);
}
db_set_active();
?>
I'm thinking that it is the key field 'id' that I am specifying with fetchAllAssoc() that is the problem, as fetchAssoc() prints values correctly. All documentation I have found seems to say that you pass a database field as the key but I have also passed a numeric value with no success.
Many thanks in advance for any advice, I'm sure I'm just missing something stupid.
I think it should work in this way, but within the foreach you want to print the $sqlresult variable as a string, but it is an object (it causes the error).
printf function needs a string as the first parameter, see:
http://php.net/manual/en/function.printf.php
Use for instance var_dump instead:
var_dump($sqlresult);
Im a bit new to the php side of parse, mainly objective-c and swift but I need to write some code that I can query a column (not the objectID one) to return the results..
The column I'm trying to query is a pointer to another class.
Here is the very basic code I have which returns all the rows in the class and the pointers data with the include key, but I need to filter or get only the row/s that I'm looking for.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$results = $query->find();
In the php sdk I see an option to use equalTo which has a key and a value to it so I tried the following code.
so I choose the column that was the pointer , and its objectid to hopefully only return those row/s that has that object id.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$query->equalTo("ColumnNameX", "yjdyaGRWP7");
$results = $query->find();
Nothing was returned and a php error was spit out
'pointer field ColumnNameX needs a pointer value' in /var/www/parse/src/Parse/ParseClient.php:326
So im not 100% sure why I cant filter by a ColumnNameX using its objectID which is a pointer to ClassA..
Did I miss something in the PHP docs..
I mean ideally in mysql to just get that row I want would be
SELECT * FROM ClassB WHERE ColunNameX = yjdyaGRWP7
That would return me the row of data, I can use a Join of course to get some info from ClassA as well.
Any thoughts on what im missing or do I need to first query the Class A to get a pointer, then in the equalTo do something like ("ColumnNamX" , $pointerfromClassA) ?
any one have anyone point out what im missing or have a code example.. I have seen some that use the objectID but I dont have access to that.
Ok I figured out one way to do this, not sure if this is the right way but it returns now what I want..
$query->equalTo("ColunNameX", array("__type" => "Pointer", "className" => "ColunNameX", "objectId" => "yjdyaGRWP7"));
I'm experiencing a strange issue while running a mysql query from php.
I have a mysql database with many (23) tables; two of them are Users and FollowersList.
FollowersList contains two colums: in the first one there's the code of a user, and in the second one the code of the user that the first one is following; each column references the primary index of Users table.
I have defined a stored procedure called "getFollowings" that returns the codes of the users that someone is following; it is defined in this way:
CREATE PROCEDURE getFollowings(IN cod INT(11))
BEGIN
SELECT Code2 FROM FollowersList WHERE Code1=cod;
END
When I call the stored procedure from phpmyadmin, everything works fine; I get all the correct results.
When I call it from php, using this code:
$sql0="CALL getFollowings('".$cod."')";
$res0=mysqli_query($con, $sql0);
$array0=mysqli_fetch_array($res0);
mysqli_next_result($con);
I can't get the correct results. The connection is defined correctly, and all the variables are correctly defined.
Let's make an example. I'm working on the user with code 94; when I run the procedure from phpmyadmin, I get two results: 63 and 89, which is correct in my database.
If I try the same from a php script, I get an array of dimension 2, but with only the first value; the var_dump is:
array(2) { [0]=> string(2) "63" ["Code2"]=> string(2) "63" }
This means that I receive and array with size=2, but with only one element stored. Do you have any idea why?
You are not iterating the mysqli_fetch_array.
Use like this
while($row = mysqli_fetch_array($res0)){
$array0[] = $row;
}
print_r($array0);
and you are using mysqli_fetch_array which give both associative array and numeric array.
If you want associative array use
mysqli_fetch_array($res0,MYSQLI_ASSOC);
If you want numeric array use
mysqli_fetch_array($res0,MYSQLI_NUM);
as default it return both
use like this
while($row = mysqli_fetch_array($res0)){
$array0[] = $row;
}
echo count($array0);
foreach($array0 as $value){
echo $value[0];
}
Say I have the following entity called inventory:
I would like to know if there are database, sql or magento actions or any other methods to produce:
I have solved this by iterating through the entire collection and inserting into a temporary table : i.e
$inventorySet = Mage::getModel('custom/module')->getCollection(*);
foreach($inventorySet as $item)
{
$this->insertItem($item->getSku());
}
}
public function insertItem($sku)
{
//insert sku if it does not exist in the temp set
// if it does exists add one to the QTY field
}
I can then retrieve what I want. I don't see an issue with it, but my entity is a lot larger than the example as well as the set of data containing anything between 2000 - 15 000 rows. is there not a more efficient way of doing this?
EDIT: In words, I would like to search the collection for occurrences of the "sku" field and return an array of unique sku's and the number of times it was found in the initial collection.
To get a particular set of column values from a collection you can use the getColumnValues() method.
For example, using a Magento product collection, this would return an array containing the sku attribute value for every product:
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('sku');
$skus = $collection->getColumnValues('sku');
Finally, to answer the second part of your question, to count the occurence of each unique value:
array_count_values($skus);
This will give you a nice associative array of sku => occurrence count.