【发布时间】:2016-11-28 00:46:20
【问题描述】:
我有一个带有数据列表的文档视图,我在这里使用了连接来组合它们。我在此处有一个按钮Read,它将带我进入带有选定行的content 列的文本编辑器。
控制器:
//SHOW
public function showDocuments()
{
$documentLists = DB::table('document_user')->select('documents.title', 'documents.content', 'categories.category_type', 'users.username', 'document_user.dateReceived', 'documents.id')
//Table name //PK //FK
->join('users', 'users.id', '=', 'document_user.sender_id')
->join('documents', 'documents.id', '=', 'document_user.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->where('sender_id', '!=', Auth::id())
->where('user_id', '!=', Auth::id())->get();
//VIEW
return view ('document.show')->with('documentLists', $documentLists);
}
这是所有表记录的所在。
//READ
public function readDocuments($id)
{
$documentLists = DB::table('document_user')->select('documents.title', 'documents.content', 'categories.category_type', 'users.username', 'document_user.dateReceived', 'documents.id')
//Table name //PK //FK
->join('users', 'users.id', '=', 'document_user.sender_id')
->join('documents', 'documents.id', '=', 'document_user.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->where('sender_id', '!=', Auth::id())
->where('user_id', '!=', Auth::id())->get();
return view ('document.read')->with('documentLists', $documentLists);
}
这是将我带到document.read 视图的地方,该视图为我带来了文本编辑器,以显示当前内容的视图。如您所见,我还在选择查询中包含'documents.id' 以获取当前文档的ID。
查看:
<table class = "table">
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Category</th>
<th>Sender</th>
<th>Date Received</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($documentLists as $list)
<tr class = "info">
<td>{{ $list->title }}</td>
<td>{{ strip_tags(substr($list->content, 0, 50)) }} {{ strlen($list->content) > 50 ? "..." : '' }}</td>
<td>{{ $list->category_type }}</td>
<td>{{ $list->username }}</td>
<td>{{ date('M j, Y', strtotime($list->dateReceived)) }}</td>
<td>
<a href = "{{ route('document.read', $list->id) }}"><button type = "submit" class = "btn btn-info">Read</button></a>
</td>
</tr>
@endforeach
</tbody>
</table>
read.blade.php
<form class = "form-vertical">
<div class = "col-md-8">
<div class = "form-group">
<textarea id = "content" value = "{{ $documentList->content }}"></textarea>
</div>
</div>
<form>
我不再发布tinymce 的脚本了。我只是想在 textarea 中显示基于 selected 的 content 列。
路线:
Route::get('/read/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController@readDocuments',
'as' => 'document.read',
'middleware' => 'auth',
]);
但不幸的是它给了我一个错误。
be7388ad5369ce939d52cb53ac5f3a4674217011.php 第 34 行中的 ErrorException:尝试获取非对象的属性(查看:C:\Users\JohnFrancis\LaravelFrancis\resources\views\document\read.blade.php)
但在我的 URL 中,它会打印出所选行的正确 ID。任何帮助或提示我该如何解决这个问题?任何帮助将不胜感激!
更新
在函数readDocuments($id) 上将我的get() 更改为find() 方法后。这有效,它将我的值打印到 textarea 但只有第一条记录。我尝试在find($id) 中添加$id,但返回错误。
SQLSTATE[23000]:完整性约束违规:1052 where 子句中的列“id”不明确(SQL:选择
documents.title、documents.content、categories.category_type、users.username,document_user.dateReceived,documents.idfromdocument_user内连接usersonusers.id=document_user内连接documentsdocuments987654359id987654360document_id987654361categoriesONcategories987654364documents987654366sender_id8087654367 @!= 13和user_id!= 13 和id= 137 限制 1)
数据库图:
更新 2:
document_user 迁移
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'));
});
}
文档迁移
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();
});
}
用户迁移
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();
});
}
类别迁移
public function up()
{
Schema::create('categories', function (Blueprint $table)
{
$table->increments('id');
$table->string('category_type');
$table->timestamps();
});
}
型号
用户:
public function documents()
{
return $this->belongsToMany('App\Models\Document', 'document_user', 'user_id', 'document_id');
}
文档:
public function categories()
{
return $this->belongsTo('App\Models\Category');
}
public function recipients()
{
return $this->belongsToMany('App\Models\User', 'document_user', 'document_id', 'user_id');
}
类别:
public function userDocuments()
{
return $this->hasMany('App\Models\Document');
}
【问题讨论】:
标签: php laravel laravel-5.2 query-builder