Array to string conversion insert to database - arrays

I have a problem with array to string again. But this time I need insert to database.
Checks::find($id)->update($request->all());
$issue[] = $request->issue;
foreach ($issue as $item) {
dd($item);
addIssues::create([
'check_id' => $id,
'issue_id' => $item
]);
}
Migration:
Schema::create('checks_issues', function (Blueprint $table) {
$table->string('check_id');
$table->string('issue_id');
$table->timestamps();
$table->primary(['check_id','issue_id']);
});

foreach ($issue as $item){
foreach ($item as $item1){
addIssues::create([
'check_id' => $id,
'issue_id' => $item1
]);
}

You should try this:
Checks::find($id)->update($request->all());
if(!empty($request->issue)){
$issue = explode(',',$request->issue);
foreach ($issue as $item){
addIssues::create([
'check_id' => $id,
'issue_id' => $item
]);
}
}

Related

Cant change single value in session array in Laravel

This my method update for products. And cant to change qty of product. Data saved inside foreach, but nothing change out of this. I can't understand what the problem.
if (!session()->has('products')) {
session()->put('products', []);
}
$products = session()->get('products');
foreach ($products as $item) {
if (isset($item[$product->id])) {
$item[$product->id]['qty'] = 10;
dd($products); // dd($item) - changes
$request->session()->put('products', $products);
return redirect()->back();
}
}
session()->push('products', [
$product->id => [
'product' => $product,
'qty' => 1
]
]);
return redirect()->back();
As you can see there is dd($products) - so its not change. But if dd($item) so it has qty 10.
I can solved this with next:
foreach ($products as $key => $value) {
if (isset($value[$product->id])) {
$value[$product->id]['qty'] = 10;
$products[$key] = $value;
$request->session()->put('products', $products);
return redirect()->back();
}
}
Just add a $key => $value and save it in $products[$key] = $value;

can not use pagination() for an array object in laravel

I am constructing an array in Laravel 5.7. I would like to paginate it, but it errors because of array feature. what shall I do?
In MyController.php:
$result = array();
foreach ($sameDateMatches as $key => $date) {
array_push($result, [
'date' => $key,
'day_of_week' => getDayOfWeek($key),
'matches' => $date,
]);
}
if (!empty($_GET['page'])) {
$result = $result->paginate(10);
$pagination = $result;
} else {
$result = $result;
$pagination = null;
}
return returnSuccessfulResponse(
trans('api.response.successful.show'),
[
'Scheduled Matches' => $result,
],
$pagination
);
Call to a member function paginate() on array
You can only use paginate on an instance of QueryBuilder or on an Eloquent query.
Instead, if you need to 'paginate' an array, you can use PHP array_chunk.
array_chunk($result, 10, true);
I solved my problems using make() and LengthAwarePaginator as below:
if (!empty($_GET['page'])) {
$per_page = !empty($_GET['per_page']) ? $_GET['per_page'] : 10;
$page = $_GET['page'];
$result = $result instanceof Collection ? $result : Collection::make($result);
$result = new LengthAwarePaginator(
$result->forPage($page, $per_page)->values(),
$result->count(),
$per_page,
$page,
['path' => request()->url()]
);
$pagination = $result;
} else {
$pagination = null;
}

Convert array laravel

I use Laravel framework and I have an array:
[▼
"0.022" => "24.00000000"
"0.013" => "506.00000000"
"0.041" => "65.00000000"
]
Could you help me to convert it to new format like this:
[▼
0 => {▼
"xxx": 0.022
"yyy": 24.00000000
}
1 => {▼
"xxx": 0.013
"yyy": 506.00000000
}
2 => {▼
"xxx": 0.041
"yyy": 65.00000000
}
]
Thank you very much.
$inputArray=array(
"0.022" => "24.00000000"
"0.013" => "506.00000000"
"0.041" => "65.00000000"
);
$outputArray=array();
foreach($inputArray as $key=>$val)
{
$obj['xxx']= $key;
$obj['yyy']= $val;
array_push($outputArray,$obj)
}
echo $outputArray;
$array = [
"0.022" => "24.00000000",
"0.013" => "506.00000000",
"0.041" => "65.00000000"
];
$data=array();
foreach($array as $key=>$value)
{
$data[]= ['xxx'=>$key,'yyy'=>$value];
}
echo "<pre>";
echo(json_encode($data));
Based on your output, you want increment index as the key
$item = [
'0.022' => '24.00000000',
'0.013' => '506.00000000',
'0.041' => '65.00000000'
];
$output = [];
$count = 0;
foreach($item as $key => $value) {
$output[$count]['xxx'] = $key;
$output[$count]['yyy'] = $value;
$count++;
}
echo json_encode($output);

applying a Function to Each element in an array (O'Reilly cookbook example)

for nested data.
I tried
<?php
$names = array('firstnames' => array("Baba", "Billy"),
'lastnames' => array("O'Riley", "O'Reilly"));
array_walk_recursive($names, function (&value, $key) {
$value = htmlentities($value, ENT_QOUTES);
})
foreach ($names as $nametypes) {
foreach ($nametypes as $name) {
print "$name\n";
}
}
?>
(An example from the book O'reilly PHP Cookbook 3rd Edition) page 148
but I keep getting a message
T_String, expecting T_Variable
use &$value instead of &value
use ENT_QUOTES instead of ENT_QOUTES
Typos in your code (ENT_QUOTES, $value):
array_walk_recursive($names, function(&$value, $key) {
$value = htmlentities($value, ENT_QUOTES);
});
foreach ($names as $nametypes) {
foreach ($nametypes as $name) {
print "$name\n";
}
}
it's working.
thanks all
<?php
$names = array('firstnames' => array("Baba", "Billy"),
'lastnames' => array("O'Riley", "O'Reilly"));
array_walk_recursive($names, function (&$value, $key) {
$value = htmlentities($value, ENT_QUOTES);
});
foreach ($names as $nametypes) {
foreach ($nametypes as $name) {
print "$name\n";
}
}
?>

CodeIgniter Session do not saving Array

i using CodeIgniter Session with database for save a array with $_FILES, but don't save. I do this (but the array never increment):
The post of form, redirects to himself.
Function to upload e load the page of upload.
public function getUpload($codtemp, $codmessage){
$this->load->library('session');
$this->layout = '';
$data = array();
$data['codmessage'] = $codmessage;
$data['codtemp'] = $codtemp;$tempfiles= $this->session->userdata('tempfiles');
if (isset($_FILES['attachment']))
{
$files = $this->fixGlobalFilesArray($_FILES['attachment']);
foreach($files as $file)
{
$tempfiles[$codtemp][] = $file;
}
$this->session->set_userdata('tempfiles', $tempfiles);
unset($files);
}
$this->parser->parse('attachment_upload', $data);
}
private static function fixGlobalFilesArray($files) {
$ret = array();
if(isset($files['tmp_name']))
{
if (is_array($files['tmp_name']))
{
foreach($files['name'] as $idx => $name)
{
$ret[$idx] = array(
'name' => $name,
'tmp_name' => $files['tmp_name'][$idx],
'size' => $files['size'][$idx],
'type' => $files['type'][$idx],
'error' => $files['error'][$idx]
);
}
}
else
{
$ret = $files;
}
}
else
{
foreach ($files as $key => $value)
{
$ret[$key] = self::fixGlobalFilesArray($value);
}
}
return $ret;
}
Before save the array, serialize the data and after get the data, unserialize. If data is empty, create a array

Resources