D
D
Dos7712018-01-28 10:06:38
Laravel
Dos771, 2018-01-28 10:06:38

Laravel relation how to check?

Hello. Can you please tell me how can I link tables to each other through relations (Relationship)? For the third day I have not been able to correctly implement the relationship and check it through the dd () function.
For example, I need to bind from the users table to the documents table
1) In the User model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    public function document()
    {
        return $this->hasOne('App\Document');
    }

}

2) I create a migration and a Documents - Document model
<?php

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

class DocumentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::create('documents', function (Blueprint $table) {
            $table->increments('id');
            $table->string('user_id');
            $table->string('title');
            $table->timestamps();
        });


    }

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

    }
}


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Document extends Model
{

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

    }

}

3) Now how should I display information about the user id in the document i.e. user_id ?
4) As far as I understand, I need to create a controller DocumentController?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question