简体   繁体   中英

Trying to get property 'id' of non-object Laravel 7.X

Now I have two Models: user.php and Trx.php. I have created a one to many relationships as below:

User Model:

public function transactions()
    {
        return $this->hasMany('App\Trx');
    }

Trx Model:

 public function user()
    {
        return $this->belongsTo('App\User');
    }

And this is the controller method for process data from Trx.php model and sending data to the views:

public function purchasedPlan()
    {
        $page_title = 'Purchased Plan';
        $transactions = Trx::where('type', 7)->orderBy('id', 'DESC')->paginate(config('constants.table.default'));
        $empty_message = 'No data found.';
        // dd($transactions);
        return view('admin.reports.transactions', compact('page_title', 'transactions', 'empty_message'));

    }

Now the problem is that when I try accessing the data from the model, that's the $transactions, everything is working well except when I try getting the user in the relationship established like this in the blade template:

transactions.blade.php

<td>  <a href="{{ route('admin.users.detail', $trx->user->id) }}">{{ $trx->user->username }}</a></td>

With that, I get this error:

Error:
[2020-05-24 05:35:40] production.ERROR: Trying to get property 'id' of non-object (View: /home/icashers/public_html/acc/core/resources/views/admin/reports/transactions.blade.php) {"exception":"[object] (ErrorException(code: 0): Trying to get property 'id' of non-object (View: /home/icashers/public_html/acc/core/resources/views/admin/reports/transactions.blade.php) 

What am I doing wrong please? Anyone who's seeing what I am not please help. Thank you in advance

Trx.php Migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTrxesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('trxes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id')->nullable();
            $table->string('amount',50)->nullable();
            $table->string('main_amo',50)->nullable();
            $table->string('charge',50)->nullable();
            $table->string('type',50)->nullable();
            $table->string('title')->nullable();
            $table->string('trx')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('trxes');
    }
}

User.php Migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('mobile')->unique();
            $table->decimal('balance', 11, 2)->default(0);
            $table->string('password');
            $table->string('image')->nullable();
            $table->text('address')->nullable()->comment('contains full address');
            $table->tinyInteger('status')->default(1)->comment('0: banned, 1: active');
            $table->tinyInteger('ev')->default(0)->comment('0: email unverified, 1: email verified');
            $table->tinyInteger('sv')->default(0)->comment('0: sms unverified, 1: sms verified');
            $table->string('ver_code')->nullable()->comment('stores verification code');
            $table->dateTime('ver_code_send_at')->nullable()->comment('verification send time');
            $table->tinyInteger('ts')->default(0)->comment('0: 2fa off, 1: 2fa on');
            $table->tinyInteger('tv')->default(1)->comment('0: 2fa unverified, 1: 2fa verified');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Your migration has an error.... no foreign key defined>

public function up()
{
    Schema::create('trxes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('user_id')->nullable();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('amount',50)->nullable();
        $table->string('main_amo',50)->nullable();
        $table->string('charge',50)->nullable();
        $table->string('type',50)->nullable();
        $table->string('title')->nullable();
        $table->string('trx')->nullable();
        $table->timestamps();
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM