简体   繁体   English

laravel 如果请求是刀片中的用户名

[英]laravel if request is username in blade

I have simple URL /players/USERNAME to view user profile.我有简单的 URL /players/USERNAME 来查看用户资料。 I want to insert some thing only in user profile page.我只想在用户个人资料页面中插入一些东西。 Cuz user profile got another links.因为用户个人资料有另一个链接。 Example:例子:

@if (Request::is('players/{{ $user->slug }}'))
@include('users::stats') 
@endif

But dont get anything.但什么也得不到。 Any ideas what's wrong?有什么想法有什么问题吗? Thanks!谢谢!

I don't think Request::is is necessary in your blade view.我不认为Request::is在您的刀片视图中是必需的。 You should have a route defined in your web.php file to capture that pattern meaning you already know the end user is on players/{username} .您应该在web.php文件中定义一个路由来捕获该模式,这意味着您已经知道最终用户在 player players/{username}上。

web.php web.php

Route::get('/players/{slug}', [
    PlayerController::class, 'show'
])->name('players.show');

PlayerController.php PlayerController.php

public function show($slug)
{
    $user = User::where('username', $slug')->firstOrFail();

    return view('players.show', compact('user'));
}

Blade view (players/show.blade.php)刀片视图 (players/show.blade.php)

// some stuff

@include('users.stats')

// some other stuff

Note that I've named the methods and files based on what you've provided, you'll need to change them to be whatever they are in your project.请注意,我已根据您提供的内容命名了方法和文件,您需要将它们更改为项目中的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM