i have one question in laravel query and search that in google and can't find that
This is my query :
$data = DB::table('event')
->where('TU', '!=', null)
->where('TM', '!=', 180)
->leftJoin('market', 'event.event_id', '=', 'market.event_id')
->select([
'event.event_id as id',
'TU',
DB::raw('(TM*60+TS) as elapsed'),
'markets' => 'market.*'
])
->orderBy('event.time', 'ASC')
->get();
and my result is this :
Collection {#380 ▼
#items: array:3 [▼
0 => {#381 ▼
+"id": 629
+"TU": 0
+"elapsed": 1200
+"default": 1
+"group_name": "other"
+"api_updated_at": null
+"updated_at": "2020-01-31 11:16:11"
}
1 => {#382 ▶}
2 => {#385 ▶}
]
}
i want all columns from market table pushed in one index in result
like that :
Collection {#380 ▼
#items: array:3 [▼
0 => {#381 ▼
+"id": 629
+"TU": 0
+"elapsed": 1200
+"markets": array:3 [▼
0 => {#382 ▼
+"default": 1
+"group_name": "other"
+"api_updated_at": null
+"updated_at": "2020-01-31 11:16:11"
}
1 => {#383 ▶}
2 => {#384 ▶}
]
}
]
}
please help me for edit query in laravel
you can not use a field instead of many fields of one table in select. you can try use
->select([
'event.event_id as id',
'TU',
DB::raw('(TM*60+TS) as elapsed'),
'market.*'
])
or use with('market') instead of leftjoin
Related
I am using a query to sort some of my appointments from the appointment date,
this is my code
$appointments = $appointments
->where(DB::raw( "to_char((appointments.response::json->>'datetime')::timestamp, 'MM/DD/YYYY HH:MM:SS')" ), '>=', $start_date_format)
->where(DB::raw( "to_char((appointments.response::json->>'datetime')::timestamp, 'MM/DD/YYYY HH:MM:SS')" ), '<=', $end_date_format);
When I debug
dd($appointments->select(DB::raw( "to_char((appointments.response::json->>'datetime')::timestamp, 'MM/DD/YYYY HH:MM:SS')"))->get());
t\Collection {#1800 ▼
#items: array:22 [▼
0 => {#1829 ▼
+"to_char": "01/20/2021 11:01:00"
}
1 => {#1831 ▼
+"to_char": "01/22/2021 02:01:00"
}
2 => {#1828 ▼
+"to_char": "01/19/2021 02:01:00"
}
3 => {#1835 ▼
+"to_char": "01/21/2021 02:01:00"
}
4 => {#1830 ▼
+"to_char": "01/05/2021 01:01:00"
}
5 => {#1832 ▼
+"to_char": null
}
6 => {#1833 ▼
+"to_char": "03/28/2021 11:03:00"
}
]
when i debug dd($start_date_format); - format would be like these
"12/31/2020 00:00:00"
when i sort my date it works , but if i change the year it wont work , is there anything i had done wrong
I am currently facing a problem using the query builder and a left join. What I want to achieve is select all columns from the Article-Entity and, using a left join with two conditions, "append" a field to that array. In the snippet you'll see the working query without a join and the query with the left join and their results.
I know you can use "getScalarResult", but that produces the result shown in the snippet as well - it prefixes all columns from table "Article". What I would want is that result without the "a_" prefixes.
I know you can list the table fields in the select statement, but I do not want that query to be static but rather have it dynamic for when the table structure might change.
// default query
return $this->createQueryBuilder('a')
->andWhere('a.publishedAt IS NOT NULL')
->andWhere('a.publishedAt <= CURRENT_TIMESTAMP()')
->andWhere('a.status = 2')
->orderBy('a.publishedAt', 'DESC')
->getQuery()
->getResult();
// default result
array:5 [
0 => Article {#723
-id: 18
-title: "Title!"
-slug: "slug"
-content: "content"
-image: "img.jpg"
-author: BackendUser {#817 ▶}
-status: 2
-publishedAt: DateTime #1536546264 {#720 ▶}
-ratingPositive: 1
-ratingNegative: 0
-articleRatings: PersistentCollection {#816 ▶}
#createdAt: DateTime #1539163998 {#715 ▶}
#updatedAt: DateTime #1539180102 {#709 ▶}
}
...
]
// Left-Join-Query:
return $this->createQueryBuilder('a')
->andWhere('a.publishedAt IS NOT NULL')
->andWhere('a.publishedAt <= CURRENT_TIMESTAMP()')
->andWhere('a.status = 2')
->leftJoin('App\Entity\Article\ArticleRating', 'ar', 'WITH', 'ar.article = a.id AND ar.user = ' . $userId)
->addSelect('ar.rating as userRating')
->orderBy('a.publishedAt', 'DESC')
->getQuery()
->getResult();
// Left Join Result
array:5 [
0 => array:2 [
0 => Article {#722 ▼
-id: 18
-title: "Title"
-slug: "Slug"
-content: "Content"
-image: "image.jpg"
-author: BackendUser {#817 ▶}
-status: 2
-publishedAt: DateTime #1536546264 {#689 ▶}
-ratingPositive: 1
-ratingNegative: 0
-articleRatings: PersistentCollection {#816 ▶}
#createdAt: DateTime #1539163998 {#737 ▶}
#updatedAt: DateTime #1539180102 {#732 ▶}
}
"userRating" => true
]
...
]
// getScalarResult - result
array:5 [▼
0 => array:12 [▼
"a_id" => 18
"a_title" => "Title"
"a_slug" => "slug"
"a_content" => "content"
"a_image" => "image.jpg"
"a_status" => 2
"a_publishedAt" => DateTime #1536546264 {#689 ▶}
"a_ratingPositive" => 1
"a_ratingNegative" => 0
"a_createdAt" => DateTime #1539163998 {#737 ▶}
"a_updatedAt" => DateTime #1539180102 {#732 ▶}
"userRating" => "1"
]
...
]
My desired outcome would look like something like this:
array:5 [
0 => Article {#722 ▼
-id: 18
-title: "Title"
-slug: "Slug"
-content: "Content"
-image: "image.jpg"
-author: BackendUser {#817 ▶}
-status: 2
-publishedAt: DateTime #1536546264 {#689 ▶}
-ratingPositive: 1
-ratingNegative: 0
-articleRatings: PersistentCollection {#816 ▶}
#createdAt: DateTime #1539163998 {#737 ▶}
#updatedAt: DateTime #1539180102 {#732 ▶}
-userRating- => true
}
...
]
{`
+"insights": array:1 [▼
0 => {#209 ▼
+"group": "Provision"
+"dataset": array:1 [▼
0 => {#207 ▼
+"group": "Provision"
+"set": array:3 [▼
0 => {#194 ▼
+"name": "Neutral"
+"value": 917
}
1 => {#203 ▶}
2 => {#197 ▶}
]
}
]
}
]
+"errorCode": 0
}`
How to get the name property inside the set array? I've tried multiple way but it kept error return trying to get property non-object.
suppose you provide $response to your blade view
$response = {
+"insights": array:1 [▼
0 => {#209 ▼
+"group": "Provision"
+"dataset": array:1 [▼
0 => {#207 ▼
+"group": "Provision"
+"set": array:3 [▼
0 => {#194 ▼
+"name": "Neutral"
+"value": 917
}
1 => {#203 ▶}
2 => {#197 ▶}
]
}
]
}
]
+"errorCode": 0
}
You have to loop through to response , in your blade view :
#foreach($response->insights as $insight)
#foreach($insight['dataset'] as $dataset)
#foreach($dataset['set'] as $set)
<tr><td>$set['name']</td></tr>
#endforeach
#endforeach
#endforeach
data_get($data, 'insights.0.dataset.0.set.0.name');
end if you have json - convert it into array -> json_decode(string);
You have to simply loop through it and do whatever you want to with that name,
foreach($response->insights as $temp){
foreach($temp->dataset as $var){
foreach($var as $obj){
$name = $obj->name;
}
}
}
I have this collection:
Collection {#604 ▼
#items: array:4 [▼
0 => CarsMark {#596 ▶}
1 => CarsMark {#594 ▶}
2 => CarsMark {#594 ▶}
3 => CarsMark {#595 ▶}
]
}
As you can see there is two the same items (594). How can i retrieve all items from with collection without the same items?
Use the collection's unique method:
$unique = $collection->unique();
I have a join in laravel 5.1 and it works fine, however I am getting back an array with each result of my join. Is there a way I can get the results back with the join results a nested array inside of my main select query, or would this have to be something I do in PHP after getting the results back?
$single = $this->db->table($this->table)
->leftJoin("ootd_clothing", "ootd_clothing.ootd_id", "=" ,"$this->table.id")
->leftJoin("users", "users.id", "=", "$this->table.user_id")
->where("$this->table.slug", "=", $slug)
->select("$this->table.name AS ootd_name", "$this->table.image_path", "$this->table.slug", "$this->table.description", "users.name AS user_name", "ootd_clothing.item", "ootd_clothing.brand")
->get();
Result I'm getting:
array:5 [▼
0 => {#158 ▶}
1 => {#159 ▶}
2 => {#160 ▶}
3 => {#161 ▶}
4 => {#162 ▶}
]
Result I would like:
array:1 [
'id': 1,
'joinResults':array:5 [▼
0 => {#158 ▶}
1 => {#159 ▶}
2 => {#160 ▶}
3 => {#161 ▶}
4 => {#162 ▶}
]
]