How to get columns name from sql server view by Laravel - php

I am trying to get columns name from sql view, my view appear like this:
name type building_id
test type_test 5
test2 type2_test 6
I want to get names(name & type & building_id)
I tried Laravel function getColumnListing() but i get null array

Try this
\Illuminate\Support\Facades\DB::select('show columns from table_name');
// table_name must be db table name
Result
array:5 [▼
0 => {#309 ▼
+"Field": "id"
+"Type": "int(10) unsigned"
+"Null": "NO"
+"Key": "PRI"
+"Default": null
+"Extra": "auto_increment"
}
1 => {#311 ▼
+"Field": "name"
+"Type": "varchar(100)"
+"Null": "NO"
+"Key": ""
+"Default": null
+"Extra": ""
}
2 => {#312 ▶}
3 => {#313 ▶}
4 => {#314 ▶}
]

Related

Laravel select left join all columns in new array index

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

remove duplicates from a group of arrays

I have the following group of arrays in one array, where the content of them is a group of posts :
array:5 [▼
"in" => array:2 [▼
0 => {#371 ▼
+"id": 61
+"created_at": "2019-11-29 08:12:11"
+"updated_at": "2019-11-29 08:12:11"
+"title": "Sports test "
+"body": "Test answer"
+"ttype": 0
+"cat": 0
+"a_id": 61
+"tag": ""
}
1 => {#372 ▶}
]
"out" => []
"other" => []
"fol" => array:17 [▶]
"tag" => []
]
The arrays :
$Final_updts['in'] = $all_update_in;
$Final_updts['out'] = $all_update_out;
$Final_updts['other'] = $all_update_oth;
$Final_updts['fol'] = $follow_psts;
$Final_updts['tag'] = $post_tags;
There might be some posts repeated in more than one array. I tried to use array_unique($Final_updts);, but received Error exception: Array to string conversion
Is there any easy way to do this, or I have to modify the 5 queries to prevent any duplicate results ?
I assume that you want to get unique items by id. Lets use collection to solve this problem.
$results = collect($array)->collapse()->unique('id');
Pls let me know if I got it right.

Symfony Querybuilder left join merge arrays

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
}
...
]

Laravel illluminati collection remove duplicate item

I have a collection called $applied_exams which contain following data
Collection {#2033 ▼#items: array:4 [▼
0 => {#2035 ▼
+"post_id": 1
+"designation_id": 9
+"token_number": 438
+"name": "TECHNICIAN TELECOM"
+"adb": "2074-06-23"
+"ContainMultiplePost": false
+"Group_id": 8
}
1 => {#2034 ▼
+"post_id": 11
+"designation_id": 1
+"token_number": 740
+"name": "DEPUTY MANAGER TELECOM"
+"adb": "2074-06-25"
+"ContainMultiplePost": false
+"Group_id": 1
}
2 => {#2040 ▼
+"post_id": 3
+"designation_id": 12
+"token_number": 2810
+"name": "ASST. BUSINESS OFFICER"
+"adb": "2074-10-24"
+"ContainMultiplePost": true
+"Group_id": 7
}
3 => {#2039 ▼
+"post_id": 2
+"designation_id": 8
+"token_number": 2811
+"name": "ASST. ADMINISTRATIVE OFFICER"
+"adb": "2074-10-24"
+"ContainMultiplePost": true
+"Group_id": 7
}]}
I want to unset the data with duplicate group_id. I only want one data of one group_id. In this case I just want the $applied_exam[3] to be removed because group_id is same as $applied_exams[2]. Please help. Thanks
Use unique function
$unique_applied_exams = $applied_exams->unique('group_id');

Laravel 5 get attribute from collection

I have a $location collection that looks like this:
Collection {#225 ▼
#items: array:5 [▼
0 => GoogleAddress {#336 ▼
-id: "ChIJjWwHAP72w0cR44_HJ-bRcJE"
-locationType: "ROOFTOP"
-resultType: array:1 [▶]
-formattedAddress: ""
-streetAddress: null
-intersection: null
-political: ""
-colloquialArea: null
-ward: null
-neighborhood: null
-premise: null
-subpremise: null
-naturalFeature: null
-airport: null
-park: null
-pointOfInterest: null
-establishment: null
-subLocalityLevels: AdminLevelCollection {#339 ▶}
-coordinates: Coordinates {#331 ▶}
-bounds: Bounds {#332 ▶}
-streetNumber: "21"
-streetName: ""
-subLocality: null
-locality: ""
-postalCode: ""
-adminLevels: AdminLevelCollection {#337 ▶}
-country: Country {#335 ▶}
-timezone: null
-providedBy: "google_maps"
}
1 => GoogleAddress {#344 ▶}
2 => GoogleAddress {#352 ▶}
3 => GoogleAddress {#360 ▶}
4 => GoogleAddress {#368 ▶}
]
}
So I am trying to get the formattedAddress like this:
$location[0]->formattedAddress
But I get the following error:
Cannot access private property
Geocoder\Provider\GoogleMaps\Model\GoogleAddress::$formattedAddress
Anyone can help me out here?
It's a collection Do it like this
$location->first()->formattedAddress
The variable is private, meaning you can only access it from within the same class. In this case, the class has a getter method called getFormattedAddress() you can use.
Source: https://github.com/geocoder-php/Geocoder/blob/master/src/Provider/GoogleMaps/Model/GoogleAddress.php#L182
Solution: $location->first()->getFormattedAddress()
And a bit of advice, if you don't understand the private part, you really should have a look at the PHP documentation on visibility: http://php.net/manual/en/language.oop5.visibility.php
First, try to get the first object with $model->first()
If not, try to use mutators:(Here an example)
public function getNameAttribute()
{
return $this->attributes['firstName'];
}

Categories