在我的Laravel 7.6应用中, 我使用资源集合, 并检索了创建者, 我想在某些情况下不显示所有创建者的字段, 而只显示其中的一部分. 我在我的控件中:
$activeVotes = Vote
::getByStatus('A')
->where('votes.id', 1)
->with('voteCategory')
->with('creator')
return (new VoteCollection($activeVotes));
在appHttpResourcesVoteCollection.php中, 我有:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class VoteCollection extends ResourceCollection
{
public static $wrap = 'votes';
public function toArray($request)
{
$this->collection->transform(function ($votes) {
return new VoteResource($votes);
});
return parent::toArray($request);
}
}
而在appHttpResourcesVoteResource.php中:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class VoteResource extends JsonResource
{
public static $wrap = 'votes';
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'creator' => $this->whenLoaded('creator'),
...
如何定义如果我需要只显示上面的一些创建者字段?
EDITED.在appHttpResourcesVoteResource.php中:如何定义如果我需要只显示上面的一些创建者的字段?
My app/User.php has :
<?php
namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DB;
...
class User extends Authenticatable implements MustVerifyEmail
{
use Billable;
use HasApiTokens, Notifiable;
use funcsTrait;
protected $table = 'users';
protected $primaryKey = 'id';
public $timestamps = false;
protected $userAvatarPropsArray = [];
protected $avatar_filename_max_length = 255;
protected $full_photo_filename_max_length = 255;
protected static $password_length = 8;
protected $fillable = [ 'username', 'email', 'status', 'provider_name', 'template_id', 'provider_id', 'first_name', 'last_name', 'phone', 'website', 'password',
'activated_at', 'avatar', 'full_photo', 'updated_at', 'verified' ];
protected $hidden = [ 'password', 'remember_token' ];
...
public function votes()
{
return $this->hasMany('App\Vote', 'creator_id', 'id');
}
...
and appVote.php :
<?php
namespace App;
use DB;
use App\MyAppModel;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use App\Http\Traits\funcsTrait;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Validation\Rule;
class Vote extends MyAppModel
{
use funcsTrait;
use Sluggable;
use HasTags;
protected $table = 'votes';
protected $primaryKey = 'id';
public $timestamps = false;
protected $casts = [
'meta_keywords' => 'array'
];
protected $fillable = ['name', 'slug', 'description', 'creator_id', 'vote_category_id', 'is_quiz', 'status', 'image'];
protected static $logAttributes = ['*'];
...
public function creator(){
return $this->belongsTo('App\User', 'creator_id','id');
}
在Vote和用户模型中,有引用到其他模型.Otherwize。
->with('creator')
不工作.是否有一些选项我错过了?
谢谢!
我想你有两个选择。
hidden
在你 user
模型通过使用 hidden
在Laravel文档中, 你可以指定哪些字段会显示在你的模型的JSON或Arrray表示中.
with
声明$activeVotes = Vote::getByStatus('A')->where('votes.id', 1)
->with('voteCategory')
->with(['creator' => function ($query) {
$query->select('id', 'first_name');
}]);
return (new VoteCollection($activeVotes));
您需要包括的领域 id
,因为这是负责加入这两个。