Вопрос

I am using a 16 character alphanumeric string as IDs (MDBIDs) throughout my app; for users, artists, albums, tracks etc.

I am using route::pattern to match this.

routes.php

Route::pattern('mdbid', '[a-zA-Z0-9]{16}');

Route::get('/{mdbid}','MdbidsController@show');

MdbidsController.php (controller)

public function show($mdbid)
{
    //return $mdbid;
    return Mdbid::whereMdbid($mdbid)->first()->table();
}

Mdbid.php (model)

public function table() {
    return $this->table_name;
}
public function show($mdbid)
{
    switch($table_name) {
        case 'artists':

            break;
    }
    //$album = Album::findOrFail($id);
    $album = Album::whereMdbid($mdbid)->firstOrFail();

    return View::make(table().'show', compact('album'));
}

I would like to know how I can find which table the MDBID belongs to and then call the show method on that model.

I am thinking that it would be ok to have a show method on the IdsController that returns the relevant view but I am stuck as to how to go about it.

Это было полезно?

Решение 2

If anyone has any issues with this, then please do say. However, this is the solution that I have some up with and it works.

MdbidsController.php

public function show($mdbid)
{
    try {
        $table = Mdbid::whereMdbid($mdbid)->firstOrFail()->table();
    } catch ( ModelNotFoundException $e ) {
        Notification::error("Sorry that MDBID doesn't exist.");
        return Redirect::home();
    }
    switch ($table) {
        case 1:
            $artist = Artist::whereMdbid($mdbid)->firstOrFail();
            return Redirect::to('artists/' . $artist->mdbid);
            break;
        case 2:
            $album = Album::whereMdbid($mdbid)->firstOrFail();
            return Redirect::to('albums/' . $album->mdbid);
            break;
        case 3:
            $track = Track::whereMdbid($mdbid)->firstOrFail();
            return Redirect::to('tracks/' . $track->mdbid);
            break;
        case 7:
            $user = User::whereMdbid($mdbid)->firstOrFail();
            return Redirect::to('users/' . $user->mdbid);
            break;
        case 8:
            $tag = Tag::whereMdbid($mdbid)->firstOrFail();
            return Redirect::to('tags/' . $tag->mdbid);
            break;
    }
}

Другие советы

This could be a good use-case for polymorphic relationships - you have one table that stores all the MDBIDs, and that table links out to artists, albums, etc.

Check out the docs.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top