I am passing json to a laravel route as following and I am runnig this query sql view.
{"columns":["fname","lname","mobile"], "offset":"1", "limit":"25",
"order":[["fname","asc"],["lname","asc"]],
"filter":[["gender","=","M"]]}
And this is the function placed in controller which will going to call on route
public function fetch_contacts(Request $request){
if($request->header('content-type') == 'application/json' && !empty($request->header('Device')) && !empty($request->header('UID'))){
$query = DB::connection('mysql_freesubs')->table("contact_view")
->select($request->columns);
if(!empty($request->filter))
$query = $query->where($request->filter);
if(!empty($request->offset))
$query = $query->offset($request->offset);
if(!empty($request->limit))
$query = $query->limit($request->limit);
if(!empty($request->order))
$query = $query->orderBy($request->order);
$contacts = $query->get();
return $contacts;
}
where am I going wrong ?
You're passing multidimensional array to orderBy, try this instead:
$query = $query->orderBy($request->order[0][0], $request->order[0][1]);
$query = $query->orderBy($request->order[1][0], $request->order[1][1]);
Related
i have project on laravel 5.3
and this is my function inside the controller
public function CustomerReportGet(Request $request)
{
$full = Voucherreceive::where('voucherreceive_customer_id','=',$request->customer_id)->sum('voucherreceive_amount');
if($request->from_date == '' and $request->to_date == '')
$data = Voucherreceive::where('voucherreceive_customer_id','=',$request->customer_id)->get();
elseif($request->from_date <> '' or $request->to_date <> '')
{
$data = Voucherreceive::where('voucherreceive_customer_id','=',$request->customer_id)->
whereBetween('created_at',array($request->from_date,$request->to_date));
}
return $data;
}
how can i send the $full variable with $data ..
i tried to do this
$data = Voucherreceive::where('voucherreceive_customer_id','=',$request->customer_id)->with('full')->get();
but i still have internal server error ..
thanks
Before return, you can make an array with full & data as it's element and then return this new array.
return array($full, $data);
Or you can define the relationship in the model to use 'with'
I have sql query in controller, this query produces number of rows.
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}
then i pass this data to my model,
public function update_cancel_stock($code,$qty)
{
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $order );
$this->db->update("tbl_inventory6");
}
but no any update. please check above code. thanx
Try this
$cond = array("order_id" => $order_no, "location" => $location);
$q1 = $this->db->select("product_code,product_quantity")->from("tbl_order_details")->where($cond)->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
and then model ($code was not used)
public function update_cancel_stock($code,$qty){
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $code);
$this->db->update('tbl_inventory6');
}
You are using row() function of Active Record. It already return only one row. So there is no requirement of foreach. Try this:
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
If you are getting multiple records and you are using foreach, you should use result().
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->result();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}
I'm trying to make conditions on my search query, when I use this code I got this error :
my code :
$condition = null;
foreach (session()->get('filters') as $items)`
{
$condition .= "where(".$items['type'].",".$items['value'].")->";
}
$cars = Car::where('is_active','active')->$condition->get();
My error:
Undefined property: Illuminate\Database\Eloquent\Builder::$where(city_id,1)
If you want to add methods to an existing query, start your query with $query = Car::newQuery(). Then just append your methods to that variable.
$query = Car::newQuery();
$query = $query->where('is_active', 'active');
foreach (session()->get('filters') as $items) {
$query = $query->where($items['type'], $items['value']);
}
$cars = $query->get();
Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}
I would like to return the query results along w/ the row count without having to run multiple queries. Is this possible in Codeigniter? The following works for returning the query results, but is it possible to also count the number of entries found?
Controller:
$data['records'] = $this->item_model->searchItem($item_name);
Model:
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query->result();
$bindings = array($this->user_id, '%'.$item_name.'%');
$records = $this->db->query($sql, $bindings)->result();
return array(
'records' => $records,
'count' => count($records),
);
Then, in your controller:
$query = $this->item_model->searchItem($item_name);
$data['records'] = $query['records'];
$data['count'] = $query['count'];
Option one
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
$data['result'] = $query->result();
$data['rows'] = $query->num_rows();
return $data;
Option two
// model
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query;
// controller
$data['query'] = $this->item_model->searchItem($item_name);
// then you have $data['query']->result();
// and $data['query']->num_rows();
You can send as many variables from a function as you want ..However plz remember that a function is supposed to do one UNIT of work . thus this rule must not be violated.
we can used contrel structure
if this {return A;} elseif () {return B;} else {return C;}
ALso we can send (bundle) variables in an array and send