如何使用 Laravel 5.1 从数据库中只选择 2 列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31598637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:01:25  来源:igfitidea点击:

How to only select 2 column from the database with laravel 5.1?

phpmysqllaravellaravel-5

提问by Junior

I am trying to use laravel 5.1 to select records from MySQL database and break the results into pages.

我正在尝试使用 laravel 5.1 从 MySQL 数据库中选择记录并将结果分成页面。

Here is the method that I use to return the database results to the listall view. (This code display white screen "no errors, no results")

这是我用来将数据库结果返回到 listall 视图的方法。(此代码显示白屏“无错误,无结果”)

public function getIndex(){

    $accounts = DB::table('accounts')
                    ->lists('account_id','account_name')
                    ->where('client_id', '=', 7)
                    ->paginate(100);

    $name = 'Mike A';

    return view('accounts.listall', compact('accounts', 'name'));

}

When using this code below, it works but it returns all the column. I only want to display 2 columns.

在下面使用此代码时,它可以工作,但会返回所有列。我只想显示 2 列。

public function getIndex(){

    $accounts = DB::table('accounts')
                    ->where('client_id', '=', 7)
                    ->paginate(100);

    $name = 'Mike A';

    return view('accounts.listall', compact('accounts', 'name'));

}

EDITED

已编辑

This is my code after Kyle Suggestion "below"

这是我在凯尔建议“下面”之后的代码

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Accounts;

class AccountsController extends Controller
{

    public function getIndex(){

        $accounts = Accounts::select('account_id', 'account_name')
                    ->where('client_id', '=', 7)
                    ->paginate(100);


        $name = 'Mike A';

        return view('accounts.listall', compact('accounts', 'name'));

    }

    public function getAccounts($id){

        return view('accounts.account')->withName('Mike A');
    }
}

This is my Accounts Model

这是我的账户模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Accounts extends Model
{

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'accounts';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['account_id', 'account_name', 'company_code'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];
}

But I still get a white screen

但是我还是白屏

采纳答案by Kyle

First off, you shouldn't be using DB::table('accounts'). You should be using Account::all(). It's just syntax though I guess.

首先,你不应该使用DB::table('accounts'). 你应该使用Account::all(). 虽然我猜这只是语法。

I assume you have a table named accounts with 2 columns of that table being account_idand account_name. That being said, your whole class should look similar to this:

我假设您有一个名为 accounts 的表,该表的 2 列是account_idaccount_name。话虽如此,您的整个班级应该类似于以下内容:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Account; //don't forget this so you can use it below

class AccountController extends Controller {

    public function getIndex() {
        $accounts = Account::select('account_id', 'account_name')
                    ->where('client_id', '=', 7)
                    ->paginate(100);

        $name = 'Mike A';

        return view('accounts.listall', compact('accounts', 'name'));
    }

Is that what you need?

那是你需要的吗?

回答by Don't Panic

In the Laravel Query Builder docs, see "Specifying a Select Clause". To do what you are trying to do, you need to use the selectmethod instead of the listsmethod you are using.

Laravel Query Builder docs 中,请参阅“指定选择子句”。要做你想做的事,你需要使用select方法而不是lists你正在使用的方法。

The selectmethod allows you to specify which columns you want from the table, and returns $this(The Query Builder instance) so that you can chain it with more Query Builder methods like you are doing with whereand paginate.

select方法允许您从表中指定您想要的列,并返回$this(查询生成器实例),以便您可以将它与更多查询生成器方法链接起来,就像使用where和 一样paginate

The listsmethod returns an array (see in the docs under "Retrieving A List Of Column Values"). The returned array will not have the whereand paginatemethods. This is what is currently killing your script and giving you the white screen.

lists方法返回一个数组(参见“检索列值列表”下的文档)。返回的数组将没有wherepaginate方法。这就是目前正在杀死您的脚本并为您提供白屏的原因。

The listsmethod should be the last method called in a chain of query builder methods. Like getor paginate, it is meant to return the results of your query in a specific way.

lists方法应该是查询构建器方法链中调用的最后一个方法。像getor一样paginate,它旨在以特定方式返回查询结果。