如何将Query Builder对象传递给textarea?

时间:2016-07-24 17:33:05

标签: php laravel laravel-5.2 query-builder

我有一个带有数据列表的文档视图,我在这里使用连接来组合它们。我在这里有一个按钮阅读,它将带我到带有所选行content列的文本编辑器。

SC

控制器:

//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视图的地方,这个视图为我带来了文本编辑器,向我展示了当前内容的视图。如您所见,我还在select查询中包含'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中显示基于所选的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)

但是在我的网址中,它会打印出所选行的正确ID。任何帮助或提示如何解决这个问题?任何帮助将不胜感激!

更新

在功能get()上更改find()readDocuments($id)方法后。这样可以将值打印到textarea,但只打印第一条记录。我尝试在$id中添加find($id),但是返回错误。

  

SQLSTATE [23000]:完整性约束违规:1052列&#39; id&#39; where子句不明确(SQL:选择documentstitledocumentscontentcategoriescategory_typeusersusernamedocument_userdateReceiveddocumentsid来自document_user内部加入users usersid上的document_user = sender_iddocuments内部加入documentsid = document_userdocument_id内部加入categories上的categoriesid = documentscategory_id其中sender_id!= 13和user_id!= 13和{{ 1}} = 137限制1)

数据库图:

enter image description here

更新2:

document_user migration

id

文件迁移

 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');
}

1 个答案:

答案 0 :(得分:1)

read.blade.php文件中第34行的内容是什么?它是<textarea id = "content" value = "{{ $documentList->content }}"></textarea>吗?那里有一个变量,它可能返回null或不是对象。

此外,您可以在textarea标记之间使用$documentList->content,而不是像<textarea>{{ $documentList->content }}</textarea>

这样的值HTML属性

更新:您必须在查询结束时使用find($id),以便按ID查找特定文档并将其显示在显示页面中。

更新2:在$document = Document::with('category', 'users')->find($id);方法中使用show($id),并将$ document变量传递给视图。