简体   繁体   English

通过连接 Laravel 中的两个表对列进行排序

[英]Sorting of columns by joining two tables in Laravel

I have two tables books and publishers.我有两个表书籍和出版商。 publisher_id is primary key in publishers table and foreign key in books table. publisher_id 是publishers 表中的主键和books 表中的外键。 In index.blade.php of books, I want to create link for sorting of columns of book index.在书籍的 index.blade.php 中,我想为书籍索引列的排序创建链接。 here is my that part of index.blade.php view where I am creating links.这是我在其中创建链接的 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>

And in BooksController.php, I have my index method in which sorting part is given below在 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);

Below is given my index view of books下面是我的书籍索引视图index.blade.php 视图

But When I click on publisher_name for sorting, it gives me an error like this.但是当我点击 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)

So what can I do to solve it?那么我能做些什么来解决它呢?

You can alias publishers.name to publisher_name , so that mysql can find the alias column:您可以将publisher_name publishers.name以便 mysql 可以找到别名列:

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

Like you did in your join you have to use dot notation in $sort , table.column .就像您在join中所做的那样,您必须在$sorttable.column中使用点符号。

Besides that... This would be the better way to code this:除此之外......这将是编写此代码的更好方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM