2015-01-17 1 views
0

저는 Laravel을 사용하여 소셜 네트워크 사이트를 만들려고합니다. 사용자는 프로필 사진이있는 프로필이 있습니다. 프로필 사진은 3 가지 크기로 표시됩니다. 1 (176x176) 주 프로필 페이지 (50x50)에 대한 작은 버전 및 프로필 (30x30)에 대한 링크로 탐색 모음에 표시 할 작은 버전사용자 프로필 이미지를보기에로드하는 방법. Laravel

내 데이터베이스에서 사용자는 사용자에 저장됩니다 표 :

id->int->primarykey 
media_id->int (id of the media set as the users profile picture) 
first_name->varchar 
last_name->varchar 
email->varchar 
password->varchar 

media_id는 사용자 프로필 이미지의 ID입니다. 모든 사용자 업로드 이미지는 public/images/users/{사용자 ID}/

에 저장됩니다 (단, 사용자의 프로필 이미지는 public/images/users/{사용자 ID}/프로필 사진에 저장됩니다). /)

이미지에 대한 URL이 매개체 테이블에 저장됩니다

id->int->primary 
user_id->int (id of the user that uploaded the image) 
mime_type->varchar 
url->varchar 

I에 포함 된 미디어의 ID에 사용자 테이블에서 사용자의 MEDIA_ID을 설정하는 방법을 알아내는 데 문제가 사용자가 프로필 사진으로 업로드 한 이미지의 URL 여기

public function showEditProfilePicture($id) 
    { 
     $user = $this->userRepository->findById($id); 

     return View::make('edit_profile-picture'); 
    } 

내가 이미지

{{ Form::open(['route' => 'postProfilePicture', 'files' => true]) }} 
    {{ Form::hidden('user_id', Auth::user()->id) }} 
    <h5 class="post-type">Upload Picture</h5> 
    {{ Form::file('media') }} 
    {{ Form::submit('Update Profile Picture', ['class' => 'posting-submit-btn d-bluegreen-btn-thick']) }} 
{{ Form::close() }} 
을 업로드하는 사용자를 위해 사용하는 형태이다 : 여기

는 프로필 사진

Route::get('{id}/edit-profile-picture', ['as' => 'profile_editProfilePicture', 'uses' => '[email protected]']); 

UsersController의 @의 showEditProfilePicture를 업로드 폼에 내 경로입니다

다음은 양식이 제출 될 때의 경로입니다.

Route::post('edit-profile-picture', ['as' => 'postProfilePicture', 'uses' => '[email protected]'])->before('csrf'); 

다음은 MediaController @ storeProfilePicture입니다. 내가 원래 이미지를 public/images/{users is}/profilePictures/디렉토리에 저장하는 방법과 3 가지 다른 크기의 이미지를 만들고 거기에 저장하는 방법에 유의하십시오.

class MediaController extends \BaseController { 


    public function storeProfilePicture() 
    { 
     $media = new Media; 

     $input = Input::all(); 

     $image = Image::make($input['media']->getRealPath()); 

     $name = time() . '-' . Auth::user()->id . '.' . $input['media']->getClientOriginalExtension(); 
     $imagePath = public_path() . '/images/users/'; 
     $directory = Auth::user()->id; 

     // check if directory exists, if not create the directory 
     File::exists($imagePath . $directory . '/profilePictures') or File::makeDirectory($imagePath . $directory . '/profilePictures', 0777, true, true); 

     $image->save($imagePath . $directory . '/profilePictures/' . $name) 
       ->fit(176, 176) 
       ->save($imagePath . $directory . '/profilePictures/' . '176thumb-' . $name) 
       ->fit(50, 50) 
       ->save($imagePath . $directory . '/profilePictures/' . '50thumb-' . $name) 
       ->fit(30, 30) 
       ->save($imagePath . $directory . '/profilePictures/' . '30thumb-' . $name); 
     // save to database 
     $media->url = $name; 
     $media->mime_type = $input['media']->getMimeType(); 
     $media->user_id = $input['user_id']; 
     $media->save(); 

     return Redirect::back(); 
    } 
} 

미디어 모델

class Media extends Eloquent { 
    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'medias'; 

    protected $fillable = ['url', 'user_id', 'mime_type']; 

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

여기

class User extends Eloquent implements UserInterface, RemindableInterface { 

    use UserTrait, RemindableTrait, EventGenerator, FollowableTrait; 

    /** 
    * Which feilds may be mass assigned 
    * @var array 
    */ 
    protected $fillable = ['first_name', 'last_name', 'email', 'password', 'media_id']; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 


    /** 
    * @return mixed 
    */ 
    public function media() 
    { 
     return $this->hasMany('Media'); 
    } 

} 

사용자 모델은 프로필 페이지 여기

Route::get('{id}', ['as' => 'profile', 'uses' => '[email protected]']); 

에 대한 경로입니다은 UsersController의 @ 쇼

입니다
public function show($id) 
    { 
     $user = $this->userRepository->findById($id); 

     return View::make('profile')->withUser($user); 
    } 

617,451,515,지금, 프로필 페이지에 나는 쉽게 사용자 이름을 표시 할 수있어 :

<h4>{{ $user->first_name }}</h4> 

임은 사용자에게 176x176 프로필 이미지를 표시하는 방법을 알아 내려고 노력.

내가

는 다음 url 필드 아래 media table에 이미지 이름을 저장 공공/이미지/사용자/{사용자 ID} 원래 이미지/profilePictures/{이미지 이름을} 저장하기 때문에

및 그 이미지의 3 가지 다른 크기를 만들고 176thumb- {image-name}, 50thumb- {image-name} 및 30thumb- {image-name} 으로 이름을 바꾸고 public/images/users/{사용자 ID }/profilePictures/{이미지 이름}

나는 다음을 수행하여 이미지를 얻을 수 있다고 가정합니다.

<img src="images/users/{{ $user->id }}/profilePictures/176thumb-{{ $user->media->url }}" width="176" height="176"> 
<img src="images/users/{{ $user->id }}/profilePictures/50thumb-{{ $user->media->url }}" width="176" height="176"> 
<img src="images/users/{{ $user->id }}/profilePictures/30thumb-{{ $user->media->url }}" width="176" height="176"> 

하지만 작동하지 않습니다.

public/images/users /를 확인하면 해당 이미지가 디렉토리에 있음을 알 수 있습니다. 데이터베이스의 medias 테이블을 확인하고 사용자 ID가 정확하고 mime_type이 정확하며 URL이 정확합니다.

하지만 이미지를 페이지에 표시하는 방법을 모르십니까? 내 문제 중 하나 알고

은 사용자 테이블의 MEDIA_ID이 난을 설정하려면 어떻게

(사용자의 MEDIA_ID가 프로필 사진은 미디어의 ID로 가정한다) 설정되지 않는다는 것입니다 사용자의 media_id, 이미지의 user_id, mime_type 및 url을 데이터베이스에 저장할 때?

마지막으로 사용자에게 속한 medias 테이블의 URL을 페이지에 어떻게 부르겠습니까?

미리 감사드립니다. 시간을내어 도와 주신 모든 분들께 감사드립니다. 새로운 미디어를 저장할 때, 그리고

public function profilePicture(){ 
    return $this->belongsTo('Media', 'media_id'); 
} 

associate

$media->save(); 
$user = Auth::user(); 
$user->profilePicture()->associate($media); 
$user->save(); 

를 사용하고보기 사용 {{ $user->profilePicture->url }}의 프로필 사진을 검색 할 수 :

답변

1

첫째, User 모델에 새로운 관계를 추가 URL.

그러나 이것이 작동해야하는 동안 미디어 테이블에 is_profile_picture 플래그가있는 것이 좋습니다.

+0

다시 한번이 토론을 열어보고 싶습니다. 이런 식으로하면 어떨까요? [http://imgur.com/a/eK8bi] URL : localhost : 8000/admin/dashboard에 내 프로필 사진을 표시하려면 어떻게해야합니까? – d3cypher

관련 문제