I am new to Laravel. And I have been following a lot of questions and responses/answers regarding the above mentioned error, but I am yet to get a hang of it all.
For my app, when I log in with registered user and try to view my profile, I get the error:
ERROR: Trying to get property of non-object {"userId":8,"email"...
Please see below some code snippet from my ProfileController.php:
namespace App\Http\Controllers;
use App\Models\SmsVerificationToken;
use App\Notifications\SmsPhoneNumberVerification;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Session;
class ProfileController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('read-news');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$item = auth()->user();
if(!$item->profile){
return redirect()->to('/dashboard');
}
$current_transaction = $item->profile->current_transaction;
if(!$current_transaction->is_complete){
flash('You can not update your profile while you still have an incomplete transaction.')->error();
return redirect()->to('/dashboard');
}
return view('profiles.index',compact('item'));
}
As well as my profile views blade:
@extends('layouts.app')
@section('content')
@component('dashboard.frame')
<div class="panel panel-default">
<div class="panel-heading">
<h3>
Profile
</h3>
<p>{{ $item->email }}</p>
</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="/profile">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name',$item->name) }}" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="phone_number" class="col-md-4 control-label">Phone Number</label>
<div class="col-md-6">
<input id="phone_number" type="text" class="form-control" name="phone_number" value="{{ old('phone_number',$item->phone_number) }}" required>
@if ($errors->has('phone_number'))
<span class="help-block">
<strong>{{ $errors->first('phone_number') }}</strong>
</span>
@endif
</div>
</div>