Laravel Model created_at inconsistent display - php

I am new to laravel and php,
I have the code below, REST API
public function sendMessage(Request $request)
{
$message = Message::create($request->all());
return response()->json(['result'=>'success','created_at'=>$message->created_at],200);
}
This is displayed like this in the postman
{
"result": "success",
"created_at": "2020-06-29T23:31:32.000000Z"
}
If I change the return message-object by changing return statement to
return response()->json(['result'=>'success','created_at'=>$message],200);
Then time format is displayed differently as below
{
"result": "success",
"created_at": {
"sender": "47",
"receiver": "23",
"message": "hello world reply",
"updated_at": "2020-06-29 23:38:53",
"created_at": "2020-06-29 23:38:53",
"id": 515
}
}
I do not want this form "2020-06-29T23:31:32.000000Z" when I access it as a property,not sure what is this 00000Z at then end. want it like this "2020-06-29 23:38:53" Any help

When you access the data member ->created_at you get a Carbon instance, instead if you serialize the object, you get its attribute.
This is why you are getting two different serialization, because you are serializing two different things (one is a Carbon instance, the onthe one is a Model)

If you convert the Carbon instance to a string it will be in the same format as you see in the serialized model. When you pass it without doing this it is going to json_encode the value which will return it how you are currently seeing it.
return response()->json([
'result' => 'success',
'created_at' => (string) $message->created_at
],200);

Related

Get whole object data instead id in Laravel

I have an object with this structure, that I need to get name of source_id in my blade file,
When I try to access that by the way
$data['source_id']['name']
$data->source_id->name
$data->{'source_id'}->{'name'}
I got this error
Trying to get property 'name' of non-object
I just try this $data->source_id, but it return its ID, instead the object,
any suggestion?
{
"id": 4,
"type": "s1",
"source_id": {
"id": 1,
"code": "۱",
"name": "تیل پطرول",
"manager": "نجیب",
"phone": "۰۷۷۲۴۳۴۳۲۱",
"address": "دهمزنگ",
"capacity": "0.00",
"oum_id": 1,
"created_at": "2021-03-02T15:55:20.000000Z",
"updated_at": "2021-03-02T15:55:20.000000Z"
},
"source_type": "STRG",
}
Here is the function to get data
public function loadSale($id){
$base = Sale::findOrFail($id);
if ($base->type == "s1") {
$sale = Sale::with(['saleS1.project.pro_data', 'source_id'])->where('id', $id)->first();
$sale['sales'] = $sale->saleS1;
}
return $sale
}
I could understand your problem. But it is an object not an array. You have to use a loop to access the object. Create a for loop and and inside which you can access the particular field of the object. Hope this helpful
As #MAY mentioned in the comment
I guess the name of your relation and foreign key field is same, that's why you get id when you do this $data->source_id
So I modify the relation and define the source, now I can't access the data simply as before $data->source->name

Codeigniter - filtering json data by user request

So, I have table ($data) with JSON records getting by select query.
for example:
"data": {
"id": "1",
"technology_name": "First",
"technology_info": "Something about first rec",
"created_at": null,
"updated_at": null
},
{
"id": "2",
"technology_name": "Second",
"technology_info": "Something about second rec",
"created_at": null,
"updated_at": null
}
}
...
Now I need to create filter by column (technology_name) by user request with 'like', something like
select * from "data" where technology_name = '%css%';
It is possible to do this with CodeIgniter/PHP code and JSON file, or is better way to do that? Can you give me some advice how to figure it out?
I think that I misspelled my problem.
I was able to find the correct solution.
So, I modified my script that return response as JSON representation with some instructions that SELECT data with WHERE by getting user request params.

Laravel Collection Returns Inconsistant Results

I have a collection that will switch between responding as an array or as an object seemingly at random. What would cause that to happen?
$events = Event::all();
$events = $events->map(function ($event) use ($request) {
$reducedEventName = Helper::alphaNum($event->name);
$reducedRequestName = Helper::alphaNum($request->name);
$distance = levenshtein($reducedEventName, $reducedRequestName);
return [
'name' => $event->name,
'url' => route('event.view', ['slug' => $event->slug]),
'distance' => $distance,
];
})
->filter(function ($event) {
return $event['distance'] <= Helper::threshold($event['name']);
})
->take(3)
->sortBy('distance');
return $events->toArray();
This method is called via XHR, so I want the raw JSON response. Sometimes it looks like this (👍):
[{
"name": "Taylor Swift - Reputation - Release",
"url": "http:\/\/localhost\/e\/lgKejoPSg",
"distance": 22
}, {
"name": "Wiz Khalifa \"Laugh now, fly later\"",
"url": "http:\/\/localhost\/e\/DdLnFD3Qf",
"distance": 24
}]
And sometimes it looks like this (👎):
{
"1": {
"name": "Wiz Khalifa \"Laugh now, fly later\"",
"url": "http:\/\/localhost\/e\/DdLnFD3Qf",
"distance": 18
},
"0": {
"name": "Taylor Swift - Reputation - Release",
"url": "http:\/\/localhost\/e\/lgKejoPSg",
"distance": 23
}
}
Laravel 5.4
As to your question, I am not sure what could cause that to happen as the sortBy() method states it returns a collection. Do you return that exact $events as is to your blade? If so that could be inconsistency on the browser deciding how to handle the collection that is sent back to it. You could always use ->toArray() at the end of your query to ensure that it always returns an array, or ->toJson() if you don't want to do anything else with it.
Resorting the results tries to preserve the original keys, so I needed to discard the original keys:
return array_values($events->toArray());

Laravel: JSON and pivot table

Sorry about the non-explanatory title but I could not come up with a descriptive one.
I've got the following 3 tables:
- games
- platforms
- games_platforms
And I've got 2 Models in Laravel for both Platform and Game.
public function games()
{
return $this->belongsToMany('App\Game', 'games_platforms')->withPivot('release_date');
}
public function platforms()
{
return $this->belongsToMany('App\Platform', 'games_platforms')->withPivot('release_date');
}
Now this works like a charm, I get a JSON string with all the information in the 3 tables, like this.
[{
"id": 1,
"name": "Borderlands",
"short_description": "",
"created_at": null,
"updated_at": null,
"platforms": [{
"id": 4,
"name": "PC",
"pivot": {
"game_id": 1,
"platform_id": 4,
"release_date": "2016-03-03"
}
}]
}]
Now my problem is as follows. I don't want to show the whole 'pivot' information, just the 'release_date', like this:
"platforms": [{
"id": 4,
"name": "PC",
"release_date": "2016-03-03"
Is there an easy way in Laravel to do such a thing? As far as I can see right now, looking at other posts, is to either write a function that turns the json into an array and I can arrange it then. Or I can write my own query instead of letting Laravel do all that.
Hope you guys can help me with this issue. Thanks!
I would modify the data returned from the query via methods on the collection class:
//replace Game::all() with your actual query
return Game::all()->each(function($game){
$game->platforms->map(function($platform){
$platform->release_date = $platform->pivot->release_date;
unset($platform->pivot);
return $platform;
});
});
I know this is already answered but I believe the proper answer would be to add whatever you want to hide to your hidden attribute on the model.
<?php
class Games extends Eloquent
{
protected $hidden = ['pivot.game_id', 'pivot.platform_id'];
}
I am not sure what your keys are becuase they are different in your two examples.
Please see: https://github.com/laravel/framework/issues/745
A better way is to use Laravel resources,
First create Resource (php artisan make:resource)
Rresource GameResource extends Resource
{
public function toArray($request)
{
return [
'release_date' => $this->pivot->release_date,
'name' => $this->name,
/// All other fields or you can merge both here
];
}
}
Now use this resource;
$data = GameResource::collection(Game::all());

JMSSerializerBundle and Symfony 2 - Output Doctrine/ODM to JSON file

I am trying to retrieve all records and display them in a JSON file.
My current function retrieves all Events that belong to a specific user.
/**
* create json files from doctrine/mongo
* #Route("/createjson", name="createjson")
*/
public function createJson()
{
// check user authentication
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$dm = $this->get('doctrine_mongodb')->getManager();
$repository = $dm->getRepository('AppBundle:Event');
$events = $repository->findBy(array('user' => $this->getUser()));
$serializer = SerializerBuilder::create()->build();
$result = $serializer->deserialize($events, 'AppBundle\Document\Event', 'json');
var_dump($result);
exit;
}
This is not working because some of the elements passed into the serializer are of an array format. Error I am getting.
Warning: json_decode() expects parameter 1 to be string, array given
500 Internal Server Error - ContextErrorException
However if I use the inbuilt Symfony Serializer it works fine:
$serializer = $this->container->get('serializer');
$reports = $serializer->serialize($events, 'json');
return new Response($reports);
However the JSON to be produced will be different to my Document/Entity hence why I want/need to use the JMSSerializerBundle.
For example, a record looks like this:
[{
"id": "572041b3288b560e5e00451c",
"name": "Test",
"date": "2016-04-27T05:25:00+1000",
"enddate": "2016-04-30T11:55:00+1000",
"location": {
"name": "Sydney, NSW"
},
"key": {
"id": "1g43g34g34g23f32g32G32gGSDF"
},
"user": {
"id": "57203174288b560e5e0044da"
}, ...
}]
But I only want to display (output) to JSON
[{
"id": "572041b3288b560e5e00451c",
"name": "Test",
"date": "2016-04-27T05:25:00+1000",
"location": "Sydney, NSW"
}]
How would I go about doing this? There is not much documentation on JMSSerializerBundle online.
Edit: I should mention that the database collection I am querying has a relation to the User collection which is managed by FOSUserBundle. I'm not sure if this has any relation to my problem however
You should look at the documentation of the bundle, may be you will find more information
http://jmsyst.com/bundles/JMSSerializerBundle
http://jmsyst.com/libs/serializer/master/usage
You seems to use the wrong function of the serializer. In your case, you seems to need to get a json from your user object, so you need to use
$serializer = SerializerBuilder::create()->build();
$result = $serializer->serialize($events, 'AppBundle\Document\Event', 'json');
serialize($object):string : get a string from an object
deserialize($string):object : get an object from a representation of an object (json, xml...).

Categories