Get attribute from laravel's "with" closure - php

I have something like that:
$order = Order::with('user')->where('id', $id)->first();
It returns:
{
"id": 1,
"price": null,
"user": {
"id": 1,
"name": "jon"
}
}
Now inside my laravel code I want to get id of user. How is that possible?
because $order->user returns just null

Since above value returned by Order::with('user')->where('id', $id)->first(); is a collection so try chaining the result with pluck like below
Order::with('user')->where('id', $id)->first()->pluck(user.id);
Let me know if this works for you as I could not replicate it on my machine.

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.

How to select a row in laravel using title not the Id line ::find(); method

I trying to find a row using the title, I need just one record
I need a query like $art = Artists::find($id); but I don't want to use Id I want to find that by title
public function getArtist($slug){
//$art = Artists::where('slug', $slug)->take(1)->get();
$art = Artists::find(1);
return $art;
}
I need to show the result like this:
{
"id": 1,
"title": "yaghoub-emdadian",
"brithday": null,
"thumbnail": "storage/upload/artists/cover/4_1578739156.jpg",
"created_at": "2020-01-11 10:13:46",
"updated_at": "2020-01-11 10:39:18"
}
For this you could use where() and first():
Artist::where('title', $title)->first();

Search an array nested in a laravel collection

I have a api call that returns a set of data something like this:
{
"id": 97423,
"visitor_id": 231505,
"domain_sessionidx": 1,
"session_start": "2017-06-01 04:40:07",
"session_end": "2017-06-01 05:22:45",
"session_length": 2558,
"count_pages": 11,
"count_pings": 7,
"created_at": null,
"updated_at": "2017-06-12 18:59:51",
"pages": [
1829,
1811,
1829,
1501,
1829,
1889,
1762,
1686,
1825,
1825,
1825
] },....
I have put this into a variable and made a collection out of it:
$new_var = collect($result);
I am having a problem because I would like to access only the records in the data where pages contains the id 1762. I have been trying with:
$new_var->whereIn('pages',[1762])->pluck('pages')
but I am always getting an empty result. Any help is greatly appreciated.
I feel like you need to filter your $new_var collection and return true if it's pages attribute contains the page you are looking for. For example:
$page = 1762;
$inPages = $new_var->filter(function($collection) use($page){
return in_array($page, $collection->pages);
});
$inPages should now be a subset of $new_var with entries that contain 1762 in their pages array, otherwise it will be an empty Collection. Check here for more information: https://laravel.com/docs/5.4/collections

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());

Categories