1

我是从 Laravel 开始的初学者。我试图展示在这个网站上提出的问题。

这是控制器页面:

public function show($id)
{
    //Use the model to get 1 record from the database
    $question = $Question::findOrFail($id);

    //show the view and pass the record to the view
    return view('questions.show')->with('question', $question);
}

我已在文件顶部包含:

    use App\Question;

这是我的刀片页面:

@section('content')

<div class="container">
    <h1> {{ $question->title }} </h1>
    <p class="lead">
        {{ $question->description }}
    </p>

    <hr />
</div>

@endsection

在模型中我没有定义任何东西,因为我不需要指定任何特殊规则。最后是路线:

Route::resource('questions', 'QuestionController');

我收到错误“ErrorException Undefined Variable: Question”,据说错误是:

$question = $Question::findOrFail($id);

我期待着你的意见。

亲切的问候。

4

1 回答 1

2

您只需要更改控制器部分

public function show($id)
{
    //Use the model to get 1 record from the database
    $question = Question::findOrFail($id); // here is the error

    //show the view and pass the record to the view
    return view('questions.show')->with('question', $question);
}

说明:-您将使用未定义的变量 $Question。这是基本的 PHP 错误,而不是 laravel 问题。但是,您使用的是“App\Question”模型类而不是单独变量。

于 2020-04-05T16:52:53.387 回答