我是Laravel的初学者。我在项目中使用Laravel 5.8。
我有这个用户:
class User extends Authenticatable implements MustVerifyEmail
{
public function UserProfileImageGallery()
{
return $this->hasMany('App\UserProfileImage', 'user_id', 'id')->orderBy('number1')->orderBy('number2')->orderBy('number3')->orderBy('number4')->orderBy('number5')->orderBy('number6')->orderBy('number7');
}
}
class UserProfileImage extends Model
{
protected $quarded = ['id'];
protected $fillable = ['user_id', 'path1', 'number1', 'path2', 'number2', 'path3', 'number3', 'path4', 'number4', 'path5', 'number5', 'path6', 'number6', 'path7', 'number7'];
public $timestamps = false;
}
我有用户列表:
$users = User::with('UserProfileImageGallery')->paginate(15);
然后我列出清单:
@foreach ($usersList as $user)
@if ($user->showSecretPhoto == 1)
// here I want show $user->UserProfileImageGallery->path7
@else
$user->UserProfileImageGallery->path1
@endif
@endforeach
如何在用户模型中嵌入图像(秘密或正常)?
我想在模型中构建一个功能,为用户选择合适的照片。如果用户:$ user-> showSecretPhoto的值为1-我要显示他的第7张图片:$ user-> UserProfileImageGallery-> path7。否则,我要显示照片1。
如何做到?