使用laravel在phpstorm中的未定义变量。控制器&提供刀片模板

时间:2018-04-21 19:58:43

标签: php laravel blade

我的代码收到此错误消息:

  

未定义变量:问题(查看:   家/ XXXX / PhpstormProjects / XXXXX /资源/视图/ home.blade.php)

请尽量了解可能出现的问题。 我提供了QuestionController&问题模板。不确定,我的代码丢失了什么。这是我的代码:

    QuestionController:

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Question;

class QuestionController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function show(Question $question)
    {

        return view('question')->with('question', $question);
    }



    question.blade.php

    @extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row ">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Question</div>

                    <div class="card-body">

                        {{$question->body}}
                    </div>
                    <div class="card-footer">
                        <a class="btn btn-primary float-right"
                           href="#">
                            Edit Question
                        </a>


                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card">
                    <div class="card-header"><a class="btn btn-primary `float-left"`
                                                href="#">
                            Answer Question
                        </a></div>

                    <div class="card-body">
                        @forelse($question->answers as $answer)
                            <div class="card">
                                <div class="card-body">{{$answer->body}}`</div>`
                                <div class="card-footer">

                                    <a class="btn btn-primary float-right"
                                       href="#">
                                        View
                                    </a>

                                </div>
                            </div>
                        @empty
                            <div class="card">

                                <div class="card-body"> No Answers</div>
                            </div>
                        @endforelse


                    </div>
                </div>
            </div>
@endsection


home.blade.php

    @extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">Questions


                        <div class="card-body">

                            <div class="card-deck">
                                @foreach($questions as $question)
                                    <div class="col-sm-4 d-flex align-items-stretch">
                                        <div class="card mb-3 ">
                                            <div class="card-header">
                                                <small class="text-muted">
                                                    Updated: {{ $question->created_at->diffForHumans() }}
                                                    Answers: {{ $question->answers()->count() }}

                                                </small>
                                            </div>
                                            <div class="card-body">
                                                <p class="card-text">{{$question->body}}</p>
                                            </div>
                                            <div class="card-footer">
                                                <p class="card-text">

                                                    <a class="btn btn-primary float-right" href="{{ route('question.show', ['id' => $question->id]) }}">
                                                        View
                                                    </a>
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </div>

                        </div>
                        <div class="card-footer">
                            <div class="float-right">
                                {{ $questions->links() }}
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
@endsection

1 个答案:

答案 0 :(得分:0)

我想到的第一个想法是,您在控制器中为视图提供了一个名为question的变量,并且您必须在$questions中使用$question as $q 1}}

另一件事是您可能希望将所有问题行传递给您查看,可以通过编辑AppServiceProvider.php

来完成(这可能不是最佳解决方案)

您必须添加此内容。

use View;
use App\Question;

public function boot()
{
    //
    View::share('questions',Question::all());
}

但是如果你这样做,它将分享用你所有观点获得的行。

希望这会有所帮助。