繁体   English   中英

我可以使用另一个M:M实体创建自定义数据透视表名称吗?

[英]Can I create custom pivot table name using another M:M entity?

我仍然在想如何创建自定义数据透视表名称? 因为我创建了一个文档,users表与document_user有多对多的关系, document_user是我的数据透视表,并且该表是为用户创建的已接收文档创建的。 而且我打算为文档用户创建另一个数据透视表,这个表用于发送文档,因此我可以拥有历史记录。 请参阅下面的代码。

create_document_user_table

public function up()
 {
    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');

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

documents_table

public function up()
{
    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();
    });
}

users_table

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('middle_name');
        $table->string('email');
        $table->string('username');
        $table->string('address');
        $table->string('password');
        $table->string('remember_token');

        $table->integer('role_permission_id')->unsigned();

        $table->foreign('role_permission_id')->references('id')->on('roles_permissions_dt')->onDelete('cascade');
        $table->timestamps();
    });
}

这可以很好地将记录插入到数据透视表中。 我计划实现的是每次我插入文档的记录,这些记录也将插入到我的自定义数据透视表中,而不仅仅是在我的document_user数据透视表中。 任何帮助将不胜感激! 感谢您的信息或提示。

UPDATE

@Mina感谢您提供的提示,但实际上这是我的数据透视表的插入或保存。 如何在我的修订表中插入它?

DocumentController

public function postDocuments(Request $request)
{

    $this->validate($request,
    [
        'title' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/|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();
    $user = Auth::user();

    foreach($request->recipient_id as $recipientId)
    {
        $document->recipients()->sync([ $recipientId => ['sender_id' => $user->id]],false );
    }

    return redirect()->back();  
}

您不需要另一个数据透视表。 你需要一个这样的表:

public function up()
{
    Schema::create('revisions', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('document_id')->unsigned();
        //Rest of table structure

        $table->foreign('document_id')->references('id')->on('document_user')->onDelete('cascade');
        $table->timestamps();
    });
}

当您需要创建新版本时:

$document = new Document;
//add $document attributes
$user->documents()->save($document);

$document->recipients->each(function($recipient){
      $id = $recipient->pivot->id;
      Revision::create(['document_id'=>$id]);
})

您可以根据需要调用数据透视表。

如前所述,为确定关系连接表的表名,Eloquent将按字母顺序连接两个相关的模型名称。 但是,您可以自由地覆盖此约定。 您可以通过将第二个参数传递给belongsToMany方法来执行此操作:

return $this->belongsToMany('App\\Role', 'user_roles');

雄辩:关系#很多人

在您的情况下,您需要定义这样的关系:

class User extends Model
{
    // other stuff

    createdDocuments()
    {
        return $this->belongsToMany('App\Document', 'document_user_created');
    }

    sentDocuments() // or receivedDocuments()
    {
        return $this->belongsToMany('App\Document', 'document_user_sent');
    }

    // other stuff
}


class Document extends Model
{
    // other stuff

    createdByUsers()
    {
        return $this->belongsToMany('App\User', 'document_user_created');
    }

    sentToUsers() // or sentFromUsers() or whatever it does mean
    {
        return $this->belongsToMany('App\User', 'document_user_sent');
    }

    // other stuff
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM