Laravel创建和检索数据以实现简单的关系

in laravel i want to create simple relation ship to retrieve data between them, but i cant and i get error

i have two table as amount_repositories and report_transactions, report_transactions table has many fields in amount_repositories table, and amount_repositories belongs to report_transactions,

amount_repositories is separated data from report_transactions table, then amount_repositories belongs to report_transactions.

ReportMerchantTransactions class:

class ReportMerchantTransactions extends Model
{
    protected $table    = 'report_transactions';
    public function amount_repositories()
    {
        return $this->hasMany('App\AmountRepositories');
    }
}

AmountRepositories class:

class AmountRepositories extends Model
{
    protected $table = 'amount_repositories';
    public function report_merchant_transactions()
    {
        return $this->belongsTo('App\ReportMerchantTransactions');
    }
}

AmountRepositories migration file:

class AmountRepositories extends Migration
{
    public function up()
    {
        Schema::create('amount_repositories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('amount');
            $table->integer('report_id')->unsigned();
            $table->foreign('report_id')->references('id')->on('report_transactions');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('amount_repositories');
    }
}

now i want to get all data in AmountRepositories that belongs to ReportMerchantTransactions:

$posts= ReportMerchantTransactions::find(1)->amount_repositories;

unfortunately i get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown     column'
amount_repositories.report_merchant_transactions_id' 
in 'where     clause' 
(SQL: select * from `amount_repositories` where 
    `amount_repositories`.`report_merchant_transactions_id` = 1 and 
`amount_repositories`.`report_merchant_transactions_id` is not null)

Likely you need to pass additional parameters into the belongsTo() or hasMany() methods.

By default, Laravel will assume that the foreign key linking the tables is the current table's name with _id concatenated to it. The error points you in that direction as well; notice that it says it cannot locate a column named amount_repositories.report_merchant_transactions_id which is the default foreign key generated by Laravel. Since it seems your foreign key does not follow that convention, which is totally fine, Laravel offers additional parameters to be passed to it to help this.

The concept is: return $this->hasMany('App\Comment', 'foreign_key', 'local_key'); pulled from docs.

Not positive of your model layout, but something along these lines should get you going.

class AmountRepositories extends Model
{
    protected $table = 'amount_repositories';

    /**
     * Pass the FK along with method
     */
    public function report_merchant_transactions()
    {
        return $this->belongsTo('App\ReportMerchantTransactions', 'report_id');
    }
}

This says that the AmountRepositories table has a relationship with ReportMerchantTransactions using foreign key of report_id.

Hope this helps. You're close, just need to add a little more data to your relationship data and you're there!