【问题标题】:Sorting of columns by joining two tables in Laravel通过在 Laravel 中连接两个表来对列进行排序
【发布时间】:2020-04-12 12:02:34
【问题描述】:

我有两个表书和出版商。 publisher_id 是publishers 表中的主键和books 表中的外键。在书籍的 index.blade.php 中,我想创建用于对书籍索引列进行排序的链接。 这是我在 index.blade.php 视图中创建链接的那部分。

<table class="table">
        <tr>
            <th><a href="{{ route('books-index',['sort' => 'name','direction' => 'asc']) }}">Name</a></th>
            <th><a href="{{ route('books-index',['sort' => 'published_date','direction' => 'asc']) }}">Published Date</a></th>
            <th><a href="{{ route('books-index',['sort' => 'publisher_name','direction' => 'asc']) }}">Publisher Name</a></th>
            <th>Category</th>
            <th>Author</th>

在 BooksController.php 中,我有我的索引方法,其中排序部分如下所示

public function index(Request $request) {
        $input = $request->all();
        $sort = 'created_at';
        $direction = 'desc';
        if(isset($input['sort'])){
            $sort = $input['sort'];
        }
        if(isset($input['direction'])){
            $direction = $input['direction'];
        }

        $books = Book::join('publishers','publishers.id','books.publisher_id')
                ->orderby($sort,$direction)->select('books.*')->paginate(5);

下面是我对书籍的索引视图

但是当我点击 publisher_name 进行排序时,它给了我这样的错误。

QueryException in Connection.php line 770:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'publisher_name' in 'order clause' (SQL: select `books`.* from `books` inner join `publishers` on `publishers`.`id` = `books`.`publisher_id` order by `publisher_name` asc limit 5 offset 0)

那我该怎么解决呢?

【问题讨论】:

  • 你能发布发布者迁移吗?
  • 发布者迁移在“代码”下面给出 Schema::create('publishers', function (Blueprint $table) { $table->increments('id')->unsigned(); $table ->string('name'); $table->string('street'); $table->integer('number'); $table->bigInteger('zip'); $table->string('city '); $table->integer('country_id')->unsigned(); $table->foreign('country_id')->references('id')->on('countries'); $table-> string('license'); $table->timestamps(); }); }

标签: php mysql laravel


【解决方案1】:

可以将publishers.name别名为publisher_name,这样mysql就可以找到别名列:

$books = Book::join('publishers','publishers.id','books.publisher_id')
                ->orderBy($sort,$direction)->selectRaw('books.*, publishers.name AS publisher_name')->paginate(5);

【讨论】:

    【解决方案2】:

    就像您在 join 中所做的一样,您必须在 $sorttable.column 中使用点符号。

    除此之外... 这将是更好的编码方式:

    1. publisher 关系添加到您的Book 模型。
    function publisher () 
    {
        return $this->belongsTo("App\Publisher");
    } 
    
    1. books 关系添加到您的Publisher 模型。
    function books () 
    {
         return $this->hasMany("App\Book");
    } 
    
    1. 将您的查询更改为以下内容:
    Book::with("publisher")
               ->orderby($sort, $direction)
               ->paginate(5);
    

    【讨论】:

    • 我已经完成了你上面写的所有操作,但出现了同样的错误
    • 图书表:id name author_id published_date category_id created_at updated_at publisher_id image Publisher table:id name street number zip city country_id license created_at updated_at
    • 好的,所以没有列publisher_name,这意味着这个['sort' =&gt; 'publisher_name','direction' =&gt; 'asc'] 是错误的。应该是['sort' =&gt; 'publisher.name','direction' =&gt; 'asc'](或publishers.name)。
    • 如果您使用原始代码,它必须是 publishers 。因为我已经有几年没有使用连接了,所以我不记得您是否也必须在选择中指定 publishers.name
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 2018-01-10
    相关资源
    最近更新 更多