How to insert an array in MySQL table with Laravel ORM - mysql

I am trying to insert an entire api response into a MySQL table using Laravel Eloquent but I am getting 'Array to string conversion' error. How do I solve this?
Please note, it is compulsory that I save the entire API response.
My API call
$response = Curl::to($url.$request->account_number)
->withData($data)
->asJson(true)
->get();
My Query
TransactionLog::create([
'payer' => $request->payer,
'amount' => $request->amount,
'phone' => $request->phone,
'response' => $response
]);

Looks like you using ixudra/curl ?
With asJson(true) you will receive json_decode response, so if your plan is to save raw json response in the database you'll have to json_encode it like:
TransactionLog::create([
'payer' => $request->payer,
'amount' => $request->amount,
'phone' => $request->phone,
'response' => json_encode($response)
]);

Related

Laravel validation JSON response order

I have a validation.
$request->validate([
'bike_brand_id' => 'required',
'bike_type_id' => 'required',
'bike_gender_id' => 'required',
'bike_size_id' => 'required',
'bike_color_id' => 'required',
'bike_brake_id' => 'required',
]);
The JSON response from Laravel always returns in alphabetical order like this;
What I need is the response to be in the order of the validation rules.
(Btw. the php response returned in variable $errors by default is in correct order)
I indeed realized that the console preview orders the errors alphabetically, while the actual returned errors are in correct order

codeigniter-restserver how to put json into the response

I'm trying to put json data into the codeigniter-restserver response, but the json code is put into double quotes effectively rendering it unreadable.
I'm trying to set the response like this currently:
$this->response(array(
'status' => $result['success'],
'error' => $result['cause'],
'result' => $result['result']
), $result['statuscode']);
Where $result['result'] is the json code.
Use the json_decode() to convert your json data in to array. Try it...
$this->response(array(
'status' => $result['success'],
'error' => $result['cause'],
'result' => json_decode($result['result'],true)
), $result['statuscode']);

CakePHP redirect with form parameter

In CakePHP 3.1 I am redirecting to login page after a database update and I want to pass back the email so that form input is already populated.
I can do it the standard way with using the controller and action in the redirect.
Instead I am using just a url string
return $this->redirect('/login',['?' => ['email' => $email]]);
This gives me the error Unknown status code
The redirect method expects a status code as the second parameter. You will need to provide and array-based URL as the first parameter or append the query var to the current string.
return $this->redirect('/login?email=' . $email);
return $this->redirect([
'controller' => 'Users',
'action' => 'login',
'?' => [
'email' => $email
]
]);

Retrieve value from new column

I am trying to learn opencart structure, and trying to create a new column under the table product. The new column is "test"
Then I try to retrieve the data under this page index.php?route=checkout/cart (replace price with test column)
catalog\controller\checkout\cart.php
...
$this->data['products'][] = array(
'key' => $product['key'],
'thumb' => $image,
'name' => $product['name'],
'model' => $product['model'],
'option' => $option_data,
'quantity' => $product['quantity'],
'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')),
'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''),
'price' => $product['test'], //<-- new column
'total' => $total,
'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']),
'remove' => $this->url->link('checkout/cart', 'remove=' . $product['key'])
);
The problem is I'm not getting any output, and I'm not sure how to work with the model. Which query/function is related with this page ?
The problem is that the $products that are available at cart.php controller are retrieved from the session where they have been stored in previously set structure, so there is no test index and You should get a Notice: undefined index 'test' in .... The $products are retrieved by
foreach ($this->cart->getProducts() as $product) {
//...
}
See /system/library/cart.php and method getProducts() to understand what I am speaking about.
If You would like to use this at catalog/controller/product/category.php or catalog/controller/product/product.php controllers, the code You are trying will work.
If You replace the price within all product lists and product detail, these controllers:
product/
category.php
manufacturer_info.php
product.php
search.php
special.php
module/
bestseller.php
featured.php
latest.php
special.php
with Your value, the final price within cart would be Your test value.

Extract data from array in DB (rails)

I am trying to extract some data from an array with the following syntax:
#entries_from_db = XrEntry.find(:all, :conditions => [:FeedURI => uri ], :select => 'json')
The :FeedURI is the record that contains an array with uri's ["123456", "23345", "4453"]
The uri is the variable wich contains the current uri.
The statement I'm trying to make is 'select JSON from XrEntry where FeedURI contains uri'
Im stuck on the part to access the array and always get several error msg's when I'm trying different code.
Does anyone has an idea?
Thanks!
I solved it with this syntax
#entries_from_db = XrEntry.find(:all, :conditions => ["FeedURI like ?", "%#{uri}%"] , :select => 'json')
the "%#{your_rails_variable}%" is needed to read in an array
You seem to have switched the condition syntax. you chould start with the db attribute and then the variable.
#entries_from_db = XrEntry.find(:all,
:conditions => { :uri => FeedURI },
:select => 'json')
That will return an array of XrEntry objects with only the json attribute present. To get an array of only the json data you could map it like this:
#json_array = #entries_from_db.map(&:json)