我正在使用Laravel 5.6。此页面不适用于我。
pizza / index.blade.php看起来像这样:
@extends('layouts.app')
@section('content')
<!-- Styles -->
<link href="{{ asset('css/pizza.css') }}" rel="stylesheet">
<!-- Scripts -->
<script src="{{ asset('js/components/pizza.js')}}"></script>
<div class="container">
<div class="row justify-content-center">
<div class="card">
<div class="card-header">Pizza</div>
<div class="card-body">
<form action="/pizzas" method="post">
@if ($errors->any())
<div class="alert alert-danger" role="alert">
Please fix the following errors
</div>
@endif
@include('pizza.table')
</form>
</div>
</div>
</div>
</div>
@endsection
Run Code Online (Sandbox Code Playgroud)
披萨/table.blade.php:
<div class="pizza-selection">
<b>Thanks god its pizza day! Select your pizza of the day!</b>
<table id="pizzas" class="md-box">
<tr>
...
</tr>
@foreach ($pizzas as …Run Code Online (Sandbox Code Playgroud) 在 laravel Blade 模板中,我们可以使用以下代码排除 HTML 的某些部分:
@if (Auth::user())
<li><a href="{{ url('/home') }}">Mein Profil</a></li>
<li><a href="{{ url('/admin') }}">Admin</a></li>
@else
<li><a href="{{ url('/home') }}">Mein Profil</a></li>
@endif
Run Code Online (Sandbox Code Playgroud)
如果用户通过身份验证,则显示主页和管理链接,如果用户未通过身份验证,则仅显示主页链接。
我的问题是如何在这里检查用户是否是管理员?
我有来自 laravel 的默认登录系统,我刚刚在表 users -> ('admin') 中添加了一列,tinyint 值为 1,在这个视频 https://www.youtube.com/watch?v=tbNKRr97uVs 中, 我找到了检查用户是否为管理员的代码
if (!Auth::guest() && Auth::user()->admin )
Run Code Online (Sandbox Code Playgroud)
它在 AdminMiddleware.php 中工作,但在刀片中不起作用。如何使这个工作?
在我的刀片代码中,我需要一个计数器,以便为由 foreach 循环呈现的 div 提供唯一 ID。为此,我在刀片模板中创建了一个变量,如下所示:
{{ $counter = 0 }}
Run Code Online (Sandbox Code Playgroud)
我在 html 中使用它只是输出它 {{ $counter = 0 }}
后来,我像这样增加它: {{ $counter++ }}
这一切都像一个魅力,除了{{ $counter++ }}它不仅增加变量,它还输出到视图。
有什么办法可以防止这种情况吗?
我有用户警卫(名称为web)和管理员警卫(名称为admin)。因此,为了显示/隐藏内容,我使用 Laravel Blade 指令@auth('web'),@auth('admin')但不知何故它@guest不起作用,所以我被迫像这样使用:
@auth('admin')
<li>Dashboard</li>
<li>Log out</li>
@endauth
@auth('web')
<li>Create Post</li>
<li>Log out</li>
@endauth
@guest('admin')
@guest('web')
<li>Contact</li>
<li>About us</li>
@endguest
@endguest
Run Code Online (Sandbox Code Playgroud)
我嵌套了访客指令,但感觉太多了,有更好的方法吗?
我想cknow/laravel-money在我的应用程序的几乎每个页面中都使用这个包。我认为一个整洁的方法是在父刀片中导入类。但是,子模板似乎无权访问导入的类。
我试过使用标准<?php ?>标签和@php @endphp指令。
应用程序.blade.php
@php
use Cknow\Money\Money;
@endphp
Run Code Online (Sandbox Code Playgroud)
<title>{{ config('app.name') }}</title>
</head>
<body>
@include('inc.navbar')
@include('inc.flashmessages')
<div class="container">
@yield('content')
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
show.blade.php
<title>{{ config('app.name') }}</title>
</head>
<body>
@include('inc.navbar')
@include('inc.flashmessages')
<div class="container">
@yield('content')
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这抛出。
“找不到‘钱’类。”
而在子类中包含相同的 use 语句按预期工作。
我喜欢在 Atom 中自定义 JS-Beautify 以更好地格式化我的刀片文件。
我们的手动样式如下所示:
{{--
Template Name: Full Width Template
--}}
<!-- template-fullwidth -->
@extends('layouts.app')
@section('content')
@while(have_posts())
@php the_post() @endphp
@include('sections.content-page')
@endwhile
@endsection
Run Code Online (Sandbox Code Playgroud)
JS-Beautify 给了我们这个:
{{-- Template Name: Full Width Template
--}}
<!-- template-fullwidth -->
@extends('layouts.app')
@section('content')
@while(have_posts())
@php the_post()
@endphp
@include('sections.content-page')
@endwhile
@endsection
Run Code Online (Sandbox Code Playgroud)
我试过配置 .jsbeautifyrc 文件,但没有成功。
我想:
或者,如果所有这些都失败了,则需要某种可读的、格式良好的代码,而不仅仅是一长串指令。
这能做到吗?
我正在 Laravel 6.2 中工作,并且有一个基本数据库,其中包含一个questions表,该表可返回 id、slug、标题、正文、时间戳。我设置了一条路线,questions调用一个控制器,该控制器查询数据库并将问题传递到视图index.blade.php,在这里我用分页显示问题。问题是我可以$questions->title很好地显示,但是当我尝试显示时,$question->body我不断收到特定的超时错误。我已经将 .ini 中的 max_execution 从 30 更改为 300,但没有效果。
具体错误
PHP Fatal error: Maximum execution time of 30 seconds exceeded in /home/projects/inquiry-app/vendor/symfony/polyfill-mbstring/Mbstring.php on line 603
Run Code Online (Sandbox Code Playgroud)
603行:
return (string) iconv_substr($s, $start, $length, $encoding);
Run Code Online (Sandbox Code Playgroud)
网页.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('questions', 'QuestionsController');
Run Code Online (Sandbox Code Playgroud)
问题控制器
class QuestionsController extends Controller
{
public function index()
{
$questions = Question::latest()->paginate(2);
return view('questions.index', compact('questions'));
}
}
Run Code Online (Sandbox Code Playgroud)
索引.blade.php
@foreach($questions as $question) …Run Code Online (Sandbox Code Playgroud) 我有一个 Laravel 7 项目,需要在显示之前将大量数据从模型转换为视图。
我想过使用Laravel Accessors并直接在我的blade.php 文件中使用它们。
但是当我处理完一个简单的 html 表时,我查看了我的代码,我认为访问器太多了,甚至其中一些访问器的名称难以阅读。
@foreach($races as $race)
<tr>
<td>{{ $race->display_dates }}</td>
<td>{{ $race->display_name }}</td>
<td>{{ $race->type->name }}</td>
<td>{{ $race->display_price }}</td>
<td>{{ $race->display_places }}</td>
<td>{{ $race->online_registration_is_open ? 'Yes' : 'No' }}</td>
</tr>
@endforeach
Run Code Online (Sandbox Code Playgroud)
public function show(Group $group)
{
$races = $group->races;
$races->loadMissing('type'); // Eager loading
return view('races', compact('races'));
}
Run Code Online (Sandbox Code Playgroud)
// Accessors
public function getOnlineRegistrationIsOpenAttribute()
{
if (!$this->online_registration_ends_at && !$this->online_registration_starts_at) return false;
if ($this->online_registration_ends_at < now()) return false;
if ($this->online_registration_starts_at > …Run Code Online (Sandbox Code Playgroud) 任何人都可以帮助我如何使用 PHP laravel 将文件上传到 aws S3 存储桶。但文件应该使用预签名 URL 直接上传到 S3。
我想知道是否值得从这段代码中删除。
@extends('layouts.app')
@section('title', 'Page Title')
@section('content')
@foreach ($tasks as $task)
{{ $task }}
@endforeach
@endsection
Run Code Online (Sandbox Code Playgroud)
对于这个。
<x-layout>
<x-slot:title>
Custom Title
</x-slot>
@foreach ($tasks as $task)
{{ $task }}
@endforeach
</x-layout>
Run Code Online (Sandbox Code Playgroud)
我的意思是使用 Laravel 组件比仅使用 @extend、@yeild 和 @section 等更好。
laravel-blade ×10
laravel ×8
php ×7
amazon-s3 ×1
atom-editor ×1
aws-sdk ×1
blade ×1
ini ×1
javascript ×1
js-beautify ×1
laravel-5.2 ×1
model ×1
view ×1