我在运行此代码时遇到问题:
//DashboardController
public function getStream()
{
$user = Sentry::getUser();
$userid = $user->id;
$convs = TBMsg::getUserConversations($userid);
$getNumOfParticipants = $convs->getNumOfParticipants();
$participants = $convs->getAllParticipants();
$lastMessage = $convs->getLastMessage();
$senderId = $lastMessage->getSender();
$content = $lastMessage->getContent();
$status = $lastMessage->getStatus();
$posts = Post::whereIn('user_id', function($query) {
$query->select('follow_id')
->from('user_follows')
->where('user_id', '1');
})->orWhere('user_id', '1')->get();
return View::make('stream', array('getNumOfParticipants' => $getNumOfParticipants,
'participants' => $participants,
'lastMessage' => $lastMessage,
'senderId' => $senderId,
'content' => $content,
'status' => $status
))->with('posts', $posts)->with('convs', $convs);
}
}
我收到此错误:调用未定义的方法Illuminate \ Support \ Collection :: getNumOfParticipants()
答案 0 :(得分:0)
$convs
是一个集合,因此您无法在其上调用仅存在于单个模型上的方法。就像软件包的教程一样,你必须遍历集合才能使用该函数
From the how to:
foreach ( $convs as $conv ) {
$getNumOfParticipants = $conv->getNumOfParticipants();
$participants = $conv->getAllParticipants();
/* $lastMessage Tzookb\TBMsg\Entities\Message */
$lastMessage = $conv->getLastMessage();
$senderId = $lastMessage->getSender();
$content = $lastMessage->getContent();
$status = $lastMessage->getStatus();
}
答案 1 :(得分:0)
将->get()
替换为->first()
,因为您现在基本上返回了一组数组,但是您需要这样做。
$query->select('follow_id')
->from('user_follows')
->where('user_id', '1');
})->orWhere('user_id', '1') // ->get() is removed
->first();