1

我有一个发送群组电子邮件的功能,但我需要访问特定群组的联系人列表。我已经尝试过 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');
    }
}
4

2 回答 2

0

如果你注意到你的 Eloquent 类Communication,它使用了一个belongsToMany关系。当你调用$message->group这将返回一个Collection组。

你可以试试

$message->group->each(function($group) use($message){
   $group->contact()->get('email_address')->each(function($contact) use($message){
          Message::send($message->email_title.'\n'.$message->email_body, $contact->email_address, $message->department->short_name);
   });
});

Eloquent Collection 提供了一个each函数来循环遍历集合,该函数将回调作为参数来对集合执行附加操作。希望这可以帮助

于 2013-08-14T12:30:27.823 回答
0

这将是由于group()返回一个集合,即一组结果,而不是单个结果。

您不能对一组项目进行方法调用。

相反,您将能够遍历返回的项目group并依次调用contact它们中的每一个,ala

$contacts = array();
foreach($message->group as $group){
    merge_array($contacts, $group->contact()->select(array('email_address')));
}
于 2013-08-14T05:18:42.880 回答