0

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Nnamdi
  • 101
  • 1
  • 1
  • 7
  • 8
    Nothing here in SO is urgent. – Dez Jan 19 '18 at 11:22
  • 3
    Anyway it looks like you are trying to access to a JSON property without parsing it first, so you still have the JSON as a string. – Dez Jan 19 '18 at 11:23
  • you get the error in blade or controller? – Salar Bahador Jan 19 '18 at 11:26
  • @Dez but I urgently need the upvotes! –  Jan 19 '18 at 11:29
  • @SalarBahador I get the error in the controller – Nnamdi Jan 19 '18 at 11:48
  • @Dez Please explain. How do I do this? Thanks – Nnamdi Jan 19 '18 at 11:49
  • Use `json_decode($json)` where `$json` is your JSON string, so you get an accesible object. More info: https://secure.php.net/manual/en/function.json-decode.php – Dez Jan 19 '18 at 12:36
  • Thanks @Dez - tried this method. Still getting same error. Any other alternative? Thanks – Nnamdi Jan 19 '18 at 13:02
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jan 19 '18 at 16:49
  • Further, there's a number of questions that have solutions to this problem. Have you read them all? https://stackoverflow.com/questions/7625941/call-to-a-member-function-on-a-non-object-works-localhost-but-not-online and https://stackoverflow.com/questions/7627380/trying-to-get-property-of-non-object-php-error and https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – random_user_name Jan 19 '18 at 16:54
  • 1
    Dear @halfer many thanks for pointing that out. And I must sincerely apologise to everyone in the community, as well as thank all who have responded thus far for contributing. – Nnamdi Jan 19 '18 at 16:55

0 Answers0