1

从我之前的问题中,我从用户那里得到了这个代码。

    // app/routes.php
Route::get('characters', 'CharactersController@all');
Route::get('characters/{name}', 'CharactersController@detail');


// app/controllers/CharactersController.php
class CharactersController extends BaseController
{
    public function all()
    {
        // show all characters
    }

    public function detail($name)
    {
        // find character by name & show detail for example
        return View::make('acc.test');
    }
}

// app/views/acc/test.blade.php
// HTML::style('css/style.css') loads CSS file located at public/css/style.css
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    {{ HTML::style('css/style.css') }}
</head>
<body>

</body>
</html>

还有,搜索功能:

<form action="{{ URL::action('CharactersController@search') }}" method="get">
<input type="text" name="search-term">
<input type="submit" value="Search">

    public function search()
{
    $name = Input::get('search-term');
    $searchResult = Character::where('name', '=', $name)->get();
    ....
}

Route::get('characters/search', 'CharactersController@search');

我怎么能在:

public function detail($name) { // find character by name & show detail for example return View::make('acc.test'); }

我怎么能按名字找到角色?我试过做类似的事情

$name = $player->name

(我有一个名为 player 的模型,我也将 Character::where 更改为 Player::where),我必须在其中插入什么?另外,我怎么能在视图中显示它?

所以我的意思是,当我按名称搜索玩家时,它会显示每个特定玩家的玩家名称($player->name)。

我还必须将模型中的关系更改为 HasMany 或类似的东西吗?

4

1 回答 1

1

你可以这样做

public function detail($name) {
    $player = Player::where('name', '=', $name)->first();
}

如果更多玩家可以有相同的名字,我宁愿传递 ID 而不是名字,所以在这种情况下使用这个解决方案

public function detail($id) {
    $player = Player::findOrFail($id);
}
于 2013-07-31T06:45:34.290 回答