Bra*_*dge 37 php laravel eloquent
当我使用eloquent时,我可以使用"where"方法,然后使用方法"get"来填充包含我在数据库中选择的内容的对象.我的意思是:
$users = User::where('gender', 'M')->where('is_active', 1)->get(['pseudo', 'email', 'age', 'created_at'])->toArray();
Run Code Online (Sandbox Code Playgroud)
在这里,我可以选择我想要的列,如'伪','电子邮件'等.但我在laravel doc中想到的是相反的方法.它可能是这样的:
$users = User::where('gender', 'M')->where('is_active', 1)->notGet(['pseudo', 'email', 'age', 'created_at'])->toArray();
Run Code Online (Sandbox Code Playgroud)
谢谢你的未来答案,祝你有愉快的一天.
Raz*_*zor 45
AFAIK SQL中没有构建选项来明确排除列,因此Laravel无法做到这一点.但你可以尝试这个技巧
更新
另一个技巧是指定模型中的所有列
protected $columns = array('id','pseudo','email'); // add all columns from you table
public function scopeExclude($query,$value = array())
{
return $query->select( array_diff( $this->columns,(array) $value) );
}
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
$users = User::where('gender', 'M')->where('is_active', 1)->exclude(['pseudo', 'email', 'age', 'created_at'])->toArray();
Run Code Online (Sandbox Code Playgroud)
Chr*_*ius 36
我不知道以前的Laravel版本,但在5.4中你可以将这一行放在用户模型中
protected $hidden = ['pseudo', 'email', 'age', 'created_at'];
Run Code Online (Sandbox Code Playgroud)
然后User::find(1);将返回所有领域除外pseudo,email,age,和created_at.
但您仍然可以使用以下方法检索这些隐藏字段:
$user = User::find(1);
$email = $user['email']; // or $user->email;
Run Code Online (Sandbox Code Playgroud)
saj*_*our 24
hidden在模型中使用数组是很好的方法,但是如果您不想一直隐藏列并用于makeVisible访问需要的列,则可以使用以下makeHidden函数在需要的地方从序列化中隐藏列:
$res = Model::where('your query')->get();
$res->makeHidden(['column_one','column_two');
return response()->json($res);
Run Code Online (Sandbox Code Playgroud)
你可以hidden像这样使用数组:
class Promotion extends Model
{
protected $table = 'promotion';
protected $hidden = array('id');
}
Run Code Online (Sandbox Code Playgroud)
我查看了@Razor 的答案
但是通过跳过 $columns 属性有一种非常方便的方法
/**
* Scope a query to only exclude specific Columns
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeExclude($query, ...$columns)
{
return $query->select( array_diff( $this->getTableColumns(),$columns) );
}
/**
* Shows All the columns of the Corresponding Table of Model
*
* @author Manojkiran.A <manojkiran10031998@gmail.com>
* If You need to get all the Columns of the Model Table.
* Useful while including the columns in search
* @return array
**/
public function getTableColumns()
{
return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
}
Run Code Online (Sandbox Code Playgroud)
getTableColumns函数将获取表的所有列,因此您无需定义
$column 属性
我有一个适合我的解决方案,它与已经提到的略有不同。
$all_columns = Schema::getColumnListing('TABLE_NAME');
$exclude_columns = ['COLUMN_TO_EXCLUDE_1', 'COLUMN_TO_EXCLUDE_2'];
$get_columns = array_diff($all_columns, $exclude_columns);
return User::select($get_columns)->get();
Run Code Online (Sandbox Code Playgroud)
为我:
BadMethodCallException with message 'Call to undefined method App/CaseStudy::exclude()'
因此,最后,我修改了 Razor 的解决方案,使其无需隐藏每个方法的任何列即可工作。
我希望这可以帮助别人!
我们从充满所有字段的模型中获得雄辩的对象,将其转换为数组,然后将其放入集合中。然后我们得到除数组$ fields中指定的所有字段以外的所有字段。
$fields = ['a', 'b', 'c', 'N'];
$object = Model::find($id);
return collect($object->toArray())->except($fields);
Run Code Online (Sandbox Code Playgroud)
更清楚地,让我们举一个例子:
// Array of fields you want to remove
$fields_to_remove = ['age', 'birthday', 'address'];
// Get the result of database
$user = User::find($id);
// Transform user object to array
$user = $user->toArray();
// Create a collection with the user inside
$collection = collect($user);
// Get all fields of our collection except these fields we don't want
$result = $collection->except($fields_to_remove);
// Return
return $result;
Run Code Online (Sandbox Code Playgroud)
上面的示例与第一个示例完全相同,但有更多解释。
| 归档时间: |
|
| 查看次数: |
52592 次 |
| 最近记录: |