I tried to append several values in the same column of Laravel AJAX datatable. Here is what I typed in the controller :
$table->addColumn('academic_options', function ($row) {
return ($row->academic_section ? $row->academic_section->code : '') . ', ' . ($row->option_1 ? $row->option_1->code : '') .
', ' . ($row->option_2 ? $row->option_2->code : '') . ', ' . ($row->option_3 ? $row->option_3->code : '') . ', ' . ($row->option_9 ? $row->option_9->code : '');
});
And this is what is shown screenshot
How should I do it so the values will be separated properly, if possible how can i create a list of existed values, the empty value would be hidden.
Collect the right array in the right order, and then do "implode"
$array = [1,2,3,4,5];
echo implode(', ', $array); // Output: 1, 2, 3, 4, 5
Related
everyone.
In the codeigniter there is update_batch function by using it we are able to bulk update multiple rows.
$this->db->update_batch('table_name', $update_data_array, 'where_condition_field_name');
I want similar functionality in core PHP in one function or in one file. Is there any workaround?
Is there any way to extract update_batch function from Codeigniter in one file/function?
I tried to extract this function but it is very lengthy process and there will be many files / functions should be extracted.
Please help me in this regard
Thanks in advance
You can also insert multiple rows into a database table with a single insert query in core php.
(1)One Way
<?php
$mysqli = new mysqli("localhost", "root", "", "newdb");
if ($mysqli == = false) {
die("ERROR: Could not connect. ".$mysqli->connect_error);
}
$sql = "INSERT INTO mytable (first_name, last_name, age)
VALUES('raj', 'sharma', '15'),
('kapil', 'verma', '42'),
('monty', 'singh', '29'),
('arjun', 'patel', '32') ";
if ($mysqli->query($sql) == = true)
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. "
.$mysqli->error;
}
$mysqli->close();
? >
(2)Second Way
Let's assume $column1 and $column2 are arrays with same size posted by html form.
You can create your sql query like this:-
<?php
$query = 'INSERT INTO TABLE (`column1`, `column2`) VALUES ';
$query_parts = array();
for($x=0; $x<count($column1); $x++){
$query_parts[] = "('" . $column1[$x] . "', '" . $column2[$x] . "')";
}
echo $query .= implode(',', $query_parts);
?>
You can easily construct similar type of query using PHP.
Lets use array containing key value pair and implode statement to generate query.
Here’s the snippet.
<?php
$coupons = array(
1 => 'val1',
2 => 'va2',
3 => 'val3',
);
$data = array();
foreach ($coupons AS $key => $value) {
$data[] = "($key, '$value')";
}
$query = "INSERT INTO `tbl_update` (id, val) VALUES " . implode(', ', $data) . " ON DUPLICATE KEY UPDATE val = VALUES(val)";
$this->db->query($query);
?>
Alternatively, you can use CASE construct in UPDATE statement. Then the query would be something
like:
UPDATE tbl_coupons
SET code = (CASE id WHEN 1 THEN 'ABCDE'
WHEN 2 THEN 'GHIJK'
WHEN 3 THEN 'EFGHI'
END)
WHERE id IN(1, 2 ,3);
Hi I am trying to combine two collections in a way that, the difference from the second collection is inserted one by one at the end of the first collection. However, when I try to use diff to find the difference between two collections and push each different set, the order seems to be messed up. Can someone help me with this please?
$serviceids = Service::where('service_name', 'like', '% ' . $chip_service . ' %')
->orWhere('service_name', 'like', '% ' . $chip_service)
->orWhere('service_name', 'like', $chip_service . ' %')
->orWhere('service_name', $chip_service)
->pluck('service_recordid');
$service_description_all = Service::where('service_description', 'like', '% ' . $chip_service . ' %')
->orWhere('service_description', 'like', '% ' . $chip_service)
->orWhere('service_description', 'like', $chip_service . ' %')
->orWhere('service_description', $chip_service)
->pluck('service_recordid');
$service_description = $service_description_all->diff($serviceids)->all();
foreach ($service_description as $key => $ser){
$serviceids->push($ser);
}
push function acts like stack, so the first element will be at the end the last in the stack.
you can use directly:
$serviceids = $service_description + $serviceids
I'm trying to property from collections, but I can't do this.
I have this query:
$calls = collect(
DB::select("SELECT count(id)
FROM calls
WHERE started_at >= '" . $date . "' AND
linked_id IS NULL") );
And as the result I got this
Illuminate\Support\Collection {#82146
all: [
{#1097
+"count": 15,
},
],
}
How can I get this "count"? I've tried to
$calls->count
$count = get_object_vars($calls)
but in first case I have:
PHP error: Undefined property: Illuminate\Support\Collection::$count on line 1
and in the second I got empty array.
First, Please give alias to "count(id) as calls_count"
Here is your query.
$calls = collect(DB::select("SELECT count(id) as calls_count FROM calls WHERE started_at >= '" . $date . "' AND linked_id IS NULL"));
Here are few ways to get value of count now.
1.)
echo $calls[0]->calls_count;
2.)
$calls = $calls->first();
echo $calls->calls_count;
3.)
echo $calls->pluck('calls_count')->first()
(I prefer this)
Hope this help, Let me know i comment, if any doubt.
I have the following query (cut for brevity):
$employees = Employee::where('first_name', 'LIKE', "%$query%")
->orWhere('last_name', 'LIKE', "%$query%")
Now this works when the user inputs a single name like 'John' or 'Smith', but when they input 'John Smith' it doesn't find anything. Am I missing an extra orWhere?
Try this :
Employee::where(DB::raw('CONCAT(first_name," ",lastname)'), 'LIKE', "%' . $query . '%"))
You would have to add a 3rd orWhere. For our search function we use something like this:
Employee::whereraw("COALESCE(last_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(first_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(last_name + ', ' + first_name, '') LIKE '%$query%'")
Adding Coalesce seemed to help with some issues we had when we first implemented it, not sure if it is necessary in your case though.
You can do :
$fullName = trim($query);
$employees = Employee::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%".$fullName."%")->get();
You are concatenating the values in database for first_name + ' ' + last_name and then using like to find the matching records.
I wanted to get the selected fields of the query from an outside array.
foreach($param as $key => $val){
if($val == 'userId'){
$string .= "adminusers.id, ";
}
if($val == 'name'){
$string .= "CONCAT(firstName, ' ', lastName) as name";
}
}
My query is right below;
$where = '1';
$resultSet = UserAdmin::whereRaw($where)
->addSelect(array($string))
->groupBy('adminusers.id');
However, I received this :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'stmd_adminusers.id, CONCAT(firstName, ' ', lastName)' in 'field list' (SQL: select stmd_adminusers.id, CONCAT(firstName, ' ', lastName) as name from stmd_adminusers
Whenever you use Some Mysql native function you have to use DB::raw()
$resultSet = UserAdmin::whereRaw($where)
->addSelect(array(DB::raw($string)))
->groupBy('adminusers.id');
Hope this helps.
You can use ->selectRaw($string) instead:
$where = '1';
$resultSet = UserAdmin::whereRaw($where)
->selectRaw($string)
->groupBy('adminusers.id');
This has the advantage over using ->addSelect(DB::raw($string)) that you can (optionally) add a second parameter $bindings; which will protect you from SQL injection attacks more than using DB::raw() would.