mercredi 30 octobre 2019

Adding photo display function to the user's model

I am beginner in Laravel. I use Laravel 5.8 in my project.

I have this User:

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;
}

I have user list:

$users = User::with('UserProfileImageGallery')->paginate(15);

And I make list:

@foreach ($usersList as $user)
@if ($user->showSecretPhoto == 1)
    // here I want show  $user->UserProfileImageGallery->path7
@else
    $user->UserProfileImageGallery->path1
@endif
@endforeach

How do you embed image (secret or normal) display in a User Model?

I would like to build a function in the model that will choose the right photo for the user. If the user: $ user-> showSecretPhoto has a value of 1 - I want to display his 7th picture: $ user-> UserProfileImageGallery-> path7. Otherwise, I want to display photo 1.

How to do it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire