Get the data inside the column in laravel notification - arrays

Using DD in my controller i have this result. Now i want to get the "id":45 in column name data
Controller:
public function readbyid($id){
$test = DB::table('notifications')->where('id','=', $id)->get();
dd($test);}

Id is unique, use first instead of get
$test = DB::table('notifications')->where('id', $id)->first();
$dataId = $test->data->id;
//or $dataId = json_decode($test->data)->id;

use App\Notification;
$test = Notification::where('id', $id)->first();
$dataId = $test->id;
dd($dataId);

Related

save mathod in database cause Array to string conversion error in laravel

when I execute custom command with
php artisan query:all
every thing is good except error shown in console the error is
Array to string conversion
and the data is stored to database I did not understand the cause of this error and it's hidden when hide save to database method
the code of my service which the problem cause inside it is
<?php
namespace App\Services;
use Carbon\Carbon;
use GuzzleHttp\Client;
use App\Models\weatherStatus;
use Illuminate\Support\Collection;
class ApixuService
{
public function query(string $apiKey, Collection $cities): Collection
{
$result = collect();
$guzzleClient = new Client([ //create quzzle Client
'base_uri' => 'http://api.weatherstack.com'
]);
foreach ($cities as $city) {
$response = $guzzleClient->get('current', [
'query' => [
'access_key' => $apiKey,
'query' => $city->name,
]
]);
$response = json_decode($response->getBody()->getContents(), true); //create json from $response
$status = new weatherStatus(); //create weatherStatus object
//adding prameters
$status->city()->associate($city);
$status->temp_celsius = $response['current']['temperature'];
$status->status = $response['current']['weather_descriptions'];
$status->last_update = Carbon::createFromTimestamp($response['location']['localtime_epoch']);
$status->provider = 'weatherstack.com';
//save prameters
$status->save();
$result->push($status);
}
return $result;
}
}
So you can find some clarity in what you are trying to save, do the following:
$response = json_decode($response->getBody()->getContents(), true);
dd($response);
dd() will dump all the data from the $response and exist the script.
One of the values you are trying to save is an array. The field you are trying to save accepts a string and not array.

How to save custom datetime in cakephp 3 for created & modified field?

I'm trying to save the following data. But it's not saving the datetime fields.
Created is saving current datetime in cakephp way & the others are null.
How can I save custom datetime?
Thanks in advance
foreach ($passwords as $key => $value ) {
$data['id'] = $collection_id;
$data['name'] = $value['password'];
$data['collection_id'] = $collection_id;
$data['photo_count'] = $value['photos_count'];
$data['download_count'] = $value['downloaded_users_count'];
$data['created'] = $value['created'];
$data['modified'] = $value['modified'];
$data['expires_at'] = $value['expires_at'];
$saveData[] = $data;
}
$passwords = $this->Passwords->find('all')->toArray();
$patched = $this->Passwords->patchEntities($passwords, $saveData);
foreach ($patched as $entity) {
$this->Passwords->save($entity);
}
I've fixed the Issue. Just have to use as follows-
$data['created'] = new Time($value['created']);
$data['expires_at'] = new Time($value['expires_at']);
And to do so, have to use this.
use Cake\I18n\Time;

Laravel 5 form request

When I handle a CREATE post form, i'll do :
public function handlecreer(Requests\CreerUtilisateurRequest $request)
{
// handle create form
// all fields in one line... YEAH !!!
$user = new User($request->except('password','role'));
$user->password = bcrypt(Request::input('password'));
$user->save();
....}
But if I have an UPDATE post form I'll do :
public function handleUpdate(Requests\UpdateUtilisateurRequest $request)
{
// handle update form
$user = User::findOrFail(Request::input('id'));// find
// one line by field... BOH !!!
$user->name = Request::input('name');
$user->email = Request::input('email');
$user->password = bcrypt(Request::input('password'));
$user->telephone= Request::input('telephone');
$user->fonction = Request::input('fonction');
$user->divers = Request::input('divers');
$user->save();
....}
Is there a simplest way of processing the update post form ?
Thanks,
Paguemaou
All mass assignable attributes (the one in $fillable) can be set using fill():
$user = User::findOrFail(Request::input('id'));
$user->fill($request->except('password', 'role'));
$user->password = bcrypt(Request::input('password'));
$user->save();

Cakephp pagination for a custom function

I have a function in my model EmployeePersonal.php named get_employee_details which accepts some parameter and return the employees details. This function is used my many controllers and I prefer not to change this function.
Now I have a function view in my EmployeePersonalsController.php which outputs approx 4000 results. eg
$res = $this->EmployeePersonal->get_employee_details(
$office_id = $office_id,
$epf_no = $epf_no,
$employee_personal_id = $employee_personal_id,
$status = $status,
$permanent_district_id = $permanent_district_id,
$present_district_id = $present_district_id,
$department_id = $department_id,
$designation_id = $designation_id,
$category_id = $category_id,
$gender_type = $gender_type,
$offset = $offset,
$size = $size
);
I need to put a pagination in my view function , is there any way how can I put the $paginate here ?

CakePHP 2.5 Datasource, create and return response

I have a specific task to connect CakePHP web application to a remote restful server . I create a datasource, read method works great, but the api after save data return an array of processed data.
Looking for a way to return the data array and use in controller.
My Controller code
public function admin_generate()
{
$data = $this->request->data;
$data['path'] = 'special/generate';
$this->Tool->create();
if($this->Tool->save($data)){
// handle response ????
}
$this->set('data',$data);
$this->set('_serialize','data');
}
In datasource file
public function create(Model $model, $fields = null, $values = null)
{
$data = array_combine($fields, $values);
$api = $this->config['api_path'].$data['path'].'?auth_key='.$this->config['auth_key'];
$json = $this->Http->post($api, $data);
$response = json_decode($json, true);
if (is_null($response)) {
$error = json_last_error();
throw new CakeException($error);
}
return $response; // ??????
}
Can someone show me the correct way to use the api response data in the controller?
I found a solution, a few minutes after a post question. This can help one of you.
datasource
....
if (is_null($response)) {
$error = json_last_error();
throw new CakeException($error);
}
// SOLUTION
$model -> code = $response['code'];
$model -> key = $response['key'];
$model -> code_id = $response['code_id'];
return true;
.....
in controller
.....
if($this->Tool->save($data)){
unset($data['path']);
$data['code'] = $this->Tool->code;
$data['key'] = $this->Tool->key;
$data['code_id'] = $this->Tool->code_id;
}
.....

Resources