Php Laravel 5.4中清理不需要的对象属性并仅提取所需属性的Laravel代码?
在Laravel5.4中,我有sqlite数据库和下面的代码。我试图从下面的代码中返回的只是文章表列(id、title、description)值,仅此而已。当我使用Var_dump()时,它会返回不必要的对象属性Php Laravel 5.4中清理不需要的对象属性并仅提取所需属性的Laravel代码?,php,laravel,sqlite,Php,Laravel,Sqlite,在Laravel5.4中,我有sqlite数据库和下面的代码。我试图从下面的代码中返回的只是文章表列(id、title、description)值,仅此而已。当我使用Var_dump()时,它会返回不必要的对象属性 public function home() { $articles=Article::get(); return view('home',['items'=>$articles]); // any name can be given
public function home()
{
$articles=Article::get();
return view('home',['items'=>$articles]); // any name can be given
}
{{var_dump($items)}}
如下所列
`object(Illuminate\Database\Eloquent\Collection)#189 (1) {
["items":protected]=>
array(5) {
[0]=>
object(App\Article)#190 (25) {
["connection":protected]=>
string(6) "sqlite"
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["withCount":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "Laravel"
["description"]=>
string(34) "Laravel is Powerful Php Framework."
["created_at"]=>
string(19) "2018-09-10 17:04:38"
["updated_at"]=>
string(19) "2018-09-10 17:04:38"
}
["original":protected]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "Laravel"
["description"]=>
string(34) "Laravel is Powerful Php Framework."
["created_at"]=>
string(19) "2018-09-10 17:04:38"
["updated_at"]=>
string(19) "2018-09-10 17:04:38"
}
}`
我想要的是一个代码,它返回一个具有如下所示的确切属性的对象属性
["attributes":protected]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "Laravel"
["description"]=>
string(34) "Laravel is Powerful Php Framework."
["created_at"]=>
string(19) "2018-09-10 17:04:38"
["updated_at"]=>
string(19) "2018-09-10 17:04:38"
}
有什么好的解决方案吗?在
文章的顶部
模型中,将要筛选出的某些表列添加到此$hidden
数组中
protected $hidden = [
'user_password', 'remember_token',
];
这是对的
public function home()
{
$articles=Article::get();
return view('home',['items'=>$articles]); // any name can be given
}
在您的视图中查看输出
{{ dd($items) }}
也不用担心使用var_dump的额外参数,现在只访问需要的参数