我有一个发送群组电子邮件的功能,但我需要访问特定群组的联系人列表。我已经尝试过 EagerLoading 和链接,但似乎无法使其正常工作。所以在sendCommunications()
函数中,这条线引起了我的问题:
$contacts = $message->group->contact()->select(array('email_address'));
我从命令行收到的错误消息是:
Call to undefined method Illuminate\\Database\\Eloquent\\Collection::contact()
这就是我到目前为止所拥有的:
控制器功能:
public static function sendCommunications()
{
$queued_messages = QueuedMessage::with(array('message' => function($query)
{
$query->where('distribution_date_time', '<=', DB::raw('NOW()'));
}))->get();
foreach ($queued_messages as $queued)
{
$message = $queued->message;
$contacts = $message->group->contact()->select(array('email_address'));
foreach ($contacts as $contact)
{
Message::send($message->email_title.'\n'.$message->email_body, $contact->email_address, $message->department->short_name);
}
$queued->delete();
}
}
楷模:
排队消息:
class QueuedMessage extends Eloquent
{
protected $primaryKey = 'queued_message_pk';
protected $table = 'queued_messages';
public $timestamps = false;
public function message()
{
return $this->belongsTo('Communication', 'message_fk');
}
}
沟通:
class Communication extends Eloquent
{
protected $primaryKey = 'message_pk';
protected $table = 'messages';
public $timestamps = false;
public function establishment()
{
return $this->belongsTo('Establishment', 'establishment_fk');
}
public function group()
{
return $this->belongsToMany('Group', 'messages_groups', 'message_fk', 'group_fk');
}
}
团体:
class Group extends Eloquent
{
protected $primaryKey = 'group_pk';
protected $table = 'groups';
public $timestamps = false;
public function department()
{
return $this->belongsTo('Department', 'department_fk');
}
public function contact()
{
return $this->belongsToMany('Contact', 'contacts_groups', 'group_fk', 'contact_fk');
}
public function message()
{
return $this->belongsToMany('Communication', 'messages_groups', 'group_fk', 'message_fk');
}
}
接触:
class Contact extends Eloquent
{
protected $primaryKey = 'contact_pk';
protected $table = 'contacts';
public $timestamps = false;
public function department()
{
return $this->belongsTo('Department', 'department_fk');
}
public function group()
{
return $this->belongsToMany('Group', 'contacts_groups', 'contact_fk', 'group_fk');
}
}