Search code examples
phplaravelstore

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


To briefly explain my title: I've a table where I'm printing all the users and their information. I've a Create Schedule button on each row. When I click the Create Schedule button, I go to a page where I'll fill out a 7-day weekly schedule of THAT user and then save it. When I save it, I want all the data I just filled out, to save in a separate database table called 'schedules' where I also have a column called 'user_id', which is the ID of the user I'm adding the schedule of. Now, I've achieved most of it except the part where I save the ID of the user whose schedule I'm creating, in the Schedules DB table. Here's my create() & store() functions:

ScheduleController (Resource Controller):

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 (The view with the Schedule Form):

{!! 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 (Where I'm printing all users and passing their respective IDs to the Add Schedule button):

<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, when I press create, the data does go to the desired database table but the user_id is saved as [ ], which I'm guessing means empty. So kindly, if someone can navigate me through what I'm doing wrong here and how I could save the ID of the user I'm creating the schedule for, I'll be thankful.


Solution

  • You should pass the user ID in the store route.

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

    And then in the controller, you can retrieve 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;
    }
    

    Edit view file: Make sure $user->id exist.

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