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

时间:2018-09-01 23:07:42

标签: php laravel-5.4 laravel-blade

我有一个新的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

文件结构:

视图->布局-> index.blade.php 视图-> search.blade.php

因此,每当我将@section('content')中的代码直接粘贴到文件index.blade.php中时,它就可以完美地工作。但是,正如我现在所掌握的那样,浏览器中没有来自search.blade.php的内容,并且也没有错误。

有什么想法吗?

谢谢大家!!我非常感谢您可以提供的任何帮助,技巧,问题和评论。 :)

1 个答案:

答案 0 :(得分: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'),模板就会识别出它。

相关问题