如何在laravel中的一个路由中绑定多个相关参数

时间:2017-03-02 22:46:28

标签: laravel laravel-5 routing laravel-5.3 laravel-routing

我有两个相关模特动漫,剧集

我已经更改了两者的RouteKeyName

动漫模特:

public function getRouteKeyName() 
{
    return 'slug';
}

在剧集模型中:

public function getRouteKeyName()
{
    return 'ep_num'; // episode number
}

观看一集,我用这个:

路由/ web.php

Route::get('play/{anime}/{episode}', 'EpisodeController@index');

EpisodeController.php

public function index(Anime $anime, Episode $episode)
{
    return view('play')->with([
        'anime' => $anime,
        'episode' => $episode
    ]);
}

例如,如果我有这个链接

... /播放/鸣/ 10

然后通过使用路线模型绑定,我将有第一集与ep_num = 10

相反,我希望在

时使用ep_num = 10的剧集
anime_id = $anime->id

有没有办法在RouteServiceProvider

中执行此操作

我想将这个应用于包含动漫和剧集的所有路线,例如:

Route::delete('anime/{anime}/episode/{episode}/delete', 'EpisodeController@destroy');
Route::get('anime/{anime}/episode/{episode}/edit', 'EpisodeController@edit');
Route::put('anime/{anime}/episode/{episode}/edit', 'EpisodeController@update');

将其添加到boot()

中的RouteServiceProvider方法
Route::bind('episode', function () {
    $episode = \App\Models\Episode::where('ep_num', request()->episode);

    if (request()->route()->hasParameter('anime')) {
        $anime= \App\Models\Anime::where('slug', request()->anime)->first();
        $episode = $episode->where('anime_id', $anime->id);
    }

    return $episode->firstOrFail();
});

1 个答案:

答案 0 :(得分:7)

您可以执行以下操作:

    # Decision Tree Classifier
from sklearn import datasets
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# load the iris datasets
iris = datasets.load_iris()
#print(iris.data)
#print(dataset)
# fit a CART model to the data
model = LogisticRegression()
from sklearn.utils import shuffle
import numpy as np
#print(type(dataset.data))

#Xtrain = dataset.data[:int(0.8*len(dataset.data))]
#Ytrain = dataset.target[:int(0.8*len(dataset.data))]
#Xtest = dataset.data[int(0.8*len(dataset.data)):]
#Ytest = dataset.target[int(0.8*len(dataset.data)):]
Xtrain, Ytrain, Xtest, Ytest = train_test_split(iris.data.astype(np.float64), iris.target.astype(np.float64), test_size=0.4, train_size=0.6)
model.fit(Xtrain,Ytrain)
#print(model)
# make predictions
expected = Ytest
predicted = model.predict(Xtest)
# summarize the fit of the model
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))

希望这有帮助!