如何从 Laravel JSON Api 响应中删除字段
How to remove fields from Laravel JSON Api response
我有一个 API 返回一个与另一个对象具有一对一关系的对象。作为对象背后的模型,确实有时间戳,在询问 API.
时也会提供这些时间戳
// Get all transactions
Route::get('transaction', function() {
return Transaction::with('Personone','Persontwo')->get();
});
如何防止 Laravel 返回 API 中对象的时间戳?
我用谷歌搜索,但只找到了一些关于中间件或响应宏的提示,但没有找到指向正确方向的示例。也许你能帮帮我。
我不确定我是否答对了问题,但如果您想 select 从急切的加载中,有两种方法
第一个是内联 selecting
Route::get('transaction', function () {
return Transaction::with('Personone:id,foo,bar', 'Persontwo:id,foo,bar,foobar')->get();
});
第二个是传递闭包
Route::get('transaction', function () {
return Transaction::with([
'Personone' => function ($query) {
$query->select('id', 'foo', 'bar');
},
'Persontwo' => function ($query) {
$query->select('id', 'foo', 'bar', 'foobar');
}])->get();
});
Eager Loading Specific Columns You may not always need every column
from the relationships you are retrieving. For this reason, Eloquent
allows you to specify which columns of the relationship you would like
to retrieve:
$users = App\Book::with('author:id,name')->get();
Constraining Eager Loads Sometimes you may wish to eager load a
relationship, but also specify additional query constraints for the
eager loading query. Here's an example:
$users = App\User::with(['posts' => function ($query) {
$query->where('title', 'like', '%first%'); }])->get(); In this example, Eloquent will only eager load posts where the post's title
column contains the word first. Of course, you may call other query
builder methods to further customize the eager loading operation:
$users = App\User::with(['posts' => function ($query) {
$query->orderBy('created_at', 'desc'); }])->get();
您可以使属性 "hidden" 不显示在 json 中。
docs
class Transaction extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['timestamp'];
}
我有一个 API 返回一个与另一个对象具有一对一关系的对象。作为对象背后的模型,确实有时间戳,在询问 API.
时也会提供这些时间戳// Get all transactions
Route::get('transaction', function() {
return Transaction::with('Personone','Persontwo')->get();
});
如何防止 Laravel 返回 API 中对象的时间戳?
我用谷歌搜索,但只找到了一些关于中间件或响应宏的提示,但没有找到指向正确方向的示例。也许你能帮帮我。
我不确定我是否答对了问题,但如果您想 select 从急切的加载中,有两种方法
第一个是内联 selecting
Route::get('transaction', function () {
return Transaction::with('Personone:id,foo,bar', 'Persontwo:id,foo,bar,foobar')->get();
});
第二个是传递闭包
Route::get('transaction', function () {
return Transaction::with([
'Personone' => function ($query) {
$query->select('id', 'foo', 'bar');
},
'Persontwo' => function ($query) {
$query->select('id', 'foo', 'bar', 'foobar');
}])->get();
});
Eager Loading Specific Columns You may not always need every column from the relationships you are retrieving. For this reason, Eloquent allows you to specify which columns of the relationship you would like to retrieve:
$users = App\Book::with('author:id,name')->get();
Constraining Eager Loads Sometimes you may wish to eager load a relationship, but also specify additional query constraints for the eager loading query. Here's an example:
$users = App\User::with(['posts' => function ($query) { $query->where('title', 'like', '%first%'); }])->get(); In this example, Eloquent will only eager load posts where the post's title column contains the word first. Of course, you may call other query builder methods to further customize the eager loading operation:
$users = App\User::with(['posts' => function ($query) { $query->orderBy('created_at', 'desc'); }])->get();
您可以使属性 "hidden" 不显示在 json 中。 docs
class Transaction extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['timestamp'];
}