I want to format some column fetched from database but didn't know how. I use Laravel 8.x. Here's the snippet :
GarageController.php
$q = Garage::with('relative')->paginate(15);
return response()->json($q);
the output
{
"data": [
{
"id": 39819,
"name": "john",
"date": "2020-12-20", // i want to format this with date_format()
"relative": {
"rid": 912039,
"rname": "ROV" // and i just want this value instead of all columns
}
},
{
"id": 38178,
"name": "danny",
"date": "2020-12-20", // and this too
"relative": {
"rid": 182738,
"rname": "YIV"
}
}
],
"links": {
.....
},
"meta": {
....
}
}
The model, the garage have is belong to relative (relative could have many garage).
public function relative() {
return $this->belongsTo(Relative::class, 'relative', 'rid');
}
I tried accessor, but it didn't change anything
public function getDateAttributes($value) {
return date_format(date_create($value), 'd/m/Y');
}
Any help would be appreciated. Thank you.
I think you just need to remove s from the getDateAttributes
From
public function getDateAttributes($value) {
return date_format(date_create($value), 'd/m/Y');
}
To
public function getDateAttribute($value) {
return date_format(date_create($value), 'd/m/Y');
}
Related
I am trying to use an accessor on a model to return the status whether a relationship exists.
My User model:
class User {
protected $appends = ['has_profile'];
public function profile()
{
return $this->hasOne(Profile::class)
}
public function getHasProfileAttribute()
{
$exists = $this->profile;
if($exists){
return 1;
}
else{
return 0;
}
}
}
The problem is when the User model is loaded via User::find(1)->get();, the profile property is also loaded into JSON resource whereas, I only want the has_profile attribute in my JSON return. How should I query the relationship existence without loading it, or should I unload the relationship?
What I Get
"data": {
"id": 270,
"name": "John Doe",
"mobile_number": "01234567890",
"created_at": "2021-08-19T06:55:33.000000Z",
"updated_at": "2021-08-19T06:55:33.000000Z",
"deleted_at": null,
"has_profile": 1,
"profile": {
"id": 1,
"details": "Details"
}
}
What I want
"data": {
"id": 270,
"name": "John Doe"
"mobile_number": "01234567890",
"created_at": "2021-08-19T06:55:33.000000Z",
"updated_at": "2021-08-19T06:55:33.000000Z",
"deleted_at": null,
"has_profile": 1
}
Updated Solution
The problem was $this->profile which led to the profile relation being attached. When used as $this->profile()->get(); or $this->profile()->first(); it works as expected.
You can use unset to remove the attribute profile.
public function getHasProfileAttribute()
{
$exists = $this->profile;
unset($this->profile);
if($exists){
return 1;
}
else{
return 0;
}
}
You can use the except() method from the documentation
User::find(1)->get()->except('profile');
Maybe you have to change order i can't test right now but it's the idea
In controller get data from db like this, I want to pass whole of $request to another function in this controller to get price it calculating price based of many things from $request:
$user = Auth::user();
$query = Post::query();
$query
->where('province', '=', $user->province)
->where('city', '=', $user->city);
$customers = $query->get();
$customers['calculator'] = $this->calculator($request); // call function
my problem is it return like this:
{
"0": {
"id": 1,
"hash": "RqH29tkfm1dwGrXp4ZCV",
},
"1": {
"id": 3,
"hash": "RqH29tkfm1dwGsXp4ZCV",
},
"calculator": {
"price": 1
}
}
But I need to use that function for each data, and result should be like this:
{
"0": {
"id": 1,
"hash": "RqH29tkfm1dwGrXp4ZCV",
"calculator": {
"price": 1
}
},
"1": {
"id": 3,
"hash": "RqH29tkfm1dwGsXp4ZCV",
"calculator": {
"price": 1
}
}
}
What you want is to set a calculator key for each item in the $customers collection. So you need to loop over it:
foreach ($customers as $customer) {
$customer->calculator = $this->calculator($request);
}
Notice that since the $customer is a Model you should set the calculator as a property. Internally it will be set to the attributes array.
How would one in an OOP-way "hydrate", "map", "create" (or whatever it's called) an object or model representation of this JSON data. The JSON string I've included below is the raw data received from Google's AdWords API and is not an exact copy of the structure I want and need, and I may therefor not just cast it to an object and similar.
I've been looking at various mapping libraries and hydration libraries but I cannot seem to wrap my head around how to get it to work properly.
{
"report-name": {
"#name": "Criteria performance report #5b3e67189ac85",
},
"date-range": {
"#date": "Jun 5, 2018-Jul 4, 2018",
},
"table": {
"columns": {
"column": [
{
"#name": "adGroup",
"#display": "Ad group",
},
{
"#name": "impressions",
"#display": "Impressions",
},
{
"#name": "clicks",
"#display": "Clicks",
},
{
"#name": "cost",
"#display": "Cost",
}
]
},
"row": [
{
"#adGroup": "Ad Group 1 of Another Campaign",
"#impressions": 0,
"#clicks": 0,
"#cost": 0,
},
{
"#adGroup": "Ad group 1 of Initial Campaign",
"#impressions": 0,
"#clicks": 0,
"#cost": 0,
}
]
}
}
I would imagine the object or model looking kind of like this.
There are different types of reports, but the structure look exactly the same, just the items are not containing the same attributes. The posted JSON structure represents an "AdGroup", meaning the "rows" would need to get de-serialized into different models. Basically calling "getRowItems" would need to return AdGroup-object's for the posted JSON, but may need to return other types of objects for different types of report data.
class ReportModelExample {
private $reportName;
private $reportDate;
private $reportItems;
public function getReportName()
{
return $this->reportName;
}
public function setReportName($reportName): void
{
$this->reportName = $reportName;
}
public function getReportDate()
{
return $this->reportDate;
}
public function setReportDate($reportDate): void
{
$this->reportDate = $reportDate;
}
public function getReportItems()
{
return $this->reportItems;
}
public function setReportItems($reportItems): void
{
$this->reportItems = $reportItems;
}
}
I have a laravel query builder result that looks like this
{
"data": [
{
"id": "",
"awardID": 2,
"title": "Dummy title",
"status": "active",
"raceStart":"",
"raceEnd:":""
}
]
}
What i want to output is something like this
{
"data": [
{
"id": "",
"awardID": 2,
"title": "Dummy title",
"status": "active",
"period": {
"raceStart":"",
"raceEnd:":""
}
}
]
}
This would have been much easier if the period was a table with a 1 to 1 relationship with parent table but this is not the case here.
How can this be achieved?
Check if this will work. I haven't tried though but according to documentation we can add accessor and mutators. But it will change every response you are doing with the model.
Using Eloquent
// Your Model
class Race extends Model
{
{...}
protected $appends = ['period'];
// accessor
public function getPeriodAttribute($value)
{
$this->attributes['period'] = (object)[];
$this->attributes['period']['raceStart'] = $this->attributes['raceStart'];
$this->attributes['period']['raceEnd'] = $this->attributes['raceEnd'];
unset($this->attributes['raceStart']); = $value;
unset($this->attributes['raceEnd']);
return $this->attributes['period'];
}
}
Now when you will access $race->period will give the raceStart and raceEnd value.
Ref: https://laravel.com/docs/5.4/eloquent-mutators#accessors-and-mutators
else another option is after query, do a map
{...}
->map(function($data) {
$data->period = (object)[];
$data->period['raceStart'] = $data->raceStart;
$data->period['raceEnd'] = $data->raceEnd;
unset($data->raceStart);
unset($data->raceEnd);
return $data;
});
Ref: https://laravel.com/docs/5.4/eloquent-collections#introduction
Using QueryBuilder
$races = DB::table('races')->get();
$races = array_map(function ($data) {
$data->period = (object)[
"raceStart" => $data->raceStart,
"raceEnd" => $data->raceEnd
];
unset($data->raceStart);
unset($data->raceEnd);
return $data;
}, $races->data);
I have the following json:
{
"Companies":[
{
"CompanyPersonTask":{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
},
"Company":{
"id":"502d1844-3638-44dc-824c-14f02e0cc009",
"name":"mostro",
"Office":[
{
"id":"502d1844-b90c-44f5-84c5-14f02e0cc009",
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"region":"Emilia-Romagna",
"city":"Rozzemilia",
"address":"-",
"phone":"-",
"legal_office":true
}
],
"CompanyPersonTask":[
{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
}
]
},
"Person":{
"id":"2",
"first_name":"Carlo",
"last_name":"Giusti",
"home_address":"Russi",
"job_address":null,
"phone":null,
"fax":null,
"mail":null,
"full_name":"Giusti Carlo",
"OfficePersonTask":[
],
"CompanyPersonTask":[
{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
}
]
},
"Task":{
"id":"1",
"short_name":"Proprietario",
"full_name":null,
"info":null,
"OfficePersonTask":[
],
"CompanyPersonTask":[
{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
}
]
}
}
]
}
That is created by this method, inside CompanyPersonTask model:
public function getCompaniesByRegion($region){
$this->recursive = 2;
return $this->find('all');
}
but I want my json to be formatted like this:
{
"Companies":[
{
"Company":{
"id":"502d1844-3638-44dc-824c-14f02e0cc009",
"name":"mostro",
"Office":[
{
"id":"502d1844-b90c-44f5-84c5-14f02e0cc009",
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"region":"Emilia-Romagna",
"city":"Rozzemilia",
"address":"-",
"phone":"-",
"legal_office":true
}
]
},
"Person":{
"id":"2",
"first_name":"Carlo",
"last_name":"Giusti",
"home_address":"Russi",
"job_address":null,
"phone":null,
"fax":null,
"mail":null,
"full_name":"Giusti Carlo"
}
}
]
}
How can I modify my method?
Use Containable behavior to filter result data.
For more explanation refer this link.
You can check unbindModel in cake php to remove the relation on the fly. This will prevent the data from the unwanted table. Check this.
<?php
$this->Model->unbindModel(
array('associationType' => array('associatedModelClassName'))
);