小编mat*_*iti的帖子

Laravel 组件:{{ slot }} 中的默认内容

在旧式的 Laravel Blade 模板中,我们使用@section('section-name')以下方式:

{{-- h1para.blade.php --}}
<h1>
    @section('heading')
        Heading from Template
    @endsection
</h1>
<p>
    @yield('details')
</p>
Run Code Online (Sandbox Code Playgroud)

然后扩展该模板:

{{-- content.blade.php --}}
@extends('h1para')

@section('details')
    Content from content page
@endsection
Run Code Online (Sandbox Code Playgroud)

在上面,我渲染的 HTML 输出如下所示,因为'heading'扩展文件中的“缺失”部分意味着我们默认返回模板中的内容:

<h1>Heading from Template</h1>
<p>Content from content page</p>
Run Code Online (Sandbox Code Playgroud)

但在新的组件做事方式中,我这样做:

<h1>Heading from Template</h1>
<p>Content from content page</p>
Run Code Online (Sandbox Code Playgroud)

如果您还没有收集到,问题是:如何为槽的内容设置默认值,这样如果扩展组件/模板中没有提供它,它就会回退到该默认值?

(我已经完成了搜索,但无法找到之前提出的相同问题)


编辑:

我应该补充一点,我已经看到了解决方案(在 Laravel 文档中):

{{-- .../components/h1para.blade.php --}}
<h1>{{ $heading }}</h1>
<p>{{ $slot }}</p>
Run Code Online (Sandbox Code Playgroud)

但这似乎仅适用于默认值是易于管理的短字符串的情况。如果默认是很长的 HTML 流,那么它就无法满足我的需求。


编辑2:

重申一下:问题的关键在于默认内容可能是一长串 HTML。通过传入字符串(无论是否格式化为 HTML)来解决问题无法满足我的实际需求。

laravel laravel-blade laravel-components

5
推荐指数
1
解决办法
3312
查看次数

升级到 Laravel 10 后,Larastan 抱怨收集方法参数

升级到 Laravel 10 后,我遇到了 Larastan 错误的困难时期。

下面的代码在 1 小时前还完全正常:

return $this->articleRepository->getDefaultArticles($organizationId)
    ->toBase()
    ->map(function (Article $article) {
        return new Content(
            $article->id,
            $article->title,
            $article->language,
        );
    })
    ->toArray();
Run Code Online (Sandbox Code Playgroud)

现在给我以下错误:

方法 Illuminate\Support\Collection<(int|string),Illuminate\Database\Eloquent\Model>::map() 的参数 #1 $callback 需要 callable(Illuminate\Database\Eloquent\Model, int|string): App\学院\内容,
关闭(应用程序\模型\文章):给出的应用程序\学院\内容

存储库方法有正确的提示:

/**
 * @return Collection<Article>
 */
public function getDefaultArticles(OrganizationId $organizationId): Collection
{
    /** @var Collection<Article> */
    return Article::query()
        ->where('organization_id', $organizationId)
        ->get()
        ->keyBy('id')
        ->values();
}
Run Code Online (Sandbox Code Playgroud)

它给了我 115 个新错误,其中大多数与此类似,与map和等收集方法相关reduce

快速解决方案是使用临时变量并添加类型提示:

/** @var Collection<Article> $articles */
$articles = $this->articleRepository
    ->getDefaultArticles($organizationId)
    ->toBase();
Run Code Online (Sandbox Code Playgroud)

但我不想执行 100 …

php laravel phpstan laravel-10

5
推荐指数
1
解决办法
1246
查看次数

使用apache隐藏.git目录或文件

我想拒绝访问.git目录(我个人更改了.git文件夹位置,因此它创建了一个.git具有该.git文件夹路径的文件).

我正在使用Apache 2.4.18.这是我添加到文件底部的apache2.conf

# Include my personal config
Include personal.conf
Run Code Online (Sandbox Code Playgroud)

在里面personal.conf我写道:

<DirectoryMatch "^\.git">
    Require all denied
</DirectoryMatch>

<FilesMatch "^\.git">
    Require all denied
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)

因此,这将拒绝访问该文件/目录.git,从用户通过URL的任何位置开始.
我对吗?我的意思是,如果用户试图访问www.example.com/.git*www.example.com/---/---/---/---/---/.gitsomething

这适用于任何虚拟主机吗?有什么建议?

apache git

3
推荐指数
1
解决办法
2469
查看次数

错误 [RuntimeException] 无法扫描“数据库/种子”内的类

我在运行作曲家更新时遇到问题

其他安装命令也会发生同样的情况

错误:

[RuntimeException] 无法扫描“数据库/种子”内的类,该类似乎既不是文件也不是文件夹

我该如何解决这个错误?尝试运行composer install,它也不起作用

{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
    "framework",
    "laravel"
],
"license": "MIT",
"require": {
    "php": "^7.2.5",
    "fideloper/proxy": "^4.2",
    "fruitcake/laravel-cors": "^1.0",
    "guzzlehttp/guzzle": "^6.3",
    "laravel/framework": "^7.0",
    "laravel/tinker": "^2.0",
    "laravel/ui": "^2.0"
},
"require-dev": {
    "facade/ignition": "^2.0",
    "fzaninotto/faker": "^1.9.1",
    "mockery/mockery": "^1.3.1",
    "nunomaduro/collision": "^4.1",
    "phpunit/phpunit": "^8.5"
},
"config": {
    "optimize-autoloader": true,
    "preferred-install": "dist",
    "sort-packages": true
},
"extra": {
    "laravel": {
        "dont-discover": []
    }
},
"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "database/seeds", …
Run Code Online (Sandbox Code Playgroud)

php laravel composer-php

3
推荐指数
1
解决办法
1万
查看次数

无法解析从“resources\js\Pages\Dashboard.vue”导入“@inertia/inertia”

我创建了一个 Laravel 应用程序,然后使用 Laravel Breeze 添加了 Inertia 和 Vue。一切都运行良好,直到我写下以下内容: import { Inertia } from '@inertia/inertia'

在我的AppName\\resources\\js\\Pages\\Dashboard.vue。然后,奇怪的是,当我在仪表板中打开浏览器时,我收到了以下 Vite 错误:

[插件:vite:导入分析] 无法解析从“resources\js\Pages\Dashboard.vue”导入“@inertia/inertia”。该文件存在吗?

这是我的package.json

{
    "private": true,
    "scripts": {
        "dev": "vite",
        "build": "vite build"
    },
    "devDependencies": {
        "@inertiajs/vue3": "^1.0.0",
        "@tailwindcss/forms": "^0.5.3",
        "@vitejs/plugin-vue": "^4.0.0",
        "autoprefixer": "^10.4.12",
        "axios": "^1.1.2",
        "laravel-vite-plugin": "^0.7.2",
        "lodash": "^4.17.19",
        "postcss": "^8.4.18",
        "tailwindcss": "^3.2.1",
        "vite": "^4.0.0",
        "vue": "^3.2.41"
    },
    "dependencies": {
        "moment": "^2.29.4"
    }
}
Run Code Online (Sandbox Code Playgroud)

inertiajs laravel vue.js vite

3
推荐指数
1
解决办法
4200
查看次数

Laravel 10 Unit Test does not see DB change from Artisan Command

I have an artisan command (Laravel 10) that makes a change to the user table in the database. I can confirm that the change is happening with output from the command itself, and it looks like it's actually working.

However, when I run the unit test, it does not see the updated database change.

Unit Test

public function test_promote_user_command_sets_user_as_super_admin()
{
    $user = $this->createUser('guest');

    $response = $this->artisan("user:promote {$user->email}")->assertSuccessful();

    $role = Role::where('name', '=', 'Super Admin')->first();
    $this->assertDatabaseHas('users', [
        'id' => $user->id,
        'role_id' => …
Run Code Online (Sandbox Code Playgroud)

php testing tdd laravel laravel-artisan

3
推荐指数
1
解决办法
490
查看次数

通过 Composer 安装 Livewire 3 时出错

我在安装 livewire 3 软件包时遇到一些问题,我收到以下信息:

问题1
根composer.json需要livewire/livewire 3.0@beta,发现livewire/livewire[dev-throw-error-when-testing-non-livewire-class, ..., dev-bad-hotfix-test, v0.0.1 , ..., v0.7.4, v1.0.0, ..., 1.x-dev, v2.0.0, ..., 2.x-dev, v3.0.0-beta.1, v3.0.0-beta .2, v3.0.0-beta.3] 但它与约束不匹配。
安装失败,将 ./composer.json 和 ./composer.lock 恢复为其原始内容

像网站文档一样安装Livewire 3。

livewires laravel laravel-livewire

3
推荐指数
1
解决办法
2280
查看次数

目标类 [PostController] 不存在。但它剂量

我得到的错误是:Target class [PostController] does not exist但确实如此。

路线web.php

Route::get('/post', 'PostController@index');
Route::post('/post', 'PostController@store');

Route::get('/', function () {
    return view('create');
});
Run Code Online (Sandbox Code Playgroud)

PostController.php

namespace App\Http\Controllers;

use App\Post;
use Redirect,Response;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        return view('create');
    }

    public function store(Request $request)
    {
        $data = json_encode($request);
        Post::create($data);

        return back()->withSuccess('Data successfully store in json format');
    }
}
Run Code Online (Sandbox Code Playgroud)

laravel laravel-8

2
推荐指数
1
解决办法
1651
查看次数

方法 Illuminate\Support\Str::replace 不存在

我在用:

  • Laravel 版本:8.35.1
  • PHP 版本:7.4.9

在 Tinker 和 Routing 上,我使用文档中的Str::replace()方法,但出现错误:

带有消息 Method Illuminate\Support\Str::replace 的 BadMethodCallException 不存在。

示例 1:

root@c6dd4af63e3c:/var/www/html# php artisan tinker
Psy Shell v0.10.7 (PHP 7.4.9 — cli) by Justin Hileman
>>> Illuminate\Support\Str::replace('8.x', '9.x', 'Laravel 8.x');
BadMethodCallException with message 'Method Illuminate\Support\Str::replace does not exist.'
>>> 
Run Code Online (Sandbox Code Playgroud)

示例2:

Route::get('/test', function () {
    return Illuminate\Support\Str::replace('8.x', '9.x', 'Laravel 8.x');
});
Run Code Online (Sandbox Code Playgroud)

为什么会出现此错误以及如何解决?

laravel laravel-8

2
推荐指数
1
解决办法
229
查看次数

我如何在 laravel phpUnit 测试的另一个测试用例中登录用户?

图1

当我检查方法 in dd(\Auth()::check());case时,它​​会返回,但是当我使用in方法 in时,它会返回。testLoginTrueLoginTesttruedd(\Auth()::check());testClientCreateFormDisplayedClientTestfalse

图2

那么如何在另一个测试用例中获取登录用户呢?在每个测试用例之前都需要登录用户吗?

phpunit laravel laravel-testing

2
推荐指数
1
解决办法
4977
查看次数