如何获取并存储我单击按钮所在的行的 ID?

How do I get fetch and store the ID of the row I'm clicking the button on?

简要解释一下我的标题:我有一个 table 打印所有用户及其信息的地方。我在每一行上都有一个 Create Schedule 按钮。当我单击“创建计划”按钮时,我会转到一个页面,我将在其中填写该用户每周 7 天的计划,然后保存它。当我保存它时,我希望我刚刚填写的所有数据都保存在一个名为 'schedules' 的单独数据库 table 中,其中我还有一个名为 'user_id' 的列,这是 ID我正在为其添加日程安排的用户。现在,我已经完成了大部分工作,除了我在 Schedules DB table 中保存我正在创建其日程安排的用户 ID 的部分。这是我的 create() 和 store() 函数:

ScheduleController(资源控制器):

public function create(User $user) {
    $user_id = User::all()->where('id',"=",$user->id);
    return view('createschedule',compact('user_id'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request, Schedule $schedule, User $user) {

    $schedule->user_id =  User::all()->where('id',"=",$user->id);
    $schedule->mon1 = $request->input('mon1');
    $schedule->mon2 = $request->input('mon2');
    $schedule->mon3 = $request->input('mon3');
    $schedule->mon4 = $request->input('mon4');
    $schedule->mon5 = $request->input('mon5');
    $schedule->mon6 = $request->input('mon6');
    $schedule->mon7 = $request->input('mon7');

    $schedule->save();
    return redirect()->route('dashboard.index');
}

CreateSchedule.blade.php(附表表格的视图):

{!! Form::open(array('route'=>'schedule.store')) !!}
                    <table class="table table-bordered table-striped" border="2" cellspacing="3" align="center">
                            <tr>
                             <td align="center">
                             <td>8:30-9:30
                             <td>9:30-10:30
                             <td>10:3-11:30
                             <td>11:30-12:30
                             <td>12:30-2:00
                             <td>2:00-3:00
                             <td>3:00-4:00
                             <td>4:00-5:00
                            </tr>

                            <tr>
                             <td align="center">MONDAY
                             <td align="center">{!! Form::select('mon1', [
                                    'Math' => 'Math',
                                    'English' => 'English',
                                    'Physics' => 'Physics',
                                    'Computer' => 'Computer',],null, ['class'=>'custom-select','placeholder' => 'Subject']); !!}
                             <td align="center">{!! Form::select('mon2', [
                                    'Math' => 'Math',
                                    'English' => 'English',
                                    'Physics' => 'Physics',
                                    'Computer' => 'Computer',],null, ['class'=>'custom-select','placeholder' => 'Subject']); !!}<br>
                             <td align="center">{!! Form::select('mon3', [
                                    'Math' => 'Math',
                                    'English' => 'English',
                                    'Physics' => 'Physics',
                                    'Computer' => 'Computer',],null, ['class'=>'custom-select','placeholder' => 'Subject']); !!}<br>
                             <td align="center">{!! Form::select('mon4', [
                                    'Math' => 'Math',
                                    'English' => 'English',
                                    'Physics' => 'Physics',
                                    'Computer' => 'Computer',],null, ['class'=>'custom-select','placeholder' => 'Subject']); !!}<br>
                             <td rowspan="6"align="center">L<br><br>U<br><br>N<br><br>C<br><br>H
                             <td align="center">{!! Form::select('mon5', [
                                    'Math' => 'Math',
                                    'English' => 'English',
                                    'Physics' => 'Physics',
                                    'Computer' => 'Computer',],null, ['class'=>'custom-select','placeholder' => 'Subject']); !!}<br>
                             <td align="center">{!! Form::select('mon6', [
                                    'Math' => 'Math',
                                    'English' => 'English',
                                    'Physics' => 'Physics',
                                    'Computer' => 'Computer',],null, ['class'=>'custom-select','placeholder' => 'Subject']); !!}<br>
                             <td align="center">{!! Form::select('mon7', [
                                    'Math' => 'Math',
                                    'English' => 'English',
                                    'Physics' => 'Physics',
                                    'Computer' => 'Computer',],null, ['class'=>'custom-select','placeholder' => 'Subject']); !!}
                            </tr>

</table>
{!! Form::submit('Create',['type'=>'submit','class'=>'btn btn-success btn-block']) !!}
{!! Form::close() !!}

仪表板视图(我打印所有用户并将他们各自的 ID 传递给“添加计划”按钮):

<table id="myTable" class="table table-striped table-bordered">
                        <thead>
                    <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Roll Number</th>
                    <th>Class</th>
                    <th>Action</th>
                    </tr>
                        </thead>
                        <tbody>
                        @foreach($users as $user)
                    <tr>
                        <td>{{ $user->fname }}</td>
                        <td>{{ $user->lname }}</td>
                        <td>{{ $user->name }}</td>
                        <td>{{ $user->email }}</td>
                        <td>{{ $user->rollno }}</td>
                        <td>{{ $user->class }}</td>
                        <td class="btn-group">
                                {{ link_to_route('schedule.create','Create Schedule',[$user->id],['style'=>'border-radius: 0px;','class'=> 'btn btn-success btn-sm']) }}
                                </td>
                    </tr>
                        @endforeach
                        </tbody>
                </table>

现在,当我按创建时,数据确实进入了所需的数据库table,但user_id 被保存为 [ ],我猜这意味着空。非常友好,如果有人可以引导我了解我在这里做错了什么,以及如何保存我为其创建计划的用户的 ID,我将不胜感激。

您应该在商店路由中传递用户 ID。

 Form::open(array('route' => array('route.store', 'id' => $user_id)))

然后在controller中,可以获取id。

 public function create(Request $request) {
      If($request->id){
          $user_id = User::where('id', "=", $request->id)->select ('id')->first();
          return view('createschedule',compact('user_id'));
     }
 }


public function store(Request $request, Schedule $schedule){
    $schedule->user_id = $request->id;
}

编辑视图文件: 确保 $user->id 存在。

<td class="btn-group">
     {{ link_to_route('schedule.create','Create Schedule',['id' => $user->id],['style'=>'border-radius: 0px;','class'=> 'btn btn-success btn-sm']) }}
</td>