【问题标题】:How to passed Query Builder object to textarea?如何将查询生成器对象传递给 textarea?
【发布时间】: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.titledocuments.contentcategories.category_typeusers.username, document_user.dateReceived, documents.id from document_user 内连接 users on users.id = document_user内连接documents documents 987654359 id 987654360 document_id 987654361 categories ON categories 987654364 documents 987654366 sender_id 8087654367 @!= 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


    【解决方案1】:

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

    此外,您可以在 textarea 标记之间使用 $documentList-&gt;content,而不是像 &lt;textarea&gt;{{ $documentList-&gt;content }}&lt;/textarea&gt; 这样的 HTML 属性值

    更新:您必须在查询末尾使用find($id),以便您可以通过 ID 找到特定文档并将其显示在显示页面中。

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

    【讨论】:

    • 嗨!这只是一个&lt;div class = "form-group"&gt; 我已经尝试将{{ $documentLists-&gt;content }} 放在&lt;textarea&gt;&lt;/textarea&gt; 之间,但仍然出现同样的错误。
    • 那行之前还是之后呢?
    • 我在read.blade.php 中发布了所有代码,请参阅我的答案。我使用了find(),但只返回第一条记录。
    • 但您只需要一条记录即可在显示页面上显示数据。如果你使用 get(),你必须遍历记录以在你的视图中使用 foreach 显示它们。
    • 你到底想在 read.blade.php 视图上显示什么?
    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 2016-08-30
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多