Laravel - 刀片未在浏览器中加载

问题描述 投票:1回答:1

我有一个新的Laravel项目似乎没有正确加载Blade。

我查看了Laravel.com上的文档,在Laracasts上观看了视频,并尽可能地扫描了Stackoverflow,但我仍然无法弄明白。你能帮我吗?!

好的,这是我到目前为止所拥有的:

Web.php

Route::get('/', [
    'uses' => 'RentsController@index',
    'as' => 'layouts.index'
    ]);

RentsController.php

public function index()
{
    $rents = DB::table('rents')
        ->orderByDesc('price')
        ->get();
    return view('layouts.index', ['rents' => $rents]);
}

index.blade.php

<div class="container">
    <p>test</p>
    @yield('content')
</div>

search.blade.php

@extends ('layouts.index')

@section ('content')
    <p>title</p>
    <h1>test</h1>
    <ul>
        @foreach ($rents as $rent)
        <li>{{ $rent->price }}</li>
        @endforeach
    </ul>
@endsection

文件结构:

views - > layouts - > index.blade.php views - > search.blade.php

因此,每当我将@section('content')中的代码直接粘贴到index.blade.php文件中时,它都能完美地运行。但是,正如我现在所知,search.blade.php中没有任何内容出现在浏览器中,也没有错误。

知道它可能是什么?

多谢你们!!我非常感谢您提供的任何帮助,提示,问题和评论。 :)

php laravel-5.4 laravel-blade
1个回答
2
投票

实际上,我不知道你为什么在路由和控制器中指定你的布局。它足以在控制器中返回您的页面模板:

Web.php

Route::get('/','RentsController@index');

RentsController.php

public function index()
{
    $rents = DB::table('rents')
        ->orderByDesc('price')
         ->get();
    return view('search', compact('rents'));
}

在使用布局时,使用@extends ('layouts.index')就足够了,模板将识别它。

© www.soinside.com 2019 - 2024. All rights reserved.