S
S
stdio962017-01-15 18:37:44
Laravel
stdio96, 2017-01-15 18:37:44

Does Laravel have auto-creation of relationships in models?

Hello. In migrations in Laravel, you can specify a secondary key (foreign key). There are 2 questions:

  1. Is it possible to create 2 secondary keys. What for? For example, there is a task and it has the one who added it and the one who performs it. Now I do it like this: in App\Task
    public function developerr()
        {
            return $this->belongsTo('App\User', 'developer');
        }
    
        public function senderr()
        {
            return $this->belongsTo('App\User', 'sender');
        }

    And I do not specify anything in App\User. At the same time, everything works. But I have doubts whether such a solution is optimal.
  2. After specifying the secondary key in the migrations, is it possible to simply write the
    migration in the models
    ...
                $table->foreign('sender')->references('id')->on('users');
                $table->foreign('developer')->references('id')->on('users');

    somewhere in the controller
    $task = \App\Task::find(1);
    $task->developer->name;
    or for this it is necessary to prescribe relationships as above?

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Semenko, 2017-01-15
@abler98

There is so much "magic" in Laravel, do you need to do something yourself, or are you just too lazy?
Answer: you need to prescribe all the dependencies yourself, this is the optimal solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question