Laravel Eloquent Builder: Get selected columns - php

I have a object (Illuminate\Database\Eloquent\Builder) that have a (Illuminate\Database\Query\Builder) inside it. I dump the object and can see this:
The image bellow, is a dd($querybuilder) from the object
My plan is to get the query and extract the selected columns but I already tried ($querybuilder->query) and ($querybuilder['query']) and I received errors.
How can I get the "columns" value?
Solved. Thank you
Tim Lewis
$query->getQuery()->columns worked!
Im posting where what im tying to do. select and addSelect are not doing what I wanted if no colunms are selected.
public function scopeAddStatusColumn($query)
{
$previous_columns = $query->getQuery()->columns;
$status_column = DB::raw('(begin <= NOW() AND end >= NOW()) OR (begin <= NOW() AND end IS NULL) as status');
$new_columns = $previous_columns == null ? ['*', $status_column] : [$previous_columns] + [$status_column];
$query = $query->select($new_columns);
return $query;
}

If you want to get all the columns of the table:
$query = Products::where('product_name', 'pencil')->get();
If you want to get a spesific column:
$query = Products::where('product_name', 'pencil')->get(['product_id']); // this is an array, you can get any column name here
If you want to get value of a single column:
$query = Products::where('product_name', 'pencil')->first(['description'])->description;

Related

how to show data from mysql in php where a trash != 1 (column value)

I want to show only that rows where Trash != 1 from php mysql in php. Trash variable is column field of php mysql where i stored a value of 1. so that I retrieve only rows where Trash = null
//Below mentioned code for check order status
$o_status = #$_GET['o_status'];
if(isset($o_status) and !empty($o_status)){
$o_where = array('order_s'=>$o_status);
}else if(isset($type) and !empty($type)){
//$o_where = array('status'=>1,'order_show_off'=>"No",'status'=>1);
}else{
//$o_where = array('status'=>1, 'status'=>1);
}
//And in last line of code for searching for all data
$cou_list = $con->all_fetch("orders",$o_where," group by order_id, vendor_id order by id desc");
Thank you in advance
I am expecting a solution for my problem in code terminology or maybe some guidance
you should make a query with the where clause for example
Select * From tables Where (Trash = Null)

php mysql wrong value from timestamp column

I have a mysql table that holds property for sale data, I record when then property is first listed in the column 'Added' and when the property details are updated I record the date and time in the 'Updated' Column.
This all works fine but when I run a query with php and return the row the value of the 'Updated' column is being returned as the current time.
Here is my table structure :
My query I use is this
$query = $pdo->query("SELECT * FROM table WHERE ID = 5030238");
while ($p = $query->fetch()) {
$added = $p['Added'];
$updated = $p['Updated'];
}
Expected output would be the variables to equal this:
$added = '2018-05-10 19:23:51'
$updated = '2018-05-11 09:55:11'
But the $updated is giving me the current time and date instead!
Does any one know what is wrong here?
Any help is much appreciated.

How to create auto increment series number using mysql query?

code:
<?php
$this->db->select('*');
$this->db->from('bugs');
$where = "project_name = '".$project."'";
$this->db->where($where);
$sql = $this->db->get();
$res = $sql->num_rows();
if($res === 0)
{
$i = 1;
foreach ($bug as $row)
{
echo $i;
}
}
else
{
$i = 1;
foreach ($bug as $row)
{
echo ++$i;
}
}
?>
$data = array(
'project_name'=>$this->input->post('project'),
'bug_id'=>$this->input->post('bug_id'),
);
$query = $this->db->insert('bugs',$data);
In this code I have a table name bugs where I have a column i.e. bug_id. Now, I want to insert data into my table if table row having no value it insert bug_id = 1 and after that insert 2 and then 3 and so on. But now when I click on submit button it insert bug_id = 1 and then 2 but when I click on third it insert again bug_id 2 . So, How can I fix this problem?Please help me.
Thank You
MySQL has integrated AUTO_INCREMENT. Either you log into phpMyAdmin and change the properties of your bug_id field by clicking on edit, then selecting the "A_I" field.
Or you can do it with a SQL query:
ALTER TABLE YOUR_TABLE MODIFY bug_id INTEGER NOT NULL AUTO_INCREMENT;
If #Twinfriends answer doesn't fit your needs (but that should be the correct way to do that, you shouldn't reinvent the wheel) then maybe the problem is with your:
$data = array(
'project_name'=>$this->input->post('project'),
'bug_id'=>$this->input->post('bug_id'),
);
$query = $this->db->insert('bugs',$data);
Since you are actually using $this->input->post('bug_id') as your new id, maybe you want to use something else?
Where does your $bug var come from? To be sure of not getting duplicated ids or wrong one you could add to your MySql query something like "ORDER BY bug_id DESC LIMIT 1" (so that you take 1 row as result with the highest bug_id) and then just use that result + 1 as the new id

Select one value from table and pass it to variable - php mysql

I want to create variable called $start. As a value I want to select one value from column called timestamp from the last row of my table called table_ex. So far I have this:
class Main {
//some other code
function dataBaseConnect(){
//well working part
}
function getTimeValue(){
$sql = "SELECT `timestamp` FROM `table_ex` WHERE id=(SELECT MAX(id) FROM `table_ex`)";
$this->start = $this->handler->query($sql, PDO::FETCH_COLUMN, 0);
}
function printVal(){
$this->dataBaseConnect();
$this->getTimeValue();
$this->messOuput = "Sth text " .$this->start;
}
}
The problem is that variable is not getting that value I wanted. Could anyone explain me where is the problem?
Maybe this will work for You:
function getTimeValue()
{
// note the table name is now used in the inner query
$sql = "SELECT `timestamp` FROM `table_ex` WHERE id=(SELECT MAX(id) FROM `table_ex`)";
$this-start = $this->handler->query($sql, PDO::FETCH_COLUMN, 0);
}

Codeginiter JOIN query with DATE between and LIMIT not working

I am trying to get the details of a persons based on the user input age (Integer). Now my query always returns null Array() whenever I execute the below code. I haven't specified the $postdata array. You can see I have used $postdata['ageto'] and $postdata['agefrom'] are used in calculating $agefrom and $ageto
$now = new DateTime();
//Converting _POST age to Date
$agefrom = date("Y-m-d",strtotime($now->format("Y")-$postdata['ageto'].'-'.$now->format("m").'-'.$now->format("d")));
$ageto = date("Y-m-d",strtotime($now->format("Y")-$postdata['agefrom'].'-'.$now->format("m").'-'.$now->format("d")));
$this->db->select('uacc_id, uacc_username, name, dob, city, education');
$this->db->from('user_accounts as a');
$this->db->join('personal as b','a.uacc_id = b.pruserid','INNER');
$this->db->join('profession as c','a.uacc_id = c.puserid','INNER');
$this->db->join('location as d','a.uacc_id = d.luserid','INNER');
$this->db->where('dob >= ',$agefrom);
$this->db->where('dob <= ',$ageto);
$this->db->limit(10, $offset);
$query = $this->db->get();
return $query->result();
I have suspected that my input post is not fetching the data properly. So I have replaced it with my simple query select * from... where dob >.... and it worked well. So there is no problem with _POST variables. I am not sure what I am doing wrong. Can some one help me.
I think you have to specify to which table DOB belongs like a.dob. Sorry for the post but I could not comment. Hope this helps.
The select field should add the table name
$this->db->select('a.uacc_id, a.uacc_username, a.name....); // example
and your where add table name too.
$this->db->where('b.dod >= ', $agefrom); // example
or you can output the sql command.
You can look here

Categories