Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 试图获得财产';id';非对象Laravel 7.X的定义_Php_Laravel_Eloquent - Fatal编程技术网

Php 试图获得财产';id';非对象Laravel 7.X的定义

Php 试图获得财产';id';非对象Laravel 7.X的定义,php,laravel,eloquent,Php,Laravel,Eloquent,现在我有两个模型:user.php和Trx.php。我创建了一对多关系,如下所示: 用户模型: public function transactions() { return $this->hasMany('App\Trx'); } Trx模型: public function user() { return $this->belongsTo('App\User'); } 这是从Trx.php模型获取过程数据并将数

现在我有两个模型:user.php和Trx.php。我创建了一对多关系,如下所示:

用户模型:

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

 public function user()
    {
        return $this->belongsTo('App\User');
    }
这是从Trx.php模型获取过程数据并将数据发送到视图的控制器方法:

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'));

    }
现在的问题是,当我尝试从模型(即$transactions)访问数据时,除了尝试让用户在blade模板中建立这样的关系外,其他一切都正常工作:

transactions.blade.php

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

请问我做错了什么?任何看到我不是的人请帮忙。提前感谢您的迁移有错误。。。。没有定义外键>

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();
    });
}

如果模型名为
User
,则文件名应为
User.php
(大小写必须匹配)。您还可以为表
用户
trxs
共享您的数据库表结构吗(假设这些是正确的名称),谢谢。刚刚更新了问题并添加了两个表的迁移
user_id
可为空,这是否意味着可能没有与事务关联的用户?在更改表结构并添加外键之后,当我尝试将表添加回数据库并插入其中的数据时,我遇到了以下错误:
\1452-无法添加或更新子行:外键约束失败(`icashers_jemohb`.`sql-86c7_1275989`,约束`trxes_user_id_foreign`外键(`user_id`)REFERENCES`users
我刚刚发现有一个用户的交易被删除了,由于没有设置外键,他所做的交易仍然存在。
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();
    });
}