Laravel Query Builder dynamic nested where/orWhere - laravel-4

I hope someone can help guide me in the right direction. I am new to Laravel and far from the best php coder. I am trying to build dynamic nested where/orWhere clauses and I am stuck, and by stuck I mean I can't figure it out. The code I have so far is:
public static function runNested($rId, $current_id) {
$insidestuff = DB::table('tableName')
->select('all fields necessary')
...
->get();
$count=1;
foreach ($insidestuff as $inside)
{
$var1 = $inside->var1;
$var2 = $inside->var2;
$var3 = $inside->var3;
$var4 = $inside->var4;
if ($count == 1)
{
$outsideMet = $outsideMet->where(function($query) use ($var1, $var2, $var3, $var4, $id)
{
$query->where($var1, $var2, $var3);
$outsideMet = myClass::runNested($var4, $id);
});
} else {
$outsideMet = $outsideMet->orWhere(function($query) use ($var1, $var2, $var3, $var4, $id)
{
$query->where($var1, $var2, $var3);
$outsideMet = myClass::runNested($var4, $id);
});
}
$count++;
}
}
public static function runOutside($rId) {
$outsideQuery = DB::table('tableName')
->select('all necessary fields')
->get();
$outsideMet = dbTable::select('*');
$outsideMet = $outsideMet->leftJoin('necessary fields');
$outsideCount=1;
foreach ($oursideQuery as $oQuery)
{
$oVar1 = $oQuery->oVar1;
$oVar2 = $oQuery->oVar2;
$oVar3 = $oQuery->oVar3;
$oVar4 = $oVar4;
$oVar5 = $oQuery->oVar5;
if ($outsideCount == 1)
{
$outsideMet = $outsideMet->where(function($query) use ($oVar1, $oVar2, $oVar3, $oVar4, $oVar5)
{
$query->where($oVar1, $oVar2, $oVar3);
$outsideMet = myClass::runNested($oVar4, $oVar5);
});
} else {
$outsideMet = $outsideMet->orWhere(function($query) use ($oVar1, $oVar2, $oVar3, $oVar4, $oVar5)
{
$query->where($oVar1, $oVar2, $oVar3);
$outsideMet = myClass::runNested($oVar4, $oVar5);
});
}
$outsideCount++;
}
$outsideMet = $outsideMet->count();
I hope someone can tell me how to do this properly, and if the above code is even close, what I am doing wrong. Thank you in advance for the help and guidance.

Related

How to get Select max value in codeigniter

Controller:
$next_id = $this->o->next_id();
$data['next_id']=$next_id;
Model:
public function next_id(){
$this->db->select_max('p_ori_id');
$max = $this->db->get('orientation_master');
if($max==0){
$next_id = 1;
}else{
$next_id = 1+$max;
}
return $next_id;
}
Return Error:
Object of class CI_DB_mysqli_result could not be converted to int
Please solve problem..
No offense to #pradeep but you may have some unexpected results if you don't have any rows. I suggest:
public function next_id()
{
$this->db->select_max('p_ori_id', 'max');
$query = $this->db->get('orientation_master');
if ($query->num_rows() == 0) {
return 1;
}
$max = $query->row()->max;
return $max == 0 ? 1 : $max + 1;
}
Hope this will help you:
public function next_id()
{
$this->db->select_max('p_ori_id', 'max');
$query = $this->db->get('orientation_master');
// Produces: SELECT MAX(p_ori_id) as max FROM orientation_master
$max = $query->row()->max;
if($max == 0){
$next_id = 1;
}else{
$next_id = $max+1;
}
return $next_id;
}
For more : https://www.codeigniter.com/user_guide/database/query_builder.html
You are getting that error becuase $max is a result set object and not an integer record value like you're trying to use it.
You can try this function to get the next id.
Modified function:
public function next_id(){
$this->db->select_max('p_ori_id', 'max');
$result = $this->db->get('orientation_master');
$row = $result->row_array();
$next_id = isset($row['max']) ? ($row['max']+1) : 1;
return $next_id;
}
If the column is auto increment, you can use the below code instead.
Alternative:
public function next_id() {
$sql_string = "SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '".$this->db->dbprefix."orientation_master'";
$query = $this->db->query($sql_string);
$row = $query->row_array();
return $row['auto_increment'];
}

Send Data From Controller to View Code Igniter

I have an function get() on my controller. This function I use to get data from database and I save it to $data variable.
It's possible to call get() function on another function and I send the $data variable to the view? I want to make two view, one is classic and another one is modern view.
This is my code:
public function get()
{
$data['all'] = $this->genbamodel->getAll();
$data['delay'] = $this->genbamodel->getDelay();
$data['ontime'] = $this->genbamodel->getOntime();
$data['ahead'] = $this->genbamodel->getAhead();
$data['unloading'] = $this->genbamodel->getUnloading();
$data['topdelay'] = $this->genbamodel->getTopDelay();
$data['topahead'] = $this->genbamodel->getTopAhead();
$data['jumlah'] = count($this->genbamodel->getID())+1;
for ($j=1; $j < $data['jumlah']; $j++) {
$datas['lalax'.$j] = $this->genbamodel->getRecord($j);
$data['lala'.$j] = array_reverse($datas['lalax'.$j]);
}
$data['time'] = $this->genbamodel->getTime();
}
public function home_classic()
{
$this->get();
$this->load->view('home_classic',$data);
}
public function home_modern()
{
$this->get();
$this->load->view('home_modern',$data);
}
Hope this will help you :
Your get() method should return $data
public function get()
{
$data['all'] = $this->genbamodel->getAll();
$data['delay'] = $this->genbamodel->getDelay();
$data['ontime'] = $this->genbamodel->getOntime();
$data['ahead'] = $this->genbamodel->getAhead();
$data['unloading'] = $this->genbamodel->getUnloading();
$data['topdelay'] = $this->genbamodel->getTopDelay();
$data['topahead'] = $this->genbamodel->getTopAhead();
$data['jumlah'] = count($this->genbamodel->getID())+1;
for ($j=1; $j < $data['jumlah']; $j++) {
$datas['lalax'.$j] = $this->genbamodel->getRecord($j);
$data['lala'.$j] = array_reverse($datas['lalax'.$j]);
}
$data['time'] = $this->genbamodel->getTime();
return $data;
}
in home_classic() do it like this
public function home_classic()
{
$data = $this->get();
$this->load->view('home_classic',$data);
}
in home_modern() do the same
public function home_modern()
{
$data = $this->get();
$this->load->view('home_modern',$data);
}
for more : https://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

Call to undefined function mcrypt_decrypt() - CometChat Laravel

Hi I've already installed the CometChat, but I'm facing the following error:
Call to undefined function mcrypt_decrypt() in /home/vagrant/changeglobe/public/cometchat/integration.php on line 89
I'm using Homestead with Nginx for Laravel. I have read at many places that I need to enable mycrypt, but did not found any correct. Please let me know about this issue if you know. Thank you.
Try replacing the getUserID() function in /cometchat/integration.php file with the code below:
function getUserID() {
$userid = 0;
if (!empty($_SESSION['basedata']) && $_SESSION['basedata'] != 'null') {
$_REQUEST['basedata'] = $_SESSION['basedata'];
}
if (!empty($_REQUEST['basedata'])) {
if (function_exists('mcrypt_encrypt') && defined('ENCRYPT_USERID') && ENCRYPT_USERID == '1') {
$key = "";
if( defined('KEY_A') && defined('KEY_B') && defined('KEY_C') ){
$key = KEY_A.KEY_B.KEY_C;
}
$uid = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode(rawurldecode($_REQUEST['basedata'])), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
if (intval($uid) > 0) {
$userid = $uid;
}
} else {
$userid = $_REQUEST['basedata'];
}
}
if (!empty($_COOKIE['laravel_session'])) {
$session= cookie_decrypt($_COOKIE['laravel_session']);
$data = file_get_contents(dirname(dirname(dirname(__FILE__))).'/storage/framework/sessions/'.$session);
if (!empty($data)) {
$k = explode(';i:',$data);
$m = explode(';s:',$k[1]);
$userid = $m[0];
}
}
$userid = intval($userid);
return $userid;
}
If you are still facing issues please create a support ticket https://my.cometchat.com/tickets and our team will assist you.

Filtering Eloquent Collection

Trying to filter a collection object down based upon any combination of multiple values. This is as far as I've got. Not happening. Any clues for me?
public function search()
{
$posts = Posting::all();
$type_id = Input::get('type_id');
$country_id = Input::get('country_id');
$province_id = Input::get('province_id');
$posts = $posts->filter(function($post)
{
if( !empty($type_id) && $type_id>0 )
{
return $post->where('type_id','=',$type_id);
}
})->values();
$posts = $posts->filter(function($post)
{
if( !empty($country_id) && $country_id>0 )
{
return $post->where('country_id','=',$country_id);
}
})->values();
$posts = $posts->filter(function($post)
{
if( !empty($province_id) && $province_id>0 )
{
return $post->where('province_id','=',$province_id);
}
})->values();
return $posts;
}
Any help appreciated.
First off, you should really do a validation for the id being larger than 0, if they were provided. You shouldn't be manually checking that out.
But anyhow, why are you using filter, when you can just directly use Eloquent (I omitted the check for id being bigger than 0):
$type_id = Input::get('type_id');
$country_id = Input::get('country_id');
$province_id = Input::get('province_id');
$query = Posting::query();
if ( ! empty($type_id)) $query->whereTypeId($type_id);
if ( ! empty($country_id)) $query->whereCountryId($country_id);
if ( ! empty($province_id)) $query->whereProvinceId($province_id);
return $query->get();
First off, you need to return something that makes sense from the closure in your filter:
$posts = $posts->filter(function($post)
{
if( !empty($type_id) && $type_id>0 )
{
return $post->where('type_id','=',$type_id);
}
})
Above returns Eloquent\Builder object, which is evaluated to true, so.. it really doesn't make sense.
This is what you need:
$posts = $posts->filter(function($post)
{
if( !empty($type_id) && $type_id>0 )
{
return $post->type_id == $type_id; // bool
}
})
With such code, you will filter the collection. $posts now will hold only those items, that match the statement in that closure.

Codeigniter: How to declare a global variable for a recursive function, and how to return it?

I want to use recursion in a model in Codeigniter, and am baffled as to where I should declare the global variable to be used in the recursive function.
I'm trying to show a catalog of products, ordered by their geographical classification. The recursion is needed because the geographical database is a hierarchy tree, where a place can have sons, grandsons, great-grandsons etc (for example: London is the son of England, which is the son of Britain, which is the son of Europe).
At the end of the recursion, I want to return the variable $arrCatalog, which will hold all the products. So my other question is how to return that variable in the end?
Here is my code (not tested yet, because I didn't know how to handle the global variable):
class Catalogs_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function getCatalog($place_id, $prod_type)
{
$sql =
"SELECT id, heb_name, type FROM places p
INNER JOIN places_relations r ON p.id = r.son
WHERE father = ?
ORDER BY sort_name";
$query = $this->db->query($sql, $place_id);
if($query->num_rows() > 0) {
foreach ($query->result() as $place) {
$sql2 =
"SELECT code, heb_title, price, place_id FROM products
INNER JOIN products_places ON prod_code=code
WHERE place_id = ? AND prod_type = ?";
$query2 = $this->db->query($sql, $place_id, $prod_type);
if($query2->num_rows() > 0) {
foreach ($query2->result() as $product) {
$arrCatalog[] = $product; // $arrCatalog is the global variable I don't know where to declare and how to return.
}
$this->getCatalog($place['id'], $prod_type);
}
}
}
}
}
Pass your variable to the current object ($this), then you can pass it around your model or hand it to a controller.
class Catalogs_model extends CI_Model {
public $arrCatalog = array();
public function __construct() {
parent::__construct();
}
function getCatalog($place_id, $prod_type) {
$sql = "SELECT ...";
$query = $this->db->query($sql, $place_id);
if($query->num_rows() > 0) {
foreach ($query->result() as $place) {
$sql2 = "...";
$query2 = $this->db->query($sql, $place_id, $prod_type);
if($query2->num_rows() > 0) {
foreach ($query2->result() as $product) {
$this->arrCatalog[] = $product;
}
$this->getCatalog($place['id'], $prod_type);
}
}
}
}
Your controller:
$this->catalogs_model->getCatalog(2, 'hebBook');
$data['data_rows'] = $this->catalogs_model->arrCatalog;
Testing, alter your model like so ....
class Catalogs_model extends CI_Model {
public $arrCatalog = array();
// add these variables for testing ....
private $greeting = 'Hello'; // notice this is private? You cannot access this from your controller but you can from anywhere within the Catalogs_model class.
public $output = ''; // public
public $string = null; // public
// your existing code ...
function hello ()
{
$this->output = $this->greeting;
}
function hello_string ()
{
if($this->string)
{
$this->output = $this->greeting.' '.$string;
}
}
} // end class
Now test in your controller
$this->catalogs_model->hello(); // runs the hello function which sets the model->output
echo $this->catalogs_model->output; // echos 'Hello'
echo '<br />'; // just so the output stands apart in your browser.
$this->catalogs_model->string = 'World!';
$this->catalogs_model->hello_string();
echo $this->catalogs_model->output; // echos Hello World!

Resources