Laravel WhereIn method in Eloquent not working - php

I want to use the WhereIn method in Eloquent but it now works like the below function.
<?php
$product = DB::table('product')
->join('supplier','supp_code','=','prod_supplier_code')
->select('product.*','supplier.supp_margin')
->where('prod_seo_title','=',$id)
->first();
$decArr = explode(',',$product->prod_decoration_type);
for($i=0; $i<count($decArr); $i++)
{
if($i==0)
$inString = "'".$decArr[$i]."','";
elseif ($i<(count($decArr))-1)
$inString .= $decArr[$i]."','";
else
$inString .= $decArr[$i]."'";
}
$proDeco = DB::table('decoration')->whereIn('deco_print_type', [$inString])->get();
?>
But In the query, it's displaying like this.
select * from `decoration` where `deco_print_type` in ('\'100109C9\',\'100110B9\',\'100144C9\',\'100186C9\'');
I can't understand why these slashes are coming. Please help me to fix this issue.

The whereIn() method will accept an array of all the items. In your example you are passing an array with one element with all the values already concatenated. Based on your example you only need to pass $decArr
DB::table('decoration')->whereIn('deco_print_type', $decArr)->get();

Related

How to concatenate the query arguments to the query in a scope in Laravel using Eloquent?

I'm trying to build a complicate query where there is an unknown number of postalcode areas posted to my api. At the moment I almost found the solution, but I can't concatenate my whereBetween parameters to the Query in the scope. I get the error query can't be casted to string, which also makes sense.
The array passed to the scope has the following structure: $plz = [[10000, 11000], [34200, 34500]];
Here is my code in the scope:
public function scopePlz($query, $plz)
{
$queryString = '';
foreach ($plz as $index => $single) {
$queryString .=
$index == 0
? "->whereBetween('postalcode_int', [$single[0], $single[1]])"
: "->orWhereBetween('postalcode_int', [$single[0], $single[1]])";
}
return $query . $queryString;
}
You could solve it like this:
public function scopePlz($query, $plz)
{
$first = array_shift($plz);
$query->whereBetween('postalcode_int', [$first[0], $first[1]]);
foreach ($plz as $single) {
$query->orWhereBetween('postalcode_int', [$single[0], $single[1]]);
}
return $query;
}
Get the first item from the array, add it like a where and then run the rest of the array through a foreach.
If you want one big where statement for all these postal codes, wrap this scope in an separate where method.

PHP / WP - iterate through multidimensional POST parameters

What is the proper way to iterate through multidimensional Array of POST parameters ? Example from Chrome Form Data Console below:
order[0][priority][]:1
order[0][priority][]:4
order[0][priority][]:2
order[0][priority][]:3
order[0][serviceId][]:28
order[0][serviceId][]:31
order[0][serviceId][]:29
order[0][serviceId][]:30
categoryId:5
I want to Update database table inside loop using above POST parameters.
$wpdb->query("
UPDATE $table SET order=$priority
WHERE category_id=$categoryId
AND service_id=$serviceId
");
First occurence of order[priority] is compact with first occurence of order[serviceId].
The question is how to create foreach to run UPDATE query?
Thanks !
As described in your example I think you can iterate both arrays as the same time. If "priority" and "servicesId" have the same size you can do this:
$order = $_POST['order'];
$categoryId = $_POST['categoryId'];
$size = count($order[0]["priority"]);
foreach ($i=0; $i<$size; $i++) {
$priority = $order[0]["priority"][$i];
$serviceId = $order[0]["serviceId"][$i];
$wpdb->query("
UPDATE $table SET order=$priority
WHERE category_id=$categoryId
AND service_id=$serviceId
");
}
Hope it helps!

Query to select multiple records from same column with CodeIgniter

I am trying to get records from same column. I want to get the farm codes from two IDs, I tried following with help of CodeIgniter documentation but it doesn't work.
$this->db->select('fa_code');
$this->db->where('fa_id', $by);
$this->db->where('fa_id', $from);
$res=$this->db->get('tukai_farms')->result();
What is wrong? Don't we use AND with same field?
You need to declare from where you want to get 'fa_code' then declare the conditions you have:
This is the correct way:
public function selectFaCode(){
$by = 1;
$from = 2;
$query = $this->db->select('fa_code')
->from('tukai_farms')
->where('fa_id', $by)
->or_where('fa_id', $from) //Remember that you are declaring the same condition ! Remove this to keep the first where or change the column name if you want to get items from table where the condition is another column.
->get()
->result();
foreach($query as $row){
echo $row->fa_code;
}
}
Just as anant said, you should use:
$this->db->where('fa_id', $by);
$this->db->or_where('fa_id', $from);
You can select multiple records from same column with this query
$value = array($by, $from);
$this->db->where_in('fa_id', $value);

Can you use query builder to build a query with a dynamic WHERE clause at runtime in laravel3?

I know in Laravel 4 you can create a dynamic WHERE clause at runtime as from the following question:
Can you use query builder to build a query with a dynamic WHERE clause at runtime in laravel?
But can you do the same in laravel 3?
If not, is your only option instead to create raw SQL code as in the following:
$SQL = "SELECT * FROM " . $table;
$first = 1;
foreach($items as $key => $val)
{
if($first) $SQL .= " WHERE ";
else $SQL .= " AND ";
$SQL .= $key . " LIKE " . $VAL;
$first = 0;
}
It works pretty much the exact same way. Just use $query = DB::table('tablename'); in place of the first line in the related post then use it as the rest of the example shows.
You can do this using something like this:
$query = DB::table($table);
if(isset($key1) && isset($val1)) {
$query->where($key1, 'LIKE', $val1);
}
if(isset($key2) && isset($va2)) {
$query->where($key2, '=', $val2);
}
$result = $query->get(); // returns all matching models
You can chain as many where clause as you need until you call the get() method. Also check this answer/L-4 to get another idea. Reading the Laravel - 3 manual would help you.
You can use the query builder to perform queries such as this.
$query = DB::table($table);
// Get only the fields from the form that we care about...
$form_fields = Input::get('email', 'username');
// Build a dynamic query based on the form with WHERE's
foreach ($form_fields as $name=>&$value) {
$query->where($name, '=', $value);
}
// Finally get all of the results...
$results = $query->get();
This does work fine in Laravel 3. Check out this [http://forumsarchive.laravel.io/viewtopic.php?id=1494] forum post for more information

Retrieve only one row of comment

I have a site developed in codeigniter where I want to retrieve comment of a specific tee.
I have a table tee like that:
- id
- user_id
- name
- created
- modified
And the table tee_comments like that:
- id
- user_id
- tee_id
- created
- modified
I have done this query:
$this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
$this->db->from('tee');
$this->db->join('tee_comments', 'tee_comments.tee_id = tee.id','left outer');
$this->db->order_by("tee.created", "desc");
$query = $this->db->get();
With this query I retrieve two rows of tee because I have two comments in that tee.
My goal is to retrieve only one row where inside there is an array of comment like:
tee{
id,
name,
created,
modified
comment{
[0]
id,
tee_id,
comment,
created,
modified
[1]
id,
tee_id,
comment,
created,
modified
}
}
I have tried into the join:
- left
- right
- left outer
- right outer
But doesn't solve the problem, is there a way to do that?
Thanks
I love CodeIgniter! I use it constantly! You have 2 really simple options here:
One way would be to use limit.
$this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
$this->db->join('tee_comments', 'tee_comments.tee_id = tee.id','left outer');
$this->db->order_by("tee.created", "desc");
$query = $this->db->limit(1)->get('tee');
Another way is to get first item in results Array
$query = $this->db->get();
$results = $query->result(); // gets return as an array
$row = $results[0]; // will be first row in results array
Keep in mind tho, $row will return as a object(stdClass) meaning you'll have to retrieve things from it like $row->column_name.
A handy little snippet I like to use after a call is below. It makes the row Object's Array's instead.
$results = $db->get('tee')->result(); // snippet comes after you have result array
// snippet here
foreach ($r as $k => $v) { $results[$k] = array(); foreach ($v as $kk => $vv) { $results[$k][$kk] = $vv != "NULL" ? trim($vv) : ""; } }
Use $this->db->limit(1) to retrieve a single record.
According to the accepted answer on this question, you may need to put the limit statement before the select:
$this->db->limit(1);
$this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
$this->db->from('tee');
Option one:
for ($i = 0; $i < count($records); $i++)
{
$data = $records[$i];
// work with date.
if($i == 1)
break;
}
Option two:
just assign the first row to var.
$row = $records['tee']['comment'][0];

Categories