如何在与一个主键链接的数据透视表上创建两个外键

时间:2016-07-22 16:34:03

标签: php laravel eloquent laravel-5.2 pivot-table

我正在尝试实施我需要插入保存当前登录用户的位置。将收件人插入user_id列效果很好,但我需要操纵谁发送我需要获取用户ID的数据。我有两个表用户文档,其中包含数据透视表 document_user

用户

 Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email');
        $table->string('username');
        $table->string('password');
        $table->string('remember_token');
 });

文件

Schema::create('documents', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('content');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->timestamps();
    });

document_user - 数据透视表

Schema::create('document_user',function (Blueprint $table)
    {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('document_id')->unsigned();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('document_id')->references('id')->on('documents')->onDelete('cascade');
        $table->dateTime('dateReceived')->default(DB::raw('CURRENT_TIMESTAMP'));
    });

数据库设计:

DB

  

请注意!我只在用户表格迁移中插入了一些列,只是为了保存一行文字。

模型

用户

public function documents()
{
    return $this->belongsToMany('App\Models\Document', 'document_user', 'user_id', 'document_id');
}

文档

public function recipients()
{
    return $this->belongsToMany('App\Models\User', 'document_user', 'document_id', 'user_id');
}

根据用户对数据透视表的选择插入记录效果很好。但是当我尝试回滚我的迁移并将我的数据透视表更改为此时。

$table->integer('sender_id')->unsigned();
$table->foreign('sender_id')->references('id')->on('users')->onDelete('cascade');

我说错了:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`webdev`.`document_user`, CONSTRAINT `document_user_sender_id_foreign` FOREIGN KEY (`sender_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: insert into `document_user` (`document_id`, `user_id`) values (34, 10))

如何在我的数据透视表中插入当前用户?所以我可以跟踪谁发送和接收数据。任何帮助将不胜感激!干杯!

更新1:

感谢@Dastur解决我的问题。

document_user

Schema::create('document_user',function (Blueprint $table)
    {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('document_id')->unsigned();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('document_id')->references('id')->on('documents')->onDelete('cascade');

        $table->unsignedInteger('sender_id')->nullable();
        $table->foreign('sender_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');


        $table->dateTime('dateReceived')->default(DB::raw('CURRENT_TIMESTAMP'));
    });

我很难获得当前用户的ID并将其插入sender_id列。仍然没有任何想法这样做,因为我需要跟踪用户创建的文档。

DocumentController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Document;
use App\Models\User;
use DB;
use Auth;

class DocumentController extends Controller
{

public function getDocuments()
{
    //GETTING ALL THE ID OF THE USERS IN THE DATABASE EXCEPT THE ID OF CURRENT USER.
    $resultRecipient = DB::table('users')->where('id', '!=', Auth::id())->get();


    //GETTING ALL THE CATEGORIES.
    $resultCategory = DB::table('categories')->get();

    //VIEW
    return view ('document.create')->with('resultRecipient', $resultRecipient)->with('resultCategory', $resultCategory);
}

public function postDocuments(Request $request)
{

    $this->validate($request,
    [
        'title' => 'required|alpha_dash|max:255',
        'content' => 'required',
        'category_id' => 'required',
        'recipient_id' => 'required',
    ]);


    $document = new Document();
                                //Request in the form
    $document->title = $request->title;
    $document->content = $request->content;
    $document->category_id = $request->category_id;

    $document->save();
    $document->recipients()->sync($request->recipient_id, false);

    return redirect()->back();  
}

}

更新2:根据@Dastur,我需要为我的数据透视表创建另一个模型document_user

UserDocument(Model)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserDocument extends Model
{
protected $table = 'document_user';
}

1 个答案:

答案 0 :(得分:2)

我在网上看了一下,在laracasts上发现了这篇文章:http://laravel.io/forum/09-18-2014-foreign-key-not-saving-in-migration。此外,当您尝试从另一个表中获取空值并将其放入不可为空的行时,通常会抛出此错误。

编辑:

你在这里做的很奇怪,我仍然认为数据透视表不是一个明智的选择。这正是我要做的:

迁移:

首先,我会创建用户迁移,这很简单:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('email');
    $table->string('username');
    $table->string('password');
    $table->rememberToken();
});

接下来我将创建我的文档表:

Schema::create('documents', function(Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->text('content');
    $table->integer('category_id');
    $table->integer('user_id');
    $table->integer('sender_id');
    $table->dateTime('dateRecieved')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->timestamps();
});

模型:

在您的用户和类别模型中,您需要以下方法:

public function documents() {
    $this->hasMany(/* Path to your document model */);
}

最后,在您的文档模型中,您需要以下方法:

public function category() {
    $this->belongsTo(/* Path to your category model */);
}

public function user() {
    $this->belongsTo(/* Path to your user model */);
}

文件控制器

public function postDocuments(Request $request)
{

    $this->validate($request,
    [
        'title' => 'required|alpha_dash|max:255',
        'content' => 'required',
        'category_id' => 'required',
        'recipient_id' => 'required',
    ]);

    /*
     * This part not exactly sure what to do, because of I don't know what
     * know what the difference is between the sender and the user, please
     * elaborate.
     */

}