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());
Related
I am using Laravel (PHP) and MySQL for my backend. I am creating methods for setting and getting information from the database. Those information are being send as a json to the frontend.
I can send table information like:
[
{
"id": 4,
"name": "Max"
},
{
"id": 5,
"name": "Eric"
}
]
For this I am using laravel methods like: DB::table('user')->select('user.id', 'user.name')->get();
However my friend who is doing the frontend want the following json code:
[
{
"id": 4,
"name": "Max"
"specific_user_price":{
"S":5.00,
"M":6.00,
"XL":8.00
}
},
{
"id": 5,
"name": "Eric"
"specific_user_price":{
"S":5.50,
"M":10.00,
"XL":15.00
}
}
]
"specific_user_price is a table and "S", "M", "XL" are columns which have different values depending on the user. I do not know how I can create specific_user_price as an array in a query. I can use group_concat but he needs the json like displayed above.
My idea was to create additional columns in user "size S price", "size M price" and "size XL price". However my friend want those values as an own array group, because some users only habe access to one size, so he would get null values.
Any ideas which method in PHP or Laravel I can use for that? Or is there a MySQL method for creating such thing in a query?
Firstly use Models, way easier to work with out of the box. Define your User model like this, with a relationship for the price.
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function specificUserPrice() {
return $this->hasOne(UserPrice::class);
}
}
You also need to have the UserPrice model defined.
use Illuminate\Database\Eloquent\Model;
class SpecificUserPrice extends Model
{
}
Laravel automatically transforms models, you can get away with the following code in the controller.
public function index()
{
return User::with('specificUserPrice')->get();
}
DB::table('user')->select(['id', 4],['name','max'])->get();
DB::table('user')[0]->get()
This gets you the first element in the array
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
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);
I have this json value that I want to be sorty but for some reason it's not working.
[
{
"id": 15028,
"order_id": 342,
"user_id": 3,
"status": "1",
"priority": "1",
"donedate": null,
"user": {
"id": 3,
"name": "Max"
}
},
{
"id": 15030,
"order_id": 341,
"user_id": 4,
"status": "2",
"priority": "1",
"donedate": null,
"user": {
"id": 4,
"name": "Jon"
}
}
]
This jSon structure is the result of Laravel eloquent object conversion using $object->toJson();
Now I keep this output in my Redis cache. What I want is to when the status and or priority of any order gets changed then I want to sort this jSon and store it back in Redis.
$order_list = collect($json_decoded_with_updated_values);
$order_list = $order_list->sortBy('status')->sortBy('priority');
Redis::set(\GuzzleHttp\json_encode($stich_list_in_collection));
Redis::set("orders_list", $orders_list, 302400);
However, I don't get a sort list. What I want to achieve is that, just like I would run two to three orderBy on an eloquent model like orderBy('status')->orderBy('priority')->get() .. I want to run the same two sortings on this json list.
Thanks in advance.
I figured it out. Actually we don't need to have a call-back as suggested by #brokedid. We can do it like following.
$order_list->sortBy('status')->sortBy('priority')->values()->all();
So I was missing the "->values()->all()" part. I hope if any one runs into the same problem in future, they can get a hint from this.
If you want to sort by multiple Fields, then you could try to sort with a callback-method:
$orderedList = $unorderedList->sortBy(function($item) {
return $item->priority.'-'.$item->status;
});
I wonder what's the result when you choose a different sort direction.
$order_list = $order_list->sortByDesc('status');
I am just confused on how this thing is working .
I have a M-M relationship between by Users and Roles. If I retrieve my user like the following :-
$user = Auth::User();//->with('roles')->get();
$roleName = $user->roles[0]->name;
return $this->sendResponse('User retrieved successfully',$user);
I get the following response :
{
"success": true,
"message": "User retrieved successfully",
"data": {
"id": 2,
"name": "dummy",
"email": "dummy#dummy.com",
"created_at": "2017-05-06 09:49:50",
"updated_at": "2017-05-06 09:49:50",
"tenant_id": 2,
"roles": [
{
"id": 1,
"created_at": "2017-05-06 06:26:55",
"updated_at": "2017-05-06 06:26:55",
"name": "Admin",
"permissions": null,
"pivot": {
"user_id": 2,
"role_id": 1
}
}
]
}
}
But, if I retrieve my user as :-
$user = Auth::User();//->with('roles')->get();
return $this->sendResponse('User retrieved successfully',$user);
I get the following resut :-
{
"success": true,
"message": "User retrieved successfully",
"data": {
"id": 2,
"name": "Ali",
"email": "ali#and-or.com",
"created_at": "2017-05-06 09:49:50",
"updated_at": "2017-05-06 09:49:50",
"tenant_id": 2
}
}
Why is this happening ? I expected the "first" posted result to the latter query.
Secondly, I did not modify the $user after the query in the first "method" how did it get its Roles attachment ?
I am sure there is an explanation, but I couldn't put my finger on it.
When retrieving a model, the User in this instance, relationships are not automatically also retrieved (Since on the database side this would require a second query while you might not even need the roles in a certain situation).
In your first example, by accessing the roles through $roleName = $user->roles[0]->name;, Laravel does the roles() query automatically, and also adds the roles object to the User (So it can be accessed again at a later point without needing to redo the query). This explains why the roles are 'magically' attached to your User model in the first example.
In your second example this query is not done automatically, so you do not get the roles relation in your response.
If you want to have access to the Users roles, then you could use the with() method like in your comment to eager load the relationship, but keep in mind that this implies doing the second query in order to get this data from the database.
Another option, if you always want the User model to have its Roles attached, would be to add roles to the $appends array of the model:
protected $appends = ['roles'];
This tells Laravel that the roles attribute is one which you always want available on your model, and it then does what is necessary to make this happen (In this case, query the relationship).