Solved

Laravel: Passing a sub-element of an object from one controller method to another

Posted on 2016-08-10
3
53 Views
Last Modified: 2016-10-03
I am working on an app that uses Google Books API.  The call to the API returns an object that contains arrays for each book which also includes other data (subtitle, authors, descriptions, etc.).  I am able to search for a list of books and display them using the following function in my controller:
 public function newBookSearch(Google $google)
    {
        if(Input::has('bookSearch')){
            $bookSearch = Input::get('bookSearch');
            $results = $google->getBooks($bookSearch);
            return view('bookshelves.search_results',['results' => $results]);
        }else{
            return redirect()->back();
        }        
    }

Open in new window

The list is displayed using a loop in the blade template.  What I want to do is select one book from the list object, pass it to another function in the controller...showBookDetail()..., and display the details from the element in a new view.  While this strikes me as a simple and obvious application of a object list, I can't seem to figure out how to do it.  How do I grab a sub-element (a single book) of this list and display it in a new view without sending another request to the Google API?

Here is a sample of the list object:
Google_Service_Books_Volumes Object
(
    [collection_key:protected] => items
    [internal_gapi_mappings:protected] => Array
        (
        )

    [itemsType:protected] => Google_Service_Books_Volume
    [itemsDataType:protected] => array
    [kind] => books#volumes
    [totalItems] => 447
    [modelData:protected] => Array
        (
            [items] => Array
                (
                    [0] => Array
                        (
                            [kind] => books#volume
                            [id] => DtqaBwAAQBAJ
                            [etag] => Qkbs3HDKZsA
                            [selfLink] => https://www.googleapis.com/books/v1/volumes/DtqaBwAAQBAJ
                            [volumeInfo] => Array
                                (
                                    [title] => American Sniper
                                    [authors] => Array
                                        (
                                            [0] => Chris Kyle
                                        )

                                    [publisher] => Best Seller
                                    [publishedDate] => 2013-09-25
                                    [description] => SOON TO BE A MAJOR MOTION PICTURE DIRECTED BY CLINT EASTWOOD A celebration of the remarkable life and legacy of fallen American hero Chris Kyle This commemorative memorial edition of Kyle's bestselling memoir features the full text of American Sniper, plus more than eighty pages of remembrances by those whose lives he touched personally—including his wife, Taya; his parents, brother, and children; Marcus Luttrell and other fellow Navy SEALs; veterans and wounded warriors; lifelong friends; and many others. He was the top American sniper of all time, called "the legend" by his Navy SEAL brothers, and a hero by those he served on the home front . . . From 1999 to 2009, U.S. Navy SEAL Chris Kyle recorded the most career sniper kills in United States military history. Kyle earned legendary status among his fellow SEALs, Marines, and U.S. Army soldiers, whom he protected with deadly accuracy from rooftops and stealth positions. Gripping and...
                                    [readingModes] => Array
                                        (
                                            [text] => 1
                                            [image] => 
                                        )

                                    [printType] => BOOK
                                    [averageRating] => 3.5
                                    [ratingsCount] => 26
                                    [maturityRating] => NOT_MATURE
                                    [allowAnonLogging] => 
                                    [contentVersion] => 1.3.2.0.preview.2
                                    [imageLinks] => Array
                                        (
                                            [smallThumbnail] => http://books.google.com/books/content?id=DtqaBwAAQBAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api
                                            [thumbnail] => http://books.google.com/books/content?id=DtqaBwAAQBAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api
                                        )

                                    [language] => en
                                    [previewLink] => http://books.google.com/books?id=DtqaBwAAQBAJ&dq=American+Sniper&hl=&cd=1&source=gbs_api
                                    [infoLink] => http://books.google.com/books?id=DtqaBwAAQBAJ&dq=American+Sniper&hl=&source=gbs_api
                                    [canonicalVolumeLink] => http://books.google.com/books/about/American_Sniper.html?hl=&id=DtqaBwAAQBAJ
                                )

                            [saleInfo] => Array
                                (
                                    [country] => US
                                    [saleability] => NOT_FOR_SALE
                                    [isEbook] => 
                                )

                            [accessInfo] => Array
                                (
                                    [country] => US
                                    [viewability] => NO_PAGES
                                    [embeddable] => 
                                    [publicDomain] => 
                                    [textToSpeechPermission] => ALLOWED
                                    [epub] => Array
                                        (
                                            [isAvailable] => 1
                                        )

                                    [pdf] => Array
                                        (
                                            [isAvailable] => 1
                                        )

                                    [webReaderLink] => http://books.google.com/books/reader?id=DtqaBwAAQBAJ&hl=&printsec=frontcover&output=reader&source=gbs_api
                                    [accessViewStatus] => NONE
                                    [quoteSharingAllowed] => 
                                )

                        )

                    [1] => Array
                        (
                            [kind] => books#volume
                            [id] => 1qWEMxNe2mAC
                            [etag] => lFVwp77gljA
                            [selfLink] => https://www.googleapis.com/books/v1/volumes/1qWEMxNe2mAC
                            [volumeInfo] => Array
                                (
                                    [title] => American Sniper (Enhanced Edition)
                                    [subtitle] => The Autobiography of the Most Lethal Sniper in U.S. Military History
                                    [authors] => Array
                                        (
                                            [0] => Chris Kyle
                                            [1] => Scott McEwen
                                            [2] => Jim DeFelice
                                        )

                                    [publisher] => Harper Collins
                                    [publishedDate] => 2012-01-03
                                    [description] => NOW A BLOCKBUSTER MOTION PICTURE DIRECTED BY CLINT EASTWOOD—NOMINATED FOR SIX ACADEMY AWARDS, INCLUDING BEST PICTURE This special enhanced edition features more than 10 exclusive videos with Chris Kyle and an additional 15 images and descriptions of the weapons used by the armed forces in their fight against terrorism overseas. From 1999 to 2009, U.S. Navy Seal Chris Kyle recorded the most career sniper kills in United States military history. His fellow American warriors, whom he protected with deadly precision from rooftops and stealth positions during the Iraq War, called him “The Legend”; meanwhile, the enemy feared him so much they named him al-Shaitan (“the devil”) and placed a bounty on his head. Kyle, who was tragically killed in 2013, writes honestly about the pain of war—including the deaths of two close SEAL teammates—and in moving first-person passages throughout, his wife, Taya, speaks openly about the strains of war on their family, as well as on Chris. Gripping and unforgettable, Kyle’s masterful account of his extraordinary battlefield experiences ranks as one of the great war memoirs of all time. Please note that due to the large file size of these special features this enhanced e-book may take longer to download then a standard e-book.
                                    [industryIdentifiers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [type] => ISBN_13
                                                    [identifier] => 9780062190970
                                                )

                                            [1] => Array
                                                (
                                                    [type] => ISBN_10
                                                    [identifier] => 0062190970
                                                )

                                        )

                                    [readingModes] => Array
                                        (
                                            [text] => 1
                                            [image] => 
                                        )

                                    [pageCount] => 448
                                    [printType] => BOOK
                                    [categories] => Array
                                        (
                                            [0] => Biography & Autobiography
                                        )

                                    [averageRating] => 4.5
                                    [ratingsCount] => 21
                                    [maturityRating] => NOT_MATURE
                                    [allowAnonLogging] => 1
                                    [contentVersion] => 0.8.8.0.preview.2
                                    [imageLinks] => Array
                                        (
                                            [smallThumbnail] => http://books.google.com/books/content?id=1qWEMxNe2mAC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api
                                            [thumbnail] => http://books.google.com/books/content?id=1qWEMxNe2mAC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
                                        )

                                    [language] => en
                                    [previewLink] => http://books.google.com/books?id=1qWEMxNe2mAC&printsec=frontcover&dq=American+Sniper&hl=&cd=2&source=gbs_api
                                    [infoLink] => http://books.google.com/books?id=1qWEMxNe2mAC&dq=American+Sniper&hl=&source=gbs_api
                                    [canonicalVolumeLink] => http://books.google.com/books/about/American_Sniper_Enhanced_Edition.html?hl=&id=1qWEMxNe2mAC
                                )

                            [saleInfo] => Array
                                (
                                    [country] => US
                                    [saleability] => FOR_SALE
                                    [isEbook] => 1
                                    [listPrice] => Array
                                        (
                                            [amount] => 7.99
                                            [currencyCode] => USD
                                        )

                                    [retailPrice] => Array
                                        (
                                            [amount] => 7.99
                                            [currencyCode] => USD
                                        )

                                    [buyLink] => http://books.google.com/books?id=1qWEMxNe2mAC&dq=American+Sniper&hl=&buy=&source=gbs_api
                                    [offers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [finskyOfferType] => 1
                                                    [listPrice] => Array
                                                        (
                                                            [amountInMicros] => 7990000
                                                            [currencyCode] => USD
                                                        )

                                                    [retailPrice] => Array
                                                        (
                                                            [amountInMicros] => 7990000
                                                            [currencyCode] => USD
                                                        )

                                                    [giftable] => 1
                                                )

                                        )

                                )

                            [accessInfo] => Array
                                (
                                    [country] => US
                                    [viewability] => PARTIAL
                                    [embeddable] => 1
                                    [publicDomain] => 
                                    [textToSpeechPermission] => ALLOWED_FOR_ACCESSIBILITY
                                    [epub] => Array
                                        (
                                            [isAvailable] => 1
                                            [acsTokenLink] => http://books.google.com/books/download/American_Sniper_Enhanced_Edition-sample-epub.acsm?id=1qWEMxNe2mAC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api
                                        )

                                    [pdf] => Array
                                        (
                                            [isAvailable] => 
                                        )

                                    [webReaderLink] => http://books.google.com/books/reader?id=1qWEMxNe2mAC&hl=&printsec=frontcover&output=reader&source=gbs_api
                                    [accessViewStatus] => SAMPLE
                                    [quoteSharingAllowed] => 
                                )

                            [searchInfo] => Array
                                (
                                    [textSnippet] => NOW A BLOCKBUSTER MOTION PICTURE DIRECTED BY CLINT EASTWOOD—NOMINATED FOR SIX ACADEMY AWARDS, INCLUDING BEST PICTURE This special enhanced edition features more than 10 exclusive videos with Chris Kyle and an additional 15 images and ...
                                )

                        )

                    [2] => Array
                        (
                            [kind] => books#volume
                            [id] => h2Op0C9hXA0C
                            [etag] => npG91dQFmKo
                            [selfLink] => https://www.googleapis.com/books/v1/volumes/h2Op0C9hXA0C
                            [volumeInfo] => Array
                                (
                                    [title] => The Life and Legend of Chris Kyle: American Sniper, Navy SEAL
                                    [authors] => Array
                                        (
                                            [0] => Michael J. Mooney
                                        )

                                    [publisher] => Little, Brown
                                    [publishedDate] => 2013-04-23
                                    [description] => New York Times bestseller The life story of Chris Kyle, the American Sniper Journalist Michael J. Mooney reveals the life story of Navy SEAL Chris Kyle, the American Sniper, from his Texas childhood up through his death in February 2013. A brutal warrior but a gentle father and husband, Kyle led the life of an American hero and legend. His heroism and reputation in the military service earned him the nickname "the devil" among insurgents and the nickname The Legend among his SEAL brethren, but his impact extended beyond that after he came home, through his work with fellow veterans. Mooney also sheds light on the life of the suffering fellow veteran who killed Kyle and interviews those closest to the late SEAL. The Life and Legend of Chris Kyle is an honest portrayal of the life of a man whose memorial service brought thousands of people to Cowboys Stadium--the most celebrated war hero of our time. [86 pages]
                                    [industryIdentifiers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [type] => ISBN_13
                                                    [identifier] => 9780316278232
                                                )

                                            [1] => Array
                                                (
                                                    [type] => ISBN_10
                                                    [identifier] => 0316278238
                                                )

                                        )

                                    [readingModes] => Array
                                        (
                                            [text] => 1
                                            [image] => 
                                        )

                                    [pageCount] => 64
                                    [printType] => BOOK
                                    [categories] => Array
                                        (
                                            [0] => Biography & Autobiography
                                        )

                                    [averageRating] => 5
                                    [ratingsCount] => 1
                                    [maturityRating] => NOT_MATURE
                                    [allowAnonLogging] => 1
                                    [contentVersion] => 1.4.5.0.preview.2
                                    [imageLinks] => Array
                                        (
                                            [smallThumbnail] => http://books.google.com/books/content?id=h2Op0C9hXA0C&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api
                                            [thumbnail] => http://books.google.com/books/content?id=h2Op0C9hXA0C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
                                        )

                                    [language] => en
                                    [previewLink] => http://books.google.com/books?id=h2Op0C9hXA0C&printsec=frontcover&dq=American+Sniper&hl=&cd=3&source=gbs_api
                                    [infoLink] => http://books.google.com/books?id=h2Op0C9hXA0C&dq=American+Sniper&hl=&source=gbs_api
                                    [canonicalVolumeLink] => http://books.google.com/books/about/The_Life_and_Legend_of_Chris_Kyle_Americ.html?hl=&id=h2Op0C9hXA0C
                                )

                            [saleInfo] => Array
                                (
                                    [country] => US
                                    [saleability] => FOR_SALE
                                    [isEbook] => 1
                                    [listPrice] => Array
                                        (
                                            [amount] => 2.99
                                            [currencyCode] => USD
                                        )

                                    [retailPrice] => Array
                                        (
                                            [amount] => 2.99
                                            [currencyCode] => USD
                                        )

                                    [buyLink] => http://books.google.com/books?id=h2Op0C9hXA0C&dq=American+Sniper&hl=&buy=&source=gbs_api
                                    [offers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [finskyOfferType] => 1
                                                    [listPrice] => Array
                                                        (
                                                            [amountInMicros] => 2990000
                                                            [currencyCode] => USD
                                                        )

                                                    [retailPrice] => Array
                                                        (
                                                            [amountInMicros] => 2990000
                                                            [currencyCode] => USD
                                                        )

                                                    [giftable] => 1
                                                )

                                        )

                                )

                            [accessInfo] => Array
                                (
                                    [country] => US
                                    [viewability] => PARTIAL
                                    [embeddable] => 1
                                    [publicDomain] => 
                                    [textToSpeechPermission] => ALLOWED
                                    [epub] => Array
                                        (
                                            [isAvailable] => 1
                                            [acsTokenLink] => http://books.google.com/books/download/The_Life_and_Legend_of_Chris_Kyle_Americ-sample-epub.acsm?id=h2Op0C9hXA0C&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api
                                        )

                                    [pdf] => Array
                                        (
                                            [isAvailable] => 
                                        )

                                    [webReaderLink] => http://books.google.com/books/reader?id=h2Op0C9hXA0C&hl=&printsec=frontcover&output=reader&source=gbs_api
                                    [accessViewStatus] => SAMPLE
                                    [quoteSharingAllowed] => 
                                )

                            [searchInfo] => Array
                                (
                                    [textSnippet] => New York Times bestseller The life story of Chris Kyle, the American Sniper Journalist Michael J. Mooney reveals the life story of Navy SEAL Chris Kyle, the American Sniper, from his Texas childhood up through his death in February 2013.
                                )

                        )

Open in new window


Finally, here's how I'm displaying the list in the blade view:
<table class="table table-hover">
              <thead>
                  <th>#</th>
                  <th>Cover</th>
                  <th>Title</th>
                  <th>Author(s)</th>
                  <th>Action</th>
              </thead>
              <tbody>
                @if($results->totalItems == 0)
                <h4>No results found.  Please try again.</h4>
                @else               
                @for($i = 0; $i < count($results); $i++)
                  <tr>
                    <td>{{ $i + 1 }}</td> 
                    <td>
                        @if(!$results[$i]->volumeInfo->imageLinks)
                            <img src="{{ URL::to('src/images/noCover.png') }}" alt="">
                        @else
                            <img src="{{ $results[$i]->volumeInfo->imageLinks->thumbnail }}" alt="">
                        @endif
                    </td>
                    <td>{{ $results[$i]->volumeInfo->title }} <br> {{ $results[$i]->volumeInfo->subtitle }}</td>
                    <td>
                        @if(count($results[$i]->volumeInfo->authors) == 1)
                            {{ $results[$i]->volumeInfo->authors[0] }}
                        @elseif(count($results[$i]->volumeInfo->authors) > 1)
                            @foreach($results[$i]->volumeInfo->authors as $author)
                                {{ $author}} <br>
                            @endforeach
                         @endif
                    </td>
                    <td>
                        
                    </td>  
                  </tr>
                  @endfor
                  @endif
              </tbody>
          </table>

Open in new window

0
Comment
Question by:Frank Stalla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41752309
... without sending another request to the Google API?
That sounds like the "hard part" here.  I don't know if this will work, but it's what I might try.  

Modify the "showBookDetail() method. Inject both the original search results (the list object) and the book id.  For example, if the book id is DtqaBwAAQBAJ, then you can use that value in an iterator to locate the book detail in the list object.  The list object does not have 100% of the information that you can get from the JSON response to a standalone call to the API, but it's got a lot of the info, and may meet your needs.
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 41825984
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 93
MySQL limit and not so limited 13 60
How can I split a variable 19 45
htaccess add www, redirect to https, keep query string 17 54
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question