【发布时间】:2015-07-23 21:48:23
【问题描述】:
我正在尝试使用 laravel 5.1 从 MySQL 数据库中选择记录并将结果分成页面。
这是我用来将数据库结果返回到 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'));
}
使用下面的代码时,它可以工作,但会返回所有列。我只想显示 2 列。
public function getIndex(){
$accounts = DB::table('accounts')
->where('client_id', '=', 7)
->paginate(100);
$name = 'Mike A';
return view('accounts.listall', compact('accounts', 'name'));
}
已编辑
这是我在 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');
}
}
这是我的帐户模型
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 = [];
}
但我仍然得到一个白屏
【问题讨论】:
-
用
select代替lists。
标签: php mysql laravel laravel-5