how to resolve this sql query inside laravel controller - sql

I am getting internal server error 500 when I pass this query inside my Laravel controller. Is there any alternative to write this?
$var1 = $request->xyz;
$var2 = $request->abc;
$take = $request->take;
$skip = $request->skip;
$sample = DB::table('table_name')
->select('table_name.*', DB::raw('COUNT(column_name) as total_count'))
->where('column_name1', $var1)
->where('column_name2', $var2)
->offset($skip)
->limit($take)
->get();
$encrypted = AESEncrypt($sample);
return json_encode($encrypted);

try to use ->select('column_name', DB::raw('COUNT(*) as total_count'))
and also try \DB::

Related

Questions regarding update of table on Prestashop

I'm trying to update ps_stock_available when I modify the product on Prestashop. But it's unsuccessfull. Could you help me please ?
public function hookActionUpdateQuantity(array $params)
{
$id_product = $params['id_product'];
$product = new Product((int)$id_product);
$id_category = $product->id_category_default;
$db = \Db::getInstance();
$request_loc='SELECT location FROM `'._DB_PREFIX_.'category_location` WHERE `id_category` = '.(int)$id_category;
$location = $db->getValue($request_loc);
$request_id_stock='SELECT id_stock_available FROM `'._DB_PREFIX_.'stock_available` WHERE `id_product` = '.(int)$id_product;
$id_stock_available = $db->getValue($request_id_stock);
$result = $db->update('stock_available', array('location' => $location), '`id_stock_available` = '.(int)$id_stock_available);
}
I have written this code but it doesn't seem to work.
in order to accomplish this task I would rely to the native StockAvailable class metehods getStockAvailableIdByProductId() and setLocation() (check the classes/stock/StockAvailable.php file).
Anyway your code seems to be correct, so I would definitely check for undefined variables and/or something not working in the $db->update statement.
In case, you can change it to :
$db->execute('UPDATE '._DB_PREFIX_.'stock_available SET `location` = "'.pSQL($location).'" WHERE id_stock_available = '.(int)$id_stock_available;

Codeigniter 4 - WHERE clause in Model

It's me again, Van.
Hi everyone,
I hope you're doing well!
I am doing a tutorial on a chat application using Codeigniter 4 with Ajax.
Everything worked fine until I applied the following code in the Model below
public function load_chat_data($sender_id,$receiver_id) {
// $where = ['sender_id' => $sender_id, 'receiver_id' => $receiver_id];
$where1 = "sender_id = $sender_id OR sender_id = $receiver_id";
$where2 = "receiver_id = $receiver_id OR receiver_id = $sender_id";
$builder = $this->db->table('chat_messages');
// $builder->where($where);
$builder->where($where1);
$builder->where($where2);
$builder->orderBy('chat_messages_id','ASC');
$results = $builder->get();
$rows = $results->getResultArray();
if($rows > 0)
{
return $rows;
}
else
{
return false;
}
}
The lines that I commented worked well before they were commented but it was not enough data I wanted to get so I tried to get both data of sender and receiver to display on the view by adding more code. However, when I tried $where1 and $where2 for the WHERE clauses, it didn't work. I think it must be the syntax error. Please correct my codes or any ideas on how the codes work with the same meaning supposed.
Thank you so much!!!
I tried as below, but it still didn't work.
$where1 = "sender_id={$sender_id} OR sender_id={$receiver_id}";
$where2 = "receiver_id={$receiver_id} OR receiver_id={$sender_id}";
Also, I tried:
$where1 = "'sender_id'=$sender_id OR 'sender_id'=$receiver_id";
$where2 = "'receiver_id'=$receiver_id OR 'receiver_id'=$sender_id";
I think you are trying to receive the messages of the conversation between two people. Can you try the following code for this?
$builder->groupStart(); // or $builder->orGroupStart();
$builder->where('sender_id', $sender_id);
$builder->where('receiver_id', $receiver_id);
$builder->groupEnd();
$builder->orGroupStart();
$builder->where('sender_id', $receiver_id);
$builder->where('receiver_id', $sender_id);
$builder->groupEnd();

Syntax Error SQL Codeigniter

I have this in my code and im using codeigniter yet it's producing an error
// make call to db and create a file
$this->db->select("*");
$this->db->from("mst_global_settings mgs");
$this->db->join("trans_global_settings tgs", "mgs.global_name_id=tgs.global_name_id", "inner");
$this->db->where('`tgs`.lang_id', $lang_id);
$query = $this->db->get();
$result = $query->result_array();
$tmp_array = array();
foreach ($result as $global_array) {
$tmp_array[$global_array["name"]] = $global_array["value"];
}
Codeigniter is giving me the following error
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tgs`.`lang_id` = 'en'' at line 4
SELECT * FROM (`p931_mst_global_settings` mgs) INNER JOIN `p931_trans_global_settings` tgs ON `mgs`.`global_name_id`=`tgs`.`global_name_id` WHERE `p931_`tgs`.`lang_id` = 'en'
Filename: /models/mdl_common.php
Line Number: 26
The problem is your single quotes.
Change this:
$this->db->where('`tgs`.lang_id', $lang_id);
To this:
$this->db->where('tgs.lang_id', $lang_id);

method as_array() doesn't work

I'm using idiorm as an ORM library and the method as_array() doesn't work this is my code I get 2 errors
1- $market = $market->as_array();
PHP Fatal error: Call to a member function as_array() on a non-object
2- ->as_array();
PHP Fatal error: Call to a member function as_array() on a non-object
My code:
function ($market = 'affiliate', $category = array('all')) use ($app) {
$market = \ORM::for_table('category')
->where('alias', $market)
->find_one();
$market = $market->as_array();
$category = #end($category);
if ($category != 'all') {
$category = \ORM::for_table('category')
->where('alias', $category)
->where_gt('category_id', 0)
->find_one()
->as_array();
$items = \ORM::for_table('item')
->select('item.*')
->select('category.name', 'category_name')
->join('category', 'category.id = item.category_id')
->where('item.category_id', $category['id']);
According to the Idiorm docs:
Any method chain that ends in find_one() will return either a single instance of the ORM class representing the database row you requested, or false if no matching record was found.
You should check to see if your query has returned any rows before calling as_array().
For example:
$market = \ORM::for_table('category')
->where('alias', $market)
->find_one();
if($market != false)
{
$market = $market->as_array();
}
If this still gives an error then you may not have initialised Idiorm properly.

Store.reload() ->How to Query by lastParam in ExtJs4

I have a Grid.
Get Data Code
Code:
var myParam = {
typeId : 1,
xxxx : 'xxxx' //some else
}
grid.store.load(myParam);
When to do something such as time renovate
Code:
grid.store.reload();
But this lost param.
In ExtJs3 have a Config of LastParam .
How to do in Extjs4.
thanks.
use the proxy's extraParams config.
grid.store.getProxy().extraParams = myParam;
grid.store.load();
grid.store.reload();
This way the params will be sent until you modify the extra param again with code.