php 在 Laravel 中选择多列表单数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32840007/
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
Select multiple column form database in Laravel
提问by Nishal Gurung
How can I run following query in Laravel?
如何在 Laravel 中运行以下查询?
Select column1,column2,column3 from table;
I don't want to retrieve all columns records as we do by
我不想像我们那样检索所有列记录
Select * from table;
回答by Rohit Dalal
Use this :
用这个 :
DB::table('table')
->select(array('column1', 'column2', 'column3'))
->get();
回答by Dean Collins
The Eloquent way :
雄辩的方式:
DB::table('table')
->select('column1', 'column2', 'column3')
->get();
$tableObject
->select('column1', 'column2', 'column3')
->get();
回答by Zakaria Acharki
Basicly and like @Uchihamentioned in comment you can use :
基本上和评论中提到的@Uchiha一样,您可以使用:
DB::statement('select column1,column2,column3 from table');
But will be better if you use laravel Eloquent ORM
function, so after migration you have to create TableModel
for your table and use lists
function :
但是如果你使用 laravelEloquent ORM
函数会更好,所以迁移后你必须TableModel
为你的表创建并使用lists
函数:
TableModel::lists('column1','column2','column3');
Hope this helps.
希望这可以帮助。
回答by Hamidreza
A simple way in Laravel 5.2 using Eloquent
Laravel 5.2 中使用 Eloquent 的简单方法
$rows = Table::where('name', 'like', 'John')->get(['id', 'name', 'family']);
$rows = Table::where($id, 'id')->first(['id', 'name', 'family']);
回答by Jhourlad Estrella
I'm surprised nobody thought of the Model-Controller approach.
我很惊讶没有人想到模型控制器方法。
On your model add:
在您的模型上添加:
protected $appends = ['column3'];
public function getColum3Attribute()
{
return $this->column1.$this->column2;
}
So you can use it readily from your Controller:
所以你可以很容易地从你的控制器中使用它:
YourModel::pluck('column3', 'id');
回答by Madusanka
Try this:
尝试这个:
$y = Image::select( array( 'saved_path', 'uploaded_date', 'uploaded_time') )
$y = Image::select( array( 'saved_path', 'uploaded_date', 'uploaded_time') )
->where('id', 4)
->get();