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

问题描述 投票:-1回答:1

为了简要说明我的标题:我有一个表格,在其中打印所有用户及其信息。我在每行上都有一个Create Schedule按钮。当我单击“创建计划”按钮时,我将转到一个页面,该页面将填写该用户的7天每周计划,然后将其保存。保存时,我希望将刚刚填写的所有数据保存在一个名为“ schedules”的单独的数据库表中,其中还有一个名为“ user_id”的列,这是我要添加时间表的用户的ID的。现在,除了将要创建其日程表的用户的ID保存在Schedules DB表中的那一部分之外,我已经实现了大多数功能。这是我的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() !!}

Dashboard View(在这里打印所有用户并将他们各自的ID传递到Add Schedule按钮):

<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>

Now,当我按create时,数据确实进入了所需的数据库表,但是user_id被保存为[],我猜这意味着是空的。因此,如果有人可以浏览我在这里做错的事情以及如何保存为其创建日程表的用户的ID,我将非常感激。

php laravel store
1个回答
0
投票
Form::open(array('route' => array('route.store', $user_id)))

然后在控制器中,您将检索id。

 public function store(Request $request, Schedule $schedule, $user_id) {

     $schedule->user_id =  $user_id:
 }

 public function create($id) {
      If($id){
          $user_id = User::where('id',"=",$id)->select ('id')->first();
          return view('createschedule',compact('user_id'));
     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.