mercredi 1 juin 2016

how to use "belongTo" relationship in larvael

i have two models 'users' and 'doctors' i want have used eloquent relatonahiops hasMany and belongaTo to relate them as below

This is the model for users
   protected $fillable = [
    'name',
    'email',
    'password',
    'role_id',
    'active_user',
];

public function doctor()
{
    return $this->hasMany('App\Doctor','user_id');
}

and the model for doctors is below protected $table="doctors";

protected $fillable = [
    'role_id',
    'user_id',
    'name',
    'date',
    'gender',
    'specialization',
    'state',
    'country',
    'email',
    'mobile',
    'phone',
    'address',
    'password',
    'pic'

];
public function user()
{
    return $this->belongsTo('User','user_id');
}

now i have a table where i display the doctors using @foreach statement below

     <table id="example" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                                <thead >
                                <tr >
                                    <th>
                                        S/N
                                    </th>
                                    <th>
                                        Picture
                                    </th>

                                    <th style="text-align: center;">
                                        Name
                                    </th>

                                    <th style="text-align: center;">
                                        Specialization
                                    </th>

                                    <th  style="text-align: center;">
                                        Phone Number
                                    </th>

                                    <th  style="text-align: center;">
                                        Status
                                    </th>


                                    <th style="text-align: center;">
                                        Book
                                    </th>
                                    
                                    
                                    

                                </tr>
                                </thead>
                                <tbody>
                                <?php $c=0;?>
                                @foreach($doctors as $dr)
                                    <tr style="padding: 10px; text-align: center;">
                                        <td></td>
                                        <td>
                                            <img src='' style="height:40px; width:40px;" class="img-circle">
                                        </td>
                                        <td style="padding: 20px;"></td>
                                        <td style="padding: 20px; "></td>
                                        <td style="padding: 20px;"></td>
                                        <td style="padding: 20px;">

                                        </td>
                                        <td style="padding: 20px;">
                                            <input type="radio" name="book" value="">

                                        </td>
                                       This is where i want to show if they are offline or online
                                    </tr>
                                @endforeach


                                </tbody>
                            </table>

My Controller is below

public function BookDoctors(){
    if(!Auth::check()) {
        return Redirect::to('admin/login');
    }
    $opt=User::findOrFail(Auth::User()->id)->operator->first();
    $doctors = Doctor::all();
    $patients = PatientInfo::all();
    return view('Others.Operator.Appointment.book_doctors')->with(compact('opt','doctors','patients'));

}

The Challenge now is that i want to get the "active_user" field from the users migration and include it the form that has a table head of "status" using belongsTo relationship in laravel



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire