codeigniter php Severity: Notice Message: Undefined index: user_id - codeigniter

array(3) {
[0]=> array(15) {
["guest_list_id"]=> string(1) "0"
["event_type_id"]=> string(1) "2"
["event_id"]=> string(1) "1"
["guest_attendance_status"]=> NULL
["user_id"]=> string(1) "9" },
When accessing $user['user_id'] , I get the error:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: user_id
Code from Comment
if (!empty($user)) {
foreach ($user as $key => $value) {
$user_details = array(
'user_id' => $user['0']['user_id'],
'eventTitle' => $user['pre_wedding_event_title']
);
}
}

Related

Laravel eloquent query, chaining groupBy on main query & subquery callback function

I tried to query Item model & group by groupId, then further group the group by user_id & sum the sales_count but got an error. Below is my code.
$items = Item::where('sold', true)->get()->groupBy('groupId', function ($query){
$query->select(DB::raw('userid,sum(sales_count) as total_sales_count'))->groupBy('userid');
})->get();
I got the error below:
Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in
Expected result (Eloquent\Collection):
[
17558 => [
[
"userid" => "2"
"total_sales_count" => "3"
],
[
"userid" => "4"
"total_sales_count" => "1"
],
],
18647 => [
[
"userid" => "5"
"total_sales_count" => "6"
],
[
"userid" => "7"
"total_sales_count" => "4"
],
]
]
Model Structure:
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->integer('userid');
$table->integer('sales_count')->default(1);
$table->string('groupId')->nullable();
$table->boolean('sold')->default(true);
$table->timestamps();
});
Your query should be like this -
$item = Item::where('sold', true)
->select('user_id', DB::raw('sum(items.sales_count) as total_sales_count'))
->groupBy('groupId')
->get();

Laravel Guzzle not working yet curl does: error is curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*

Could anybody help me with an issue regarding Guzzle in Laravel?
I have this bit of code using curl which works.
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, ['userId' => $appUserId]);
curl_setopt($ch, CURLOPT_URL, $url);
$content = curl_exec($ch);
var_dump($content); // I get a response, response is good.
curl_close($ch);
I get a good old response with all the data I need. Simple and nice.
However, the company I work with uses Guzzle throughout the app, so when I use Guzzle as such.
$client = new GuzzleHttp\Client();
try {
$res = $client->request('POST', $url, [
'form_params' => [
'userId' => $appUserId
],
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
// I have tried every imaginable header.
]);
$result = json_decode($res->getBody());
echo $res->getBody();
var_dump($result);
} catch (\GuzzleHttp\Exception\RequestException $e) {
var_dump('error');
$guzzleResult = $e->getResponse();
var_dump($guzzleResult);
}
I get major errors for some reason.
This is error.
object(GuzzleHttp\Psr7\Response)#317 (6) {
["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
string(12) "Unauthorized"
["statusCode":"GuzzleHttp\Psr7\Response":private]=>
int(401)
["headers":"GuzzleHttp\Psr7\Response":private]=>
array(7) {
["Host"]=>
array(1) {
[0]=>
string(14) "127.0.0.1:8000"
}
["Date"]=>
array(2) {
[0]=>
string(29) "Wed, 19 Aug 2020 13:39:56 GMT"
[1]=>
string(29) "Wed, 19 Aug 2020 13:39:56 GMT"
}
["Connection"]=>
array(1) {
[0]=>
string(5) "close"
}
["X-Powered-By"]=>
array(1) {
[0]=>
string(9) "PHP/7.3.6"
}
["Cache-Control"]=>
array(1) {
[0]=>
string(17) "no-cache, private"
}
["Content-Type"]=>
array(1) {
[0]=>
string(16) "application/json"
}
["Set-Cookie"]=>
array(2) {
[0]=>
string(316) "XSRF-TOKEN=eyJpdiI6ImlPSHczMXRUQ0ZZTUVpRkFPZ052UHc9PSIsInZhbHVlIjoiS0UrYVdkR2ZETWhcL2FLVjRhSVZPM1VxYjVWb3ZoN2dTZlwvWFlNa2hXam1WNVo1NHo3Mk9aZXc5ZFlzYzhlTlNUIiwibWFjIjoiYThiYWVlYjRmOWJmMDgxOWJmZDQzYjg2YWFmZDdjMDFiYzg0ODk5N2FlZTk3ZmI0YjY5MzlmNzYzMGExMThlYSJ9; expires=Wed, 19-Aug-2020 15:39:56 GMT; Max-Age=7200; path=/"
[1]=>
string(331) "laravel_session=eyJpdiI6ImpkK2ZtblU3MFpBN3hoSlJ1Wk5Nb2c9PSIsInZhbHVlIjoiVVYxeGtTV09ZZzVoMUcyXC8yaVI0XC9ZbWdYa0hQdkxQZ0xMamE1YzNaZ0NCTjJYbWJSUGl0MWMwRU9rczFUeHdQIiwibWFjIjoiNjJkMGVkYmI0ZDcwMTg4YmZmNjZmYjliYzFjOTI1ODRlN2I4ZTM1MjYyN2U0M2M0YzlmODk0YWVjYzRhNzAyOCJ9; expires=Wed, 19-Aug-2020 15:39:56 GMT; Max-Age=7200; path=/; httponly"
}
}
["headerNames":"GuzzleHttp\Psr7\Response":private]=>
array(7) {
["host"]=>
string(4) "Host"
["date"]=>
string(4) "Date"
["connection"]=>
string(10) "Connection"
["x-powered-by"]=>
string(12) "X-Powered-By"
["cache-control"]=>
string(13) "Cache-Control"
["content-type"]=>
string(12) "Content-Type"
["set-cookie"]=>
string(10) "Set-Cookie"
}
["protocol":"GuzzleHttp\Psr7\Response":private]=>
string(3) "1.1"
["stream":"GuzzleHttp\Psr7\Response":private]=>
object(GuzzleHttp\Psr7\Stream)#315 (7) {
["stream":"GuzzleHttp\Psr7\Stream":private]=>
resource(484) of type (stream)
["size":"GuzzleHttp\Psr7\Stream":private]=>
int(49)
["seekable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["readable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["writable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["uri":"GuzzleHttp\Psr7\Stream":private]=>
string(10) "php://temp"
["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
array(0) {
}
}
}
If I set the debug to true, I get this
ErrorException: curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE* in file C:\xampp-7\htdocs\website\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 59
I have used Guzzle before and it should be fairly simple.
You are passing some form_params to guzzle, but you are also setting the content-type to application/json and your data is not in json format. Try with
$res = $client->request('POST', $url, [
'form_params' => [
'userId' => $appUserId
],
'headers' => [
'Accept' => 'application/json'
],
]);
Also, to make sure the response body is treated as a string, you should use getContents()
$responseData = $res->getBody()->getContents();
$result = json_decode($responseData);
'debug' => fopen('php://stderr', 'w'),
this worked for me
$client->request('POST', $url, [
'debug' => fopen('php://stderr', 'w'),
'form_params' => [
'userId' => $appUserId
],
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
]);

remove empty child array from array

I have a problem which is that I have a array and in this array also a sub array I want to delete the sub-array that do not contain any values instead of the [] to null
This recursive function should do the trick... will only touch empty arrays
<?php
function emptyArraysToNull(&$object){
if(is_array($object))
foreach ($object as $key => &$value) {
if (is_array($value)) {
$value = emptyArraysToNull($value);
}
if (empty($value)) {
$object[$key] = null;
}
}
return $object;
}
// TEST
$very_nested_array = [
[
"name" => "test",
"id" => 1,
"districts" => [
"id" => 2,
"districts" => [],
"test" => []
]
],
[
"id" => []
]
];
print_r(emptyArraysToNull($very_nested_array));
Output:
array(2) {
[0]=>
array(3) {
["name"]=>
string(4) "test"
["id"]=>
int(1)
["districts"]=>
array(3) {
["id"]=>
int(2)
["districts"]=>
NULL
["test"]=>
NULL
}
}
[1]=>
array(1) {
["id"]=>
NULL
}
}
Live demo: http://sandbox.onlinephpfunctions.com/code/4644ec81b623d9cd0c79e1aa67ab55a64cd9416d

How to validate multiple input with the same name in a class extended from FormRequest

I'm new to Laravel.
I want to know how to validate multiple input with the same name.
I have a validation rule for a POST request but this fails the validation although I have all values required.
I noticed that the key names are not the same as the key names from user input, so I think that's causing the problem.
On the page I have a form like this, so, I have many input fields with the name like "visit[0]['did_visit']", "visit[1]['did_visit']", "visit[2]['did_visit']...
But, I can't figure out how to validate these on my class extended from FormRequest class.
Please help me!!
VisitRecordRequest extends FormRequest
class VisitRecordRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
switch($this->method())
{
case 'GET':
return [];
case 'POST':
{
//var_dump my input!!!
echo "<pre>";
var_dump($this->input());
echo "</pre>";
exit;
return [
'date' => 'required',
'hour' => 'required',
'minute' => 'required',
'weather' => 'required',
'room_id' => 'required',
'did_visit' => 'required',
'bounce_zone' => 'required',
'bounce_reason' => 'required',
'next_action' => 'required',
];
}
case 'PUT':
{
return [];
}
case 'PATCH':
{
return [];
}
case 'DELETE':
{
return [];
}
default:break;
}
return [];
}
public function response(array $errors)
{
}
}
var_dump log
array(2) {
["visit"]=>
array(3) {
["common"]=>
array(4) {
["date"]=>
string(10) "2017-05-13"
["hour"]=>
string(2) "09"
["minute"]=>
string(2) "00"
["weather"]=>
string(5) "sunny"
}
[0]=>
array(6) {
["room_id"]=>
string(2) "33"
["did_visit"]=>
string(2) "on"
["bounce_zone"]=>
string(1) "1"
["bounce_reason"]=>
string(1) "1"
["next_action"]=>
string(1) "2"
["memo"]=>
string(11) "hello world"
}
[1]=>
array(6) {
["room_id"]=>
string(2) "34"
["did_visit"]=>
string(3) "off"
["bounce_zone"]=>
string(1) "0"
["bounce_reason"]=>
string(1) "0"
["next_action"]=>
string(1) "1"
["memo"]=>
string(14) "hello world!!!"
},
[2]=>
array(6) {
["room_id"]=>
string(2) "35"
["did_visit"]=>
string(3) "off"
["bounce_zone"]=>
string(1) "3"
["bounce_reason"]=>
string(1) "2"
["next_action"]=>
string(1) "1"
["memo"]=>
string(14) "hello world!!!"
}
}
["_token"]=>
string(40) "2yvZEmM3SUxTcUAZusZs87B1fKD4edVFy0AY4kjC"
}
'visit.*.did_visit' => 'required'
Check here for more info https://laravel.com/docs/5.4/validation#validating-arrays
it should be visit => 'array' .. then have a separate validator for visit ..
then have
foreach($request->visit as $visit)
{
//validate visit ..
}

How to get all Magento Order status (List all magento order status)

How to get a list of all magento order status (pending, complete, processing etc.)?
It should show all values like the "status" drop down field in the order-index grid page in the magento backend.
Just use this simple line of code:
Mage::getModel('sales/order_status')->getResourceCollection()->getData();
For example:
var_dump( Mage::getModel('sales/order_status')->getResourceCollection()->getData() );
Result:
array(10) { [0]=> array(2) { ["status"]=> string(8) "canceled" ["label"]=> string(8) "Canceled" } [1]=> array(2) { ["status"]=> string(6) "closed" ["label"]=> string(6) "Closed" } [2]=> array(2) { ["status"]=> string(8) "complete" ["label"]=> string(8) "Complete" } [3]=> array(2) { ["status"]=> string(5) "fraud" ["label"]=> string(15) "Suspected Fraud" } [4]=> array(2) { ["status"]=> string(6) "holded" ["label"]=> string(7) "On Hold" } [5]=> array(2) { ["status"]=> string(14) "payment_review" ["label"]=> string(14) "Payment Review" } [6]=> array(2) { ["status"]=> string(7) "pending" ["label"]=> string(7) "Pending" } [7]=> array(2) { ["status"]=> string(15) "pending_payment" ["label"]=> string(15) "Pending Payment" } [8]=> array(2) { ["status"]=> string(14) "pending_paypal" ["label"]=> string(14) "Pending PayPal" } [9]=> array(2) { ["status"]=> string(10) "processing" ["label"]=> string(10) "Processing" } }
Get all order statuses and save the array $orderStatus[status][label] in $status associative array:
$orderStatusCollection = Mage::getModel('sales/order_status')->getResourceCollection()->getData();
$status = array();
$status = array(
'-1'=>'Please Select..'
);
foreach($orderStatusCollection as $orderStatus) {
$status[] = array (
'value' => $orderStatus['status'], 'label' => $orderStatus['label']
);
}
Here is the way to get the output for dop-down:
$statuses = Mage::getModel('sales/order_status')->getCollection()
->toOptionArray()
echo '<pre>';print_r($statuses);echo '</pre>';
//this will output:
Array
(
[0] => Array
(
[status] => canceled
[label] => Canceled
)
[1] => Array
(
[status] => cancel_ogone
[label] => Cancelled Ogone
)
[2] => Array
(
[status] => closed
[label] => Closed
)
[3] => Array
(
[status] => complete
[label] => Complete
)
.....

Resources