can't update json column using Laravel eloquent model - mysql

I'm trying to test an update to an Eloquent model...
/** #test */
public function updates_to_json_fields_are_logged()
{
$data = json_encode(["json_key" => "old_value"]);
$individual = Individual::factory()->create([
"information" => $data
]);
json_decode($individual->information)->json_key = "new_value";
$individual->save();
echo(var_dump($individual));
$this->assertTrue(false);
}
information is a json column.
When I log $individual after saving it, the value of "information->json_key" is still "old_value". Can anyone tell me why?

To change the $individual object without fancy assign
/** #test */
public function updates_to_json_fields_are_logged()
{
$data = json_encode(["json_key" => "old_value"]);
$individual = Individual::factory()->create([
"information" => $data
]);
$decodedInformation = json_decode($individual->information);
$decodedInformation->json_key = "new_value";
$individual->information = json_encode($decodedInformation);
$individual->save();
echo(var_dump($individual));
$this->assertTrue(false);
}

You don't change original $individual object, but the result of json_decode().

Related

How to retrieve data from MySQL to JSON?

I have a Symfony project where I want to store the all the rows from my MySQL table to JSON. Currently there are five rows in my table, but in my browser it only returns five empty values as {"results":[{},{},{},{},{}]}
I guess I have done something right, but not everything. What am I missing in my code?
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
$results = $repository->findAll();
return $this->json(['results' => $results]);
}
Try createQueryBuilder its usefull.
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
$qb = $repository->createQueryBuilder("b");
$results = $qb->getQuery()->getArrayResult();
return $this->json(['results' => $results]);
}
You can use the serializer or re-create the array yourself like that
$courses = $doctrine->getRepository(Course::class)->findByLikeTitle($search, $userId);
foreach ($courses as $key => $course) {
$jsonCourses[$key]['title'] = $course->getTitle();
}
```
You can achieve this by Using a Serializer to convert the array of objects into JSON. There are other ways to achieve this like using jsonResponse for example. But the serializer is the most robust way imo.
Example only:
use Symfony\Component\Serializer\SerializerInterface;
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository, SerializerInterface $serializer)
{
$results = $repository->findAll();
$jsonResults = $serializer->serialize($results, 'json');
//If you need to handle any circular references, you can use this..
$jsonResults = $serializer->serialize($results, 'json', array(
'circular_reference_handler' => function ($object) { return $object; },
));
return $jsonResults;
}

How to fetch data from single column stored in array using laravel?

I need to fetch the each and every column individually from the field array_payout can anyone solve it
$currentMonth_start_Date = Carbon::now()->startOfMonth()->subMonth(1);
$currentMonth_end_date = Carbon::now()->subMonth()->endOfMonth();
$clients_referral_tree = DB::table('mam_referral_payout')
->select('clients.id', 'mam_referral_payout.*')
//->addSelect(DB::raw('SUM(mam_referral_payout.Final_amount) as referral_amount'))
->leftjoin('clients', 'clients.id', '=', 'mam_referral_payout.to_id')
->where('clients.id', '=', (Auth::user()->id))
->whereBetween('mam_referral_payout.created_at', [$currentMonth_start_Date, $currentMonth_end_date])->get();
$clientTree = [];
foreach ($clients_referral_tree as $tree) {
$clientThree = $tree;
$clientTree[] = $clientThree;
}
dd($clientTree);
You can add the following to your Model:
protected $casts = [
"array_payout" => "object"
];
Add an accessor for each attribute in your json like this for example:
public function getTotalAmountAttribute(){
return optional($this->array_payout)->total_amount;
}
You can use them like this:
$clientTree1->total_amount;

how to select a single row from table, the model of which has function

class CreditRule extends Model
{
protected $fillable = array(
'department_id',
'created_by',
'updated_by'
);
public function department()
{
return $this->belongsTo('App\Department');
}
And my controller is
$creditRules = CreditRule::get();
You mean something like
CreditRule::find(1);
This will retrieve the row where id=1 for model CreditRule.
#Eliza, You can use where in your controller while accessing data from model.
Simple structure of where this is :
$creditRules = CreditRule::where('id', 1)->first();
Get model by id:
$element = Model::find(id)
Get all rows of model by matching a field:
$element = Model::where('field','=',$value)->get()
Or to get the first one only:
$element = Model::where('field','=',$value)->first()

Yii2 Select only few columns from related model

In controller i have:
public function actionGetItems()
{
$model = new \app\models\WarehouseItems;
$items = $model->find()->with(['user'])->asArray()->all();
return $items;
}
In WarehouseItem model i have standard (created by gii) relation declaration:
public function getUser()
{
return $this->hasOne('\dektrium\user\models\User', ['user_id' => 'user_id']);
}
How can i control which column data do i get from "user" relation? I currently get all columns which is not good as that data is being sent to Angular in JSON format.
Right now i have to loop trough $items and filer out all columns i dont want to send.
You should simply modify the relation query like this :
$items = \app\models\WarehouseItems::find()->with([
'user' => function ($query) {
$query->select('id, col1, col2');
}
])->asArray()->all();
Read more : http://www.yiiframework.com/doc-2.0/yii-db-activequerytrait.html#with()-detail
Your code should go this way.
public function actionGetItems()
{
$items = \app\models\WarehouseItems::find()
->joinWith([
/*
*You need to use alias and then must select index key from parent table
*and foreign key from child table else your query will give an error as
*undefined index **relation_key**
*/
'user as u' => function($query){
$query->select(['u.user_id', 'u.col1', 'u.col2']);
}
])
->asArray()
->all();
return $items;
}
Inside WarehouseItem model
/**
* #return ActiveQuery
*/
public function getUser()
{
$query = User::find()
->select(['id', 'col1', 'col2'])
->where([
'id' => $this->user_id,
]);
/**
* Default hasOne, setup multiple for hasMany
* $query->multiple = true;
*/
return $query;
}

How to return Repository Objects as Json on Symfony2

I'm trying to return the users like this, but of course it doesn't work, I need the data as JSon since im working with BackboneJs
/**
* #Route("/mytest",name="ajax_user_path")
*/
public function ajaxAction()
{
$em = $this->get('doctrine')->getManager();
$users = $this->get('doctrine')->getRepository('GabrielUserBundle:Fosuser')->findAll();
$response = array("users"=>$users);
return new Response(json_encode($response));
}
Thanks for your help guys, here is the Solution
Get the JMSSerializerBundle,
This is the code on the controller
/**
* #Route("/user")
* #Template()
*/
public function userAction()
{
$em = $this->get('doctrine')->getManager();
$users = $this->get('doctrine')->getRepository('GabrielUserBundle:Fosuser')->findAll();
$serializer = $this->get('jms_serializer');
$response = $serializer->serialize($users,'json');
return new Response($response);
}
So, findAll returns an array of entities (objects) and json_encode cannot correctly encode that array. You have to prepare your data berofe send response like that:
Example:
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* #Route("/mytest",name="ajax_user_path")
*/
public function ajaxAction()
{
$users = $this->get('doctrine')->getRepository('GabrielUserBundle:Fosuser')->findAll();
$response = array();
foreach ($users as $user) {
$response[] = array(
'user_id' => $user->getId(),
// other fields
);
}
return new JsonResponse(json_encode($response));
}
Moreover, it would be great if you put preparing response to ex. UserRepository class.
With Symfony you have JsonResponse like :
return new JsonResponse($users);
And don't forget to add the header :
use Symfony\Component\HttpFoundation\JsonResponse;
I have never tried to encode a complete object, but I have used json with arrays of informations like this:
$vars = array(
'test' => 'test'
);
$response = new JsonResponse($vars);
return $response;
As you can see in JsonResponse, its function setData() is encoding the array, so you don't have to do it yourself:
public function setData($data = array())
{
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
$this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
return $this->update();
}