【问题标题】:Laravel 5.8 Passing parameter to view returned undefinedLaravel 5.8 传递参数查看返回未定义
【发布时间】:2019-12-30 08:16:30
【问题描述】:

我想将用户名传递给浏览器中的 url,如“/profile/{username that's logged in here}”。但是为了测试一下,我首先尝试通过 id 。 当我按下实际链接时,这不是问题所在。当我尝试仅访问欢迎页面时出现问题。

我在视图中调用了路由并将正确的参数“用户”传递给它,但它仍然给我一个未定义的变量错误。

这是路线

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/profile/{user}', 'ProfilesController@index')->name('profile.show');

ProfilesController:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class ProfilesController extends Controller
{
    public function index($user)
    {
        $user = User::find($user);
        return view('home', [
            'user' => $user,
        ]);
    }
}

欢迎之刃:

<div class="flex-center position-ref full-height">
            @if (Route::has('login'))
                <div class="top-right links">
                    @auth
                        <a href="{{ route('profile.show', $user) }}">Home</a>
                    @else
                        <a href="{{ route('login') }}">Login</a>

                        @if (Route::has('register'))
                            <a href="{{ route('register') }}">Register</a>
                        @endif
                    @endauth
                </div>
            @endif

            <div class="content">
                <div class="title m-b-md">
                    gramClone
                </div>
                <p class="content">
                    where IG pics get to be shittier.
                </p>
            </div>
        </div>

按下主页链接后调用的主页刀片:

@section('content')
<div class="container">
    <div class="row">
        <div class="col-4 p-5">
            <img src="/png/logo.png" alt="logo" style="height: 6rem; width: 6rem;" class="float-right rounded-circle">
        </div>
        <div class="col-8 pt-5">
            <div><h1>{{$user->username}}</h1></div>
            <div class="d-flex">
                <div class="pr-5"><strong>number here</strong> posts</div>
                <div class="pr-5"><strong>number here</strong> followers</div>
                <div class="pr-5"><strong>number here</strong> following</div>
            </div>
            <div class="pt-4 font-weight-bold">Bio Summary Here</div>
            <div>Bio Here</div>
            <div><a href="#">Link Here</a></div>
        </div>
    </div>

    <div class="row pt-5">
        <div class="col-4"><img src="https://images.unsplash.com/photo-1566624790190-511a09f6ddbd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
        <div class="col-4"><img src="https://images.unsplash.com/photo-1544127715-bafd09df7c52?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
        <div class="col-4"><img src="https://images.unsplash.com/photo-1566592952746-15ea1f1a4133?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=653&q=80" alt="" class="w-100"></div>
    </div>
</div>
@endsection

我的一些朋友说这是因为我也必须将参数放在 get'/' 但是当我尝试它时,它又给了我之前未传递参数的错误。

请帮我解决这个问题,我已经溢出了 3 个小时,但仍然没有得到任何实际答案。

【问题讨论】:

  • 值是否显示在控制器中?
  • dd($user) 在welcome.blade.php 中。看看你是否在这里得到数据。
  • 你是什么意思? @shihab
  • 控制器中$user 的值是多少。你可以dd($user)看价值
  • 喜欢这个吧?

标签: php routes laravel-5.8


【解决方案1】:

在欢迎刀片中注释掉我的路由调用后发现了问题,显然是@auth 让事情变得怪异。经过一些快速的谷歌搜索后,我发现如果你有一个@auth,显然你不能只说route('profile.show', $user)

你需要这样称呼它:auth()-&gt;user()

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 2020-01-23
    • 2020-07-15
    • 1970-01-01
    • 2021-12-05
    • 2020-06-04
    • 2020-04-22
    • 2019-10-27
    • 2023-03-03
    相关资源
    最近更新 更多