mercredi 24 janvier 2018

In Laravel, how to set multiple hasMany (1 to many) relationships on the same record simultaneously?

I'm making an appointment app, a user can create an appointment at a certain date and time with another user. I have a users table and an appointments table. Each appointment has an initiator ID (the user who suggested the appointment) and a recipient ID (the user who receives the appointment request), and the models have hasMany relationships (code below)

My question is, when I create an appointment, how do I get Eloquent to associate BOTH the initiator AND the recipient with the new appointment. Looking at the syntax in the docs, I can easily do e.g. just the initiator, like so:

$initiator = User::find(Auth::user()->id); // Get user from DB who is the initiator
$appt = $initiator->appts_initiator()->create(['address' => $request->input('address'), 'whendatetime' => $request->input('whendatetime')]);

or I could do just the recipient:

$recipient = User::where('email', $request->input('email'))->first(); // Get recipient user
$appt = $recipient->appts_recipient()->create(['address' => $request->input('address'), 'whendatetime' => $request->input('whendatetime')]);

During that line with create() in it, I need to get Eloquent to associate both initiator and recipient. Or do I have to manually inject the correct ID as one of the parameters in the create(), which would seem to bypass the point of Eloquent!

Relevant model code:

class User extends Authenticatable
{
    protected $fillable = ['name', 'email', 'password'];

    // Get all of the user's appointments where they are an initiator:
    public function appts_initiator()
        {
            return $this->hasMany('App\Appointment', 'initiator_id');
        }

    // Get all of the user's appointments where they are a recipient:
    public function appts_recipient()
        {
            return $this->hasMany('App\Appointment', 'recipient_id');
        }
}

class Appointment extends Model
{
    protected $fillable = array('whendatetime', 'address', 'minuteslate');

    // Get user who is the initiator for this appointment:
    public function initiator_user()
        {
            return $this->belongsTo('App\User', 'initiator_id');
        }

    // Get user who is the recipient for this appointment:
    public function recipient_user()
        {
         return $this->belongsTo('App\User', 'recipient_id');
        }

    // Get the payment for this appointment:
    public function get_payment()
        {
            return $this->hasOne('App\Payment'); // Default foreign key (appointment_id)
        }
}

Relevant bit of user table:

$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');

and appointments table:

$table->increments('id');
$table->integer('initiator_id')->unsigned(); // User who initiated to appointment
$table->integer('recipient_id')->unsigned(); // User who receives the appointment request
$table->dateTime('whendatetime'); // Date and time of appointment
$table->string('address'); // Address of the appointment

Thanks for any suggestions. Alex



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire