简体   繁体   English

如何在 laravel 中将用户 ID append 到 URL 的末尾?

[英]How can I append a user_id to the end of a URL in laravel?

I'm creating a report table for each user in my database that logs dates/times.我正在为我的数据库中记录日期/时间的每个用户创建一个报告表。 Here's my controller:这是我的 controller:

 public function create()
    {
        $user = auth()->user();

        return view('report.create', compact('user'));
    }

 public function show($id) 
    {
        $data = Time::findOrFail($id);

        return view('report.show', compact('data'));  
    }

So right now if I go to /reports/1, it'll only give me access to the times data with an id of one.所以现在如果我将 go 转到 /reports/1,它只会让我访问 id 为 1 的时间数据。 However, I want it so if I go to reports/1, i'll have access to all the times data for the user with a user_id of 1.但是,我想要它,所以如果我 go 到报告/1,我将可以访问 user_id 为 1 的用户的所有时间数据。

I tried doing the following, but it didn't work and gave me a 404 on every /# page.我尝试执行以下操作,但没有成功,并且在每个 /# 页面上都给了我一个 404。

 public function show(Time $time) 
    {
        $data= Time::findOrFail($time->user_id)

        return view('report.show', compact('data'));  
    }

Here's my routes for 'reports':这是我的“报告”路线:

Route::resource('reports', 'ReportController');

And here's my times DB migration that i want to use the data from:这是我想使用以下数据的时间数据库迁移:

 Schema::create('times', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->date('start_day');
            $table->text('category');
            $table->time('start_time');
            $table->time('finish_time');
            $table->time('duration');
            $table->text('notes');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');

hi you only need to modify the show controller like this您好,您只需要像这样修改显示 controller

public function show($id) 
{
    $data = Time::where('user_id',$id)->get();

    return view('report.show', compact('data'));  
}

You need to define the route parameter.您需要定义路由参数。 It also looks like you have some auto wiring issues.看起来你也有一些自动接线问题。

Route::resource('reports/{id}', 'ReportController');

Change改变

$data= Time::findOrFail($time->user_id);

to

$data= Time::where('user_id','=',$time->user_id)->get();

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

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