Get results as simple value from Laravel query builder - laravel

I get the output of the values as following in the postman.
[{"currentVersion":1.1}]
How can i get output in simple form as: 1.1
$currentVersion = DB::table('app_Version')
->select('currentVersion')
->get();
echo $currentVersion
How can i get output in simple form as:
1.1

You would do:
echo $currentVersion[0]->currentVersion;
However, if you know you'll always have one record, you could simplify this a bit with:
$result = DB::table('app_Version')->first(['currentVersion']);
echo $result->currentVersion;

Related

How to echo multiple where clauses in a CI query on screen

I'm attempting to get the results of a database table and echo the query results.
Here is my code:
$queryDB = $this->db->select('*')
->from('dr_template_relational')
->where('value_id', $categoryDetails['value_id'])
->where('subcategory_id', $categoryDetails['subcategory_id'])
->get();
echo "<br>";
echo "here is queryDB";
echo($queryDB);
echo "that was it";
$queryDB doesn't echo even so I'm sure the data is there. I'm wondering what I'm doing wrong. I'm not getting an error, but also don't get any output with echo($queryDB); on screen it displays:
here is queryDBthat was it
Right now, your approach is just echoing out the instance of the query.
In order to see the query results, you need to create them. There are several CI functions like result() and row()
at the end it could look like this:
foreach ($queryDB ->result() as $row){
echo $row->value_id;
echo $row->subcategory_id;
}
see Generating Query Results
hint: in order to see the generated query you could use
echo $this->$db->last_query();

Where with a greater than option in Laravel

i have a filter function and if i select the number 3 it should show me also the tests with the number 1 and 2
so i need a multiple "where" who shows me all tests with the number who are less than 3
i tried something like this:
$tests = DB::table('tests')->orderBy('name', 'asc')-where('number', < 3)->get();
it shows me that is not possible but is there a right laravel syntax to do something like this?
i cant find anything about it
because i always use where('number', 1) so like this
Your condition is not correct, so change it to:
where('number', '<=', 3) // '<=' for 1,2,3. If you want only 1,2 try '<'
and there is an issue here:
-where
change it to:
->where
and try again.
You need to use less than mark as a string ('<') as a 2nd param and value as a 3rd param. For example:
$tests = DB::table('tests')->orderBy('name', 'asc')->where('number', '<', 3)->get();
If you use equal to ('=') mark you can use value as a 2nd param.
You can get detail knowledge from below link with multiple examples
https://laravel.com/docs/5.7/queries#where-clauses
$selected_number = '3';
$tests = DB::table('tests')->where('number', '<=', $selected_number)->orderBy('name', 'asc')->get();

Laravel 5.1, Eloquent where zero value

$sqLines = SqLines::whereDocumentId($id)->get();
$sqLines->where('item_id','=',0)->count();
item_id is an unsigned integer field
In my development server, it shows result but in my production server with the record exists.
Any reason it happen?
You can use the following
$count = SqLines::where('document_id','=',$id)
->where('item_id','=',0)
->count();
OR
$data = SqLines::where('document_id','=',$id)
->where('item_id','=',0)
->get();
$count = count($data);
With get(), you retrieve the result, so you can't limit it after. Change your code to this:
$sqLines = SqLines::whereDocumentId($id)
->where('item_id','=',0)
->get();
echo $sqlLines->count(); // wil return the number of selected records.
Maybe you will need to change your code. Use one of the following options:
Option 1. Remove the "->get()".
Option 2. Remove the "->count()" and add a loop for check manually the result with a "foreach", "while", etc...

Get whole Property Object from RETS Server using PHRETS

I am getting MLNumbers from RETS Server but now I need to get all the fields of all the MLNumbers and store into my database. Could anybody help me to write query to get the whole property Object.
If you have all the Listing Numbers then you just need to write a search query to pull all the results back and process the results. I haven't ever seen a field name MLNumber but you will have to figure that out:
<?php
$search = $rets->SearchQuery("Property","RES","(MLNumber=11111,22222,33333)");
while ($listing = $rets->FetchRow($search)) {
echo "Address: {$listing['StreetNumber']} {$listing['StreetName']}, ";
echo "{$listing['City']}, ";
echo "{$listing['State']} {$listing['ZipCode']} listed for ";
echo "\$".number_format($listing['ListPrice'])."\n";
}
$rets->FreeResult($search);
I think this should return the results you need. You will need to figure out the Resource/Class/Field name you need to use for your SearchQuery().
More PHRETS examples here.

select_max not working in CI

This is not working in my CI app:
$query = $this->db->select_max('order')->get('posts');
print_r($query);
Why is that?
I have a column in my DB called order (int, where the highest value is currently 6) and the table is called posts
Why nothing is outputted instead a number 6 ?
This just runs the query, you need to use ->row() to get the result from it.
$this->db->select_max('order', 'max_order');
$query = $this->db->get('posts');
echo $query->row()->max_order;
DOCS:
https://www.codeigniter.com/userguide2/database/active_record.html
https://www.codeigniter.com/userguide2/database/results.html

Resources