2

I have an API returning an object with one-to-one relation to another object. As the models behind the objects, do have timestamps, these are also delivered when asking API.

// Get all transactions
Route::get('transaction', function() {
return Transaction::with('Personone','Persontwo')->get();
});

How do I prevent Laravel from returning the timestamps of the objects in the API?

I googled, but only found some hints to middleware or response macros but found no example pointing me into the right direction. Maybe you can help me.

3
  • 2
    Consider using a transformer. Commented Feb 18, 2018 at 21:37
  • I second the transformer suggestion. They provide a clean abstraction for mapping a model to a response. Commented Feb 18, 2018 at 21:57
  • What version of Laravel are you using? You can use laravel's own transformation if you using laravel 5.4 or greater I think Commented Feb 19, 2018 at 1:19

2 Answers 2

2

You can make attributes "hidden" so that they do not show up in json. docs

class Transaction extends Model
{
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['timestamp'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure if I get the question correctly but if you want to select from eager loads there are two ways

first one is inline selecting

Route::get('transaction', function () {
    return Transaction::with('Personone:id,foo,bar', 'Persontwo:id,foo,bar,foobar')->get();
});

second one is to pass a closure

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.