我目前正在努力学习如何从特征中返回变量,并且应该在数组中返回以在控制器中使用: 性状:
public function getAllData($search)
{
if ($search->search == null) {
$search->search = '#technology';
}
$cb = new Codebird();
$cb->setConsumerKey(env('TwitterKey'), env('TwitterSecret'));
$cb->setToken(env('AccessToken'), env('AccessTokenSecret'));
//https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
//https://dev.twitter.com/docs/api/1.1/get/search/tweets
$params = [
'q' => $search->search,
'lang' => 'en',
'count' => '5',
];
$reply = (array)$cb->search_tweets($params);
$data = (array)$reply['statuses'];
$s = count($reply['statuses']);
return [
'data' => $data,
's' => $s,
];
控制器: 公共功能TwitterData(请求$ search){
$data = $this->getAllData($search);
return view('front.search', compact('data'));
}
它目前给我一个关于使用该对象的错误,但是我无法访问该数据'在数组中
错误: 试图获取非对象的属性(查看:C:\ xampp \ htdocs \ TwitterProject \ resources \ views \ front \ search.blade.php)
答案 0 :(得分:1)
您正在getAllData
方法上返回一个数组,但您可能尝试将其作为View上的对象进行访问:
WRONG:
{!! $data->data !!}
RIGHT:
{!! $data['data'] !!}