How to get Release Date from tmdb API on Laravel 5.4? - php

I am using Laravel PHP Framework, I'd like to use the tmdb package.
I can see release date in json. Here is screenshot
http://prntscr.com/f98ydy
But I want this in my view like I can get title with code:
{!! $movie->getTitle() !!}
How to get release date? I also tried with that code
{!! $movie->getReleaseDate() !!}
But showing me this error
Object of class DateTime could not be converted to string (View: C:\xampp\htdocs\hallofsound\resources\views\welcome.blade.php)
Here is my full Json output
ResultCollection {#1811 ▼
-page: 1
-totalPages: 36
-totalResults: 702
#data: array:20 [▼
"000000004255a5960000000012a0a3a3" => Movie {#1816 ▼
-adult: false
-backdropPath: "/aJn9XeesqsrSLKcHfHP4u5985hn.jpg"
-backdrop: BackdropImage {#1327 ▶}
-belongsToCollection: null
-budget: null
-genres: Genres {#1832 ▶}
-homepage: null
-id: 283995
-imdbId: null
-originalTitle: "Guardians of the Galaxy Vol. 2"
-originalLanguage: "en"
-overview: "The Guardians must fight to keep their newfound family together as they unravel the mysteries of Peter Quill's true parentage."
-popularity: 115.058584
-poster: PosterImage {#1837 ▶}
-posterPath: "/y4MBh0EjBlMuOzv9axM4qJlmhzz.jpg"
-productionCompanies: GenericCollection {#1331 ▶}
-productionCountries: GenericCollection {#1830 ▶}
-releaseDate: DateTime {#1840 ▶}
-revenue: null
-runtime: null
-spokenLanguages: GenericCollection {#1828 ▶}
-status: null
-tagline: null
-title: "Guardians of the Galaxy Vol. 2"
-voteAverage: 7.6
-voteCount: 1539
#alternativeTitles: GenericCollection {#1826 ▶}
#changes: GenericCollection {#1829 ▶}
#credits: CreditsCollection {#1821 ▶}
#images: Images {#1319 ▶}
#keywords: GenericCollection {#1824 ▶}
#lists: GenericCollection {#1820 ▶}
#releases: GenericCollection {#1316 ▶}
#release_dates: GenericCollection {#1822 ▶}
#similar: GenericCollection {#1819 ▶}
#translations: GenericCollection {#1825 ▶}
#reviews: null
#videos: Videos {#1831 ▶}
}
"000000004255a5a00000000012a0a3a3" => Movie {#1838 ▶}
"000000004255a5c90000000012a0a3a3" => Movie {#1863 ▶}
"000000004255a5ee0000000012a0a3a3" => Movie {#1888 ▶}
"000000004255a5f70000000012a0a3a3" => Movie {#1913 ▶}
"000000004255a51f0000000012a0a3a3" => Movie {#1937 ▶}
"000000004255a5240000000012a0a3a3" => Movie {#1962 ▶}
"000000004255a54d0000000012a0a3a3" => Movie {#1987 ▶}
"000000004255a5550000000012a0a3a3" => Movie {#2011 ▶}
"000000004255a57d0000000012a0a3a3" => Movie {#2035 ▶}
"000000004255aa840000000012a0a3a3" => Movie {#2058 ▶}
"000000004255aaac0000000012a0a3a3" => Movie {#2082 ▶}
"000000004255aab50000000012a0a3a3" => Movie {#2107 ▶}
"000000004255aadb0000000012a0a3a3" => Movie {#2133 ▶}
"000000004255aae00000000012a0a3a3" => Movie {#2158 ▶}
"000000004255aa080000000012a0a3a3" => Movie {#2182 ▶}
"000000004255aa110000000012a0a3a3" => Movie {#2207 ▶}
"000000004255aa360000000012a0a3a3" => Movie {#2232 ▶}
"000000004255aa5f0000000012a0a3a3" => Movie {#2257 ▶}
"000000004255aa640000000012a0a3a3" => Movie {#2282 ▶}
]
}

The JSON response is a collection. So you need to loop through it. See here
So maybe something like this:
foreach($movies as $movie)
{
var_dump($movie->getTitle());
}
Also the method getReleaseDate will return a DateTime object. To format it, you can probably do something like this:
$date = new DateTime($movie->getReleaseDate());
$result = $date->format('Y-m-d H:i:s');
or in Laravel, you can do this:
{!! $movie->getReleaseDate()->format('Y-m-d H:i:s') !!}

Related

Podio fields property is empty in Item object

This has just started happening with no changes to our implementation.
When I query (it seems any item) the API is no longer returning data in the fields property.
PodioItem {#354 ▼
-__attributes: array:32 [▼
"item_id" => 1234567890
"external_id" => null
"title" => "INV-1234567890-Deposit"
"link" => "[redacted]"
"rights" => array:8 [▶]
"created_on" => "2019-09-10 22:28:37"
"app_item_id_formatted" => "1310"
"app_item_id" => 1310
"created_by" => PodioByLine {#362 ▶}
"created_via" => PodioVia {#363 ▶}
"initial_revision" => PodioItemRevision {#364 ▶}
"current_revision" => PodioItemRevision {#367 ▶}
"fields" => PodioVia {#370 ▼
-__attributes: []
-__belongs_to: array:2 [▶]
-__properties: array:5 [▶]
-__relationships: []
#__id_column: null
}
"like_count" => null
"is_liked" => false
"ratings" => array:1 [▶]
"user_ratings" => array:1 [▶]
"last_event_on" => "2019-09-10 23:17:06"
"participants" => []
"tags" => []
"refs" => []
"linked_account_id" => null
"subscribed" => false
"invite" => []
"app" => PodioApp {#371 ▶}
"ref" => PodioReference {#372 ▶}
"reminder" => PodioReminder {#373 ▶}
"recurrence" => PodioRecurrence {#374 ▶}
"linked_account_data" => PodioLinkedAccountData {#375 ▶}
"comments" => PodioLinkedAccountData {#376 ▶}
"revisions" => PodioVia {#392 ▶}
"files" => PodioVia {#401 ▶}
]
-__belongs_to: null
-__properties: array:38 [▶]
-__relationships: array:15 [▶]
#__id_column: "item_id"
}
You can see __attributes are empty for fields. Again, for every item I have tested and for several apps, it is the same.
As I said nothing has changed in our implementation - last week it was working fine.
Any ideas?

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

How to get a value from a Array symfony2.8

Hi everyone i'm looking for a way to get a value in a variable but i'm stuck with an array like
$prixArrayArticle = $article->getArticlePlateforme()->getValues();
dump($prixArrayArticle);
gives me
ReassortController.php on line 1149:
array:1 [▼
0 => ArticlePlateforme {#1469 ▼
-article: Article {#1321 ▶}
-plateforme: Plateforme {#1051 ▶}
-codeProduitGv: "365717 "
-fournisseurCommun: FournisseurAchatsAdherents {#1445 ▶}
-referenceFournisseur: "100050 "
-minimumCommande: 1
-colisage: 12
-qtePalette: 12
-decolisable: true
-stock: 182
-dateCreation: DateTime {#1326 ▶}
-present: true
-articleSubstitution: null
-prix: PersistentCollection {#1430 ▼
-snapshot: array:1 [ …1]
-owner: ArticlePlateforme {#1469}
-association: array:19 [ …19]
-em: EntityManager {#137 …11}
-backRefFieldName: null
-typeClass: ClassMetadata {#1261 …}
-isDirty: false
#collection: ArrayCollection {#1439 ▼
-elements: array:1 [▼
0 => Prix {#1412 ▼
-id: 4175988
-dateDebut: DateTime {#1325 ▶}
-dateFin: DateTime {#1324 ▶}
-prixNet: "4.970"
-prixVenteConseille: null
-quantiteMinimum: null
-type: "permanent"
-numeroPromo: null
-offreComplementaire: false
-minimumCommande: 0
-colisage: 0
-invisible: null
-present: true
}
]
}
#initialized: true
}
-prixPromo: PersistentCollection {#1399 ▶}
}
]
and this
$article = $requeteArticle->getQuery()->getSingleResult();
give me this
ReassortController.php on line 1129:
Article {#769 ▼
-id: 1000940
-nom: "FILM DE PAILLAGE FRAISES FRESAFILM 1M40X10M"
-nomCourt: "FILM PAILLAGE FRAISES 1M40X10M"
-gamme: 5
-ecoContribution: "0.00"
-ecoMobilier: "0.00"
-quantiteAchat: "1.00"
-nombreUvParUa: "1.00"
-prixVenteConseille: "9.50"
-present: true
-tauxTva: TauxTva {#908 ▶}
-marque: null
-articleGencod: PersistentCollection {#1007 ▶}
-sousFamille: SousFamille {#635 ▶}
-articlePlateforme: PersistentCollection {#960 ▶}
-uniteAchat: UniteConditionnement {#833 ▶}
-uniteVente: UniteConditionnement {#833 ▶}
}
how can i get the name value for example in a variable because the array only have one index and i really don't know how to get a value with this array
Assuming that the properties are protected and that you have getters for them in your ArticlePlateforme and Article class, you should be able to get them like that:
$articleName = $prixArrayArticle[0]->getArticle()->getNom();
Otherwise, if the properties are public, you could simply get them like:
$articleName = $prixArrayArticle[0]->article->nom;

How to remove the same item from Collection laravel?

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

Laravel Query Builder Join results

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

Categories