I get this error Trying to get property of non-object How to fix it

0 votes
I have two tables, users and grades.

users table

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('grade_id')->nullable();
        }
     }

grades table

    public function up()
    {
        Schema::create('grades', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

 I save id of grade with json_encode,

[![json][1]][1]

I save in field like this

        $user->grade_id = json_encode($request->input('grade_id'));

I want to return the grade name but I did not solve my problem

Controller

    public function index()
    {
        $grades = Grade::all();
        return view('index', compact('grades');
    }

User.php

    public function grade()
    {
        return $this->belongsTo(Grade::class);
    }

index.blade.php

    @if(isset(auth()->user()->grade_id)  && !empty(auth()->user()->grade_id))
        <?php
        $grade = json_decode(auth()->user()->grade->name, true);
        @foreach($grade as $key=>$item)
            <li>
                <div class="grade">{{ $grade[$key] }}</div>
            </li>
        @endforeach
        ?>
    @endif

I get this errpr

> Trying to get property of non-object
Aug 16, 2020 in Laravel by aakash
• 230 points
24,026 views

1 answer to this question.

0 votes

Hello @aakash,

If you want to save grades as json then don't save it as foreign key in users tables. 

In users tables you can use grade as json column which does as your expectations. 

Hope it helps!!

Thank you!!

answered Aug 17, 2020 by Niroj
• 82,840 points
Hello niroj

Thanks for this solution man.....much appreciated

Can I use another table as 'user_grades' and then use grade_id and user_id as columns?

Hello @aakash,

I understand your consern so I highly recommend you to use another table as 'user_grades' and then use grade_id and user_id as columns
In future if you want to search or analyse any users having some grade then it might be difficult with JSON column unless your very familiar with JSON columns

if I run php artisan serve my app works fine
but if I try to access http://localhost/projects/laravel/public/ it doesn't work How to fix this?

Hello @aakash,

Laravel loads from the  public folder so localhost is already public by default.

Yeah I also try it but doesn't work

I think the problem is the .htaccess try to toggle around this. Hope it works now

Sure that can be other way can you look this snippet once which is causing the error

run:

php artisan cache:clear

ohh yes...it worked thanxx man for help

Related Questions In Laravel

0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2, 2020 in Laravel by Niroj
• 82,840 points
12,109 views
0 votes
1 answer

How to make Django serve that file for download as opposed to trying to find a URL and View to display it?

Hello @kartik, You can just use the built ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,840 points
6,512 views
0 votes
1 answer

How can I get the error message for the mail() function?

Hello @kartik, You can use error_get_last() when mai l()returns false. $success = mail('example@example.com', ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,840 points
9,598 views
0 votes
1 answer

How to get current path of a Request with its query parameters?

Hello @kartik, Try to use the following: \Request::getRequestUri() Hope this ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
2,027 views
0 votes
2 answers

Laravel 5.4 to get data form API response

Input::get('var_name') READ MORE

answered Feb 14, 2019 in Blockchain by anonymous
1,888 views
0 votes
1 answer

How to exclude specific folders every time I run 'eb deploy' using AWS EB CLI

If there is not .ebignore file it ...READ MORE

answered Oct 24, 2018 in AWS by Priyaj
• 58,020 points
2,097 views
0 votes
1 answer

How to get distinct values for non-key column fields in Laravel?

Hello @kartik In eloquent you can use this $users ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,840 points
22,809 views
0 votes
1 answer

How to get a list of registered route paths in Laravel?

Hello, Route::getRoutes() returns a RouteCollection. On each element, you can ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,840 points
3,787 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP