在Laravel API
中,我有一些方法,它们返回一些我不想在结果中出现的字段,例如:
id - email - user_id
,它们是可选的,因此我无法将此字段设置为自模型,现在我将此类用作ManageResource
到hode/remove
的字段/列,例如以下代码:
$user = User::whereApiToken($request->api_token)->first();
$result = ManageResource::make($user)->hide(['id','email']);
使用由下面的类实现的代码后,我在数组键上只得到id
和email
,但是我想隐藏它们
ManageResource
类:
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class ManageResource extends Resource
{
/**
* @var array
*/
protected $withoutFields = [];
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return $this->filterFields($this->withoutFields);
}
/**
* Set the keys that are supposed to be filtered out.
*
* @param array $fields
* @return $this
*/
public function hide(array $fields)
{
$this->withoutFields = $fields;
return $this;
}
/**
* Remove the filtered keys.
*
* @param $array
* @return array
*/
protected function filterFields($array)
{
return collect($array)->toArray();
}
}
答案 0 :(得分:2)
由于您希望将某些通常可见的属性隐藏在给定的模型实例上,因此可以使用makeHidden方法。
返回$ user-> makeHidden('attribute')-> toArray();
在这里,$ user代表用户模型
示例案例:使用带有代码的makeHidden隐藏分页结果中的列
$result = Job::where('user_id','=',Auth::id())->paginate(5);
$data =$result;
$result= $result->makeHidden(['hasMessage']);
$data->data = $result;
return $data;
您的情况:
$result = ManageResource::make($user)->makeHidden(['id','email'])->toArray();
$data->data = $result;
return $data;
或
$result = User::whereApiToken($request->api_token)->first()->setHidden(['id', 'email']);
$data->data = $result;
return $data;
注意:
据我所知,makeHidden不会影响关系。如果允许您将关系中的属性指定为-makeHidden(['relation.field']),那会很好。或者,您可以使用以下代码
App \ Models \ Product :: with([['images'=> function($ query){ $ query-> select([['id','product_id','image_url',
'row_id'])-> orderBy('created_at','desc');
}])-> get()-> makeHidden(['id']);
您的情况
$tickets = UserTickets::with(['user', 'reply' => function ($query) use ($user) {
$query->with('user')->select(['id','Attrib1','Attrib2'])->whereUserId($user->id);
}])->whereTicketNumber($request->ticket_number)->get();
答案 1 :(得分:1)
最简单的方法不是在模型本身上使用setHidden()
方法吗?
$user = User::whereApiToken($request->api_token)->first()->setHidden(['id', 'email']);
echo $user;
给出表/字段结构id - email - user_id
,结果应为:
{"user_id": 1}
答案 2 :(得分:1)
User::select('name')->get();
User::select('name', 'email')->get();
User::all()->pluck('name'); // array
答案 3 :(得分:0)
尝试一下:
/**
* Remove the filtered keys.
*
* @param $array
* @return array
*/
protected function filterFields($array)
{
return collect($array)->except($this->withoutFields)->toArray();
}
答案 4 :(得分:0)
答案 5 :(得分:0)
您可以使用地图过滤器获取所需字段。
$events= Events::where(['id' => 1])->get()
->map(function ($event) {
return [
'id' => $event->id,
'title' => $event->title,
'target' => $event->specific,
'date' => $event->start . '-' . $event->end,
'created' => $event->created_at
];
});
答案 6 :(得分:0)
$_SESSION['data'] = array();
$rows= Product::with('category')->get()
->map(function ($rows) {
array_push($_SESSION['data'],array(
'id' => $rows->id,
'title' => $rows->title,
'price' => $rows->price,
'photo' => $rows->price,
'categorytitle' => $rows->category->title,
'created_at' => $rows->getCreationDate(),
'updated_at' => $rows->getUpdationDate(),
));
});
$data = $_SESSION['data'];
unset($_SESSION['data']);
return view("products")->with("result",$data);