lundi 10 septembre 2018

pivot table data insert and show in table at Laravel 5.6

I have a meeting table and a user table and the user has different types likehost visitor and admin. I need to store meeting between two types of users host and visitor. So first I need to select the visitor type users and then host type users and host or visitor can be many like 2-3 persons. That's why I am using a meeting_user pivot table to storing multiple host users and visitor users. I have 5 columns in my pivot table and I want to store the userType and status into my pivot table. When creating a meeting and select 2 visitor type users and 2 host types users then how can I display all the information in a table where I can display each meeting with user?? Now I can insert user_id and meeting_id as foreign key but I also want to store userType into the pivot table.

Controller

public function meetingStore(Request $request) {
$this->validate($request,[


        'startDate' => 'required',
        'endDate' => 'required|unique:meetings|max:255',
        'purpose' => 'required' 

    ]);

    $meetings = new Meeting();

    $meetings->vtype_id = $request->vtype_id;
    $meetings->location_id = $request->location_id;
    $meetings->startDate = date("Y-m-d H:i:s", strtotime(str_replace('-', '', request('startDate'))));
    $meetings->endDate = date("Y-m-d H:i:s", strtotime(str_replace('-', '', request('endDate'))));
    $meetings->signInTime = date("Y-m-d H:i:s", strtotime(request('signInTime')));
    $meetings->signOutTime = date("Y-m-d H:i:s", strtotime(request('signOutTime')));
    $meetings->isSignIn = 0;
    $meetings->purpose = $request->purpose;
    $meetings->rfidCard = 0;
    $meetings->createdBy = auth::user()->name;
    $meetings->updatedBy = auth::user()->name;

    $meetings->save();

    $meetings->users()->attach($request->user_id);


    return redirect(route('dashboard'))->with('successMsg','meeting added successfully');

}

Meeting Model

public function users(){

    return $this->belongsToMany('App\User', 'meeting_user')->withPivot('meeting_id', 'user_id','usertype','status');
}

Now if I display the meeting info using $meetings = Meeting::with('users')->get();

<thead>
                        <tr>
                            <th> Visitor Name </th>
                            <th> Host Name </th>
                            <th> Start Date & Time </th>
                            <th> End Date & Time </th>
                            <th> Location </th>

                        </tr>
                    </thead>

<tbody>
                        @foreach ($meetings as $meeting)
                                    @foreach ($meeting->users as $user)


                        <tr>

                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </tbody>

I this case it's showing the username in each row separately but I want to show each meeting in one row based on user types. like

visitor name values Sadek Ahemd, Noor Alam

How can I show the multiple users name based on type host or visitor in one row??



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2O2EDvH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire