E
E
Eugene2016-05-05 19:14:22
Laravel
Eugene, 2016-05-05 19:14:22

Laravel how to make elegant has many?

There is a model, it has a one-to-many relationship through a global variable model code:

<?php namespace My\Trading\Models;

use Model;

/**
 * Model
 */
class Groups extends Model
{
    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
    ];

    /*
     * Disable timestamps by default.
     * Remove this line if timestamps are defined in the database table.
     */
    public $timestamps = true;

    /**
     * @var string The database table used by the model.
     */
    public $table = 'trading_groups';

    public $hasMany = [
        'statistics' => ['\My\Trading\Models\Statistics', 'key' => 'groups_id'],
    ];
}

I get the result like this:
(new Groups)->hasMany('\My\Trading\Models\Statistics');

But this, in my opinion, is not the most elegant way, I suspect there is a more beautiful solution, otherwise why write a connection to the $hasMany variable in the model? Or does $hasMany only play a role in October CMS?
How to get the connection result from the controller in the most elegant way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-05-05
@beatleboy

I did not work with OctoberCMS, but I suspect that this is their trick to indicate links through $hasMany.
All normal people use like this:

public function statistics()
{
  return $this->hasMany(My\Trading\Models\Statistics::class, 'groups_id')
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question