当我尝试在我的应用程序上查看页面时,我收到错误
尝试在StatController.php上的第38行获取非对象的属性
研究,这个错误意味着试图获得一个不存在的对象或数据。(待修正)。
但我的问题是,当应用程序的数据库没有任何数据时,联系人页面(查看联系人)也不显示错误,即使联系人表也是空的。
当我尝试在新用户登录时访问主页时,可能导致此错误的原因是什么?
StatController
public function home(Request $request)
{
$surveys = Survey::where('user_id', Auth::user()->id)->orderBy('created_at','DESC')->first();
$respondent = Invite::where('user_id', Auth::user()->id)->where('survey_id', $surveys->id)->count();
$answers = Invite::where('user_id', Auth::user()->id)->where('link_answered', 1)->where('survey_id', $surveys->id)->count();
$yet_to_respond = Invite::where('user_id', Auth::user()->id)->where('link_clicked', 1)->where('survey_id', $surveys->id)->count();
$no_response = Invite::where('user_id', Auth::user()->id)->where('link_clicked', 0)->where('survey_id', $surveys->id)->count();
return view('home', compact('surveys','respondent','yet_to_respond','no_response','answers'));
}
的ContactController
public function index()
{
$contacts = Contact::where('user_id', Auth::user()->id)->get();
return view('contact.index',compact('contact'));
}
答案 0 :(得分:2)
问题是$surveys
返回null,所以你必须在访问某个属性之前检查它:
public function home(Request $request)
{
$respondent = null;
$answers = null;
$yet_to_respond = null;
$no_response = null;
$surveys = Survey::where('user_id', Auth::user()->id)->orderBy('created_at','DESC')->first();
if($surveys){
$respondent = Invite::where('user_id', Auth::user()->id)->where('survey_id', $surveys->id)->count();
$answers = Invite::where('user_id', Auth::user()->id)->where('link_answered', 1)->where('survey_id', $surveys->id)->count();
$yet_to_respond = Invite::where('user_id', Auth::user()->id)->where('link_clicked', 1)->where('survey_id', $surveys->id)->count();
$no_response = Invite::where('user_id', Auth::user()->id)->where('link_clicked', 0)->where('survey_id', $surveys->id)->count();
}
return view('home', compact('surveys','respondent','yet_to_respond','no_response','answers'));
}
另一种方法是使用optional()方法,而不是使用$surveys->id
,您将使用optional($surveys)->id
可选函数接受任何参数,并允许您访问该对象的属性或调用方法。如果给定对象为null,则属性和方法将返回null而不是导致错误:
答案 1 :(得分:1)
由于@Luis为您提供了正确答案,这是改善代码的另一种方法,只需拨打Invite
一次,然后过滤掉。
public function home(Request $request)
{
$surveys = Survey::where('user_id', Auth::user()->id)->orderBy('created_at','DESC')->first();
$invites = collect([]);
if($surveys){
$invites = Invite::where('user_id', \Auth::id())->get();
}
$respondent = $invites->where('survey_id', $surveys->id ?? 0)->count();
$answers = $invites->where('link_answered', 1)->where('survey_id', $surveys->id ?? 0)->count();
$yet_to_respond = $invites->where('link_clicked', 1)->where('survey_id', $surveys->id ?? 0)->count();
$no_response = $invites->where('link_clicked', 0)->where('survey_id', $surveys->id ?? 0)->count();
return view('home', compact('surveys','respondent','yet_to_respond','no_response','answers'));
}
答案 2 :(得分:1)
正如@Luis所提到的,错误是因为您尝试访问null
上的属性,因为如果找不到记录,first
方法会返回null
。
有一种方法是@Luis在答案中提到检查null或使用optional
帮助
要在下面添加答案,请参阅如何使用Eloquent
在Survey
模型中添加以下关系
public function invitations()
{
return $this->hasMany(\App\Invite::class);
}
public function responded()
{
return $this->invitations()
->where('link_answered', 1);
}
public function pending()
{
return $this->invitations()
->where('link_clicked', 1);
}
public function unseen()
{
return $this->invitations()
->where('link_clicked', 0);
}
在StatController
public function home(Request $request)
{
$survey = Survey::withCount(['invitations', 'responded', 'pending', 'unseen'])
->where('user_id', Auth::user()->id)
->orderBy('created_at','DESC')
->first();
return view('home', compact('survey'));
}
然后在您的视图中,您可以使用
获取计数Invitations count: {{ $survey->invitations_count }}
Responded count: {{ $survey->responded_count }}
Pending count: {{ $survey->pending_count }}
Unseen count: {{ $survey->unseen_count }}
此外,如果您愿意,可以稍微清理现有代码。
public function home(Request $request)
{
$respondent = 0;
$answers = 0;
$yet_to_respond = 0;
$no_response = 0;
$survey = Survey::where('user_id', Auth::user()->id)
->orderBy('created_at','DESC')->first();
if ($survey) {
$invitationQuery = Invite::where('user_id', Auth::user()->id)
->where('survey_id', $survey->id);
$respondent = $invitationQuery->count();
$answers = with(clone $invitationQuery)->where('link_answered', 1)
->count();
$yet_to_respond = with(clone $invitationQuery)->where('link_clicked', 1)
->count();
$no_response = with(clone $invitationQuery)->where('link_clicked', 0)
->count();
}
return view('home', compact('survey','respondent','yet_to_respond','no_response','answers'));
}