Answer the question
In order to leave comments, you need to log in
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');
}
}
<?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');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question