我有一个用户需要填写的表单。它为用户提供了不同的输入标题,内容属于文档表,收件人输入属于用户表。我已根据用户表记录在我的收件人表单中提供了值。
问题:
我需要将用户在我的选择列表中的选择插入 recipients_received_documents (联结表)。以及当前登录的 user_id 。
形式:
<form class = "form-vertical" role = "form" method = "post" action = "{{ route('document.create')}}">
<div class = "form-group">
<label for = "title" class = "control-label">Title:</label>
<input type = "text" name = "title" class = "form-control">
</div>
<div class = "form-group">
<label for = "recipient" class = "control-label">To:</label>
<select onChange = "showUserOptions(this)" name = "recipient[]" multiple class = "form-control" id = "myUserList">
@foreach ($result as $list)
<option value = "{{ $list->id }}">{{ $list->username }}</option>
@endforeach
</select>
</div>
<div class = "form-group">
<textarea id="content" name = "content"></textarea>
</div>
<div class = "form-group">
<button type = "submit" class = "btn btn-primary" onclick="get_editor_content()">Send</button>
</div>
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">
</form>
迁移:
users_table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('password');
$table->string('remember_token');
$table->timestamps();
});
}
documents_table
public function up()
{
Schema::create('documents', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
recipients_received_documents 交汇表。
public function up()
{
Schema::create('recipients_received_documents',function (Blueprint $table)
{
$table->increments('id');
$table->integer('senderUserId')->unsigned();
$table->integer('recipientUserId')->unsigned();
$table->integer('docu_id')->unsigned();
$table->foreign('senderUserId')->references('id')->on('users')->onDelete('cascade');
$table->foreign('recipientUserId')->references('id')->on('users')->onDelete('cascade');
$table->foreign('docu_id')->references('id')->on('documents')->onDelete('cascade');
$table->dateTime('dateReceived')->default(DB::raw('CURRENT_TIMESTAMP'));
});
}
DocumentController:
public function getDocuments()
{
$result = DB::table('users')->where('id', '!=', Auth::id())->get();
return view ('document.create')->with('result', $result);
}
public function postDocuments(Request $request)
{
$this->validate($request,
[
'title' => 'required|alpha_dash|max:255',
'recipient' => 'required',
'content' => 'required',
]);
$title = $request['title'];
$content = $request['content'];
$document = new Document();
$document->title = $title;
$document->content = $content;
$document->save();
return redirect()->back();
}
我在这里有点迷惑我不知道如何将recipient
表单保存到联结表中,因为它属于文档表。任何帮助将不胜感激!
答案 0 :(得分:0)
我认为这个链接可以帮到你,当我需要学习旋转时,这对我很方便:http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/
此外,在您的模型中实际获取当前的Authed用户ID,您可以使用Eloquent Model Events:https://laravel.com/docs/5.2/eloquent#events
recipients_received_documents 模型应添加以下(请注意,如果您的模型位于任何命名空间下,则需要在命名空间声明下方添加use Auth;
)
public static function boot(){
parent::boot(); // <-- Don't forget this
self::creating(function($model){
$model->auth_id = Auth::user()->id;
});
}
希望这有帮助!