php-tmdb / laravel

Laravel Package for TMDB ( The Movie Database ) API. Provides easy access to the wtfzdotnet/php-tmdb-api library.
MIT License
156 stars 60 forks source link

trying to search for a movie using title? #32

Closed sherwinmartin closed 7 years ago

sherwinmartin commented 7 years ago

hello, i'm trying to search for a movie by title. i can't seem to figure it out. here is my code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Tmdb\Helper\ImageHelper;
use Tmdb\Repository\MovieRepository;
use Tmdb\Repository\SearchRepository;

class TmdbController extends Controller
{
    private $movies;
    private $helper;

    public function __construct(MovieRepository $movies, ImageHelper $helper, SearchRepository $search)
    {
        $this->movies = $movies;
        $this->helper = $helper;
        $this->search = $search;
    }

    public function search()
    {

        $data = [
            'page_title'            => 'Search TMDB',
            'navi_group'            => 'search',
            'navi_submenu'          => 'search',
            'results'               => $this->search->searchMovie('batman')
        ];

        return view('tmdb.search', $data);
    }
}

i get this error

Type error: Argument 2 passed to Tmdb\Repository\SearchRepository::searchMovie() must be an instance of Tmdb\Model\Search\SearchQuery\MovieSearchQuery, none given, called in TmdbController.php on line 30

i tried to pass 'title' or 'movie' as the second argument and i get a similar error that sounds like it's not supposed to be a string. i know i'm doing something wrong. hope someone can help. thanks.

devnick commented 7 years ago

@w1n78 not sure if you resolved this yet or not, but this should do the trick.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Tmdb\Helper\ImageHelper;
use Tmdb\Repository\MovieRepository;
use Tmdb\Repository\SearchRepository;
use Tmdb\Model\Search\SearchQuery\MovieSearchQuery;

class TmdbController extends Controller
{
    private $movies;
    private $helper;

    public function __construct(MovieRepository $movies, ImageHelper $helper, SearchRepository $search)
    {
        $this->movies = $movies;
        $this->helper = $helper;
        $this->search = $search;
    }

    public function search()
    {

        $data = [
            'page_title'            => 'Search TMDB',
            'navi_group'            => 'search',
            'navi_submenu'          => 'search',
            'results'               => $this->search->searchMovie('batman', new MovieSearchQuery)
        ];

        return view('tmdb.search', $data);
    }
}
sherwinmartin commented 7 years ago

@devnick thank you so much. i knew i was missing something. i was so close haha. i also assume all the methods are found in \Tmdb\Model\Movie.php right? i wish there were more sample code. i have a hard time understanding some times

devnick commented 7 years ago

@w1n78 No problem! You are correct in that the individual Movie objects contain all of the methods required to retrieve the data necessary for each item.

I agree with you in terms of the sparse documentation. An organized Wiki would certainly be nice, but fortunately the source code is documented quite well and the there are several typical examples there that help to put things in perspective.

Also, just an FYI, on this call:

$this->search->searchMovie('batman', new MovieSearchQuery)

you'll probably want to chain the toArray() method on it to get an actual returnable dataset back from the searchMovie() method. So:

$this->search->searchMovie('batman', new MovieSearchQuery)->toArray()

should get you the data you want.

sherwinmartin commented 7 years ago

@devnick ok cool thanks. yes, i found the examples but i had to change it so i can use it in laravel. i was so close just couldn't get that second argument to work haha. once i get used to it, i'll probably contribute some example code i use in the wiki that uses laravel. thanks again. i'm rewriting an old codeigniter app i had done a few years ago using the tmdb api. i'm just rusty.

MarkRedeman commented 7 years ago

In case you (or someone else reading this) hadn't seen the examples from the original php-tmdb/api repo, here is a link.

Please feel free to send any Pull Request on documentation, code or anything you like. Both me and @wtfzdotnet don't (actively?) use this project so it's hard to come up with useful documentation.

wtfzdotnet commented 7 years ago

Hey Guys,

I used to use this project for an in-house application to show and list movies that used to be on a certain machine :-). I however since moved to alternatives and pretty much abandoned this application.

We could really use the help of the community to keep maintaining this library, I still maintain the base library on a "sort of common basis", but I'd really prefer the community taking over and just supervising the pull requests.

That said, any attributions to any of the projects in our organisation whether it's documentation or code additions or modifications, they are more than welcome! And I would really encourage you to do so, if you have any questions about the inner workings of php-tmdb/api and help documenting or creating examples that will make it easier for other people that come along after you, please don't hesitate to write me an e-mail :)