【问题标题】:How to reuse of blade template that contains mysql fetched data in Laravel 5.5如何在 Laravel 5.5 中重用包含 mysql 获取数据的刀片模板
【发布时间】:2020-02-22 05:25:26
【问题描述】:

我是一个初学者,我正在 Controller 中创建一个视图,如下所示。

public function withdrawals()
 {
   return view('withdrawals')
     ->with(array(
     'title'=>'withdrawals',
     'withdrawals' => withdrawals::where('user', Auth::user()->id)->get(),
     'settings' => settings::where('id', '=', '1')->first(),
     'wmethods' => wdmethods::where('type', 'withdrawal')
     ->where('status','enabled')->orderby('name')->get(),
     ));
 } 

所以我将视图返回给resources/views/withdrawals.blade.php。还有另一个文件dashboard.blade.php 并从return view('dashboard') 为仪表板创建视图。我想在dashboard.blade.php 文件中重用withdrawals 视图而不创建另一个视图。我如何在另一个刀片模板中访问包含所有数据的withdrawals 来自数据库的现有刀片内容?

【问题讨论】:

  • dashboard 是否真的需要包含 withdrawals 视图,或者您只希望在您的 withdrawals 方法中包含当前附加到视图的数据?
  • 是的,正如我提到的,我也想从 mysql 获取它的数据。我试过了。 @extends('withdrawals') 在dashboard.blade.php 中。它带来了整个页面,但没有数据。我还想要withdraws.blade 页面的某些特定部分而不是整个页面。

标签: laravel laravel-blade extends


【解决方案1】:

基本上,您必须专注于刀片扩展中的组件和插槽。

看看这个Components in laravel

我基本上在布局设置时使用它。因为有像页眉、页脚、侧边栏这样的东西,这些东西在所有页面中都很常见。请看一下,它可能对你有帮助。

master.blade.php

<!DOCTYPE html>
<html>
<head>
    @include('Admin::Layouts.head-scripts')
    @yield('head-content')
    @yield('page-style')
</head>
<body class="hold-transition skin-blue sidebar-mini">

    {{------------------------------------------CONTENT GOES HERE-----------------------------------------}}
<div class="wrapper">
    @include('Admin::Layouts.header')
    @include('Admin::Layouts.sidebar')
    @yield('page-content')
    @include('Admin::Layouts.footer')
</div>



@include('Admin::Layouts.footer-scripts')
@yield('page-scripts')
</body>
</html>

使用other-blade-control-structures 中提到的@section 和@overwrite 语法来实现这一点。

@extends('default')

@section('content')

    {{-- First Panel --}}
    @section('heading')
        Welcome, {{ $user->name }}
    @overwrite
    @section('inner')
        <p>Welcome to the site.</p>
    @overwrite
    @include('panel')

    {{-- Second Panel --}}
    @section('heading')
        Your Friends
    @overwrite
    @section('inner')
        <ul>
        @foreach($user->friends as $friend)
            <li>{{ $friend->name }}</li>
        @endforeach
        </ul>
    @overwrite
    @include('panel')

@stop

【讨论】:

  • 感谢您的回答,那么如何使用上述方法将withdrawals.blade 的所有数据添加到&lt;div id="wd"&gt; to dashboard.blade 中?你能说得更具体点吗?
猜你喜欢
  • 2016-12-18
  • 1970-01-01
  • 2014-11-13
  • 2018-09-14
  • 2021-05-10
  • 2019-05-06
  • 2014-05-04
  • 2016-09-15
  • 2014-12-27
相关资源
最近更新 更多