mardi 29 mars 2016

Ignore field in select Laravel Eloquent

Consider these models:

Flight Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable =
  [
      'userID'
  ];    

  public function user()
  {
    return $this->belongsTo('App\User', 'userID');
  }
}

User Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable =
  [
      'userID', 'username', 'address'
  ];    
}   

I want to skip a column when I'm trying to use eager loading. I just want to know if something like IGNORE_FIELD FUNCTION exists, so I can do something like this:

$flightUser = Flight::with(['user', function ($q)
{
    $q->ignoreField('user.address');
}])

I know I could push the address field into the hidden array inside the User Model,

protected $hidden = ['address'];

or do the $q->select('fields') and not including the address field, but my real doubt is whether laravel has a function like the one I typed above.

NOTE

I know one field isn't that big of a deal. But my question is a simplified version of my real problem.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire