2014-05-25 2 views
0

Codesleeve Stapler을 사용하고 있는데 약간의 문제가 있습니다. Laravel + CodeSleeve 스테이플러 번들

나는 마지막 예제는이 페이지에 설명 정확히 한 : https://github.com/CodeSleeve/stapler

의 차이는 나의 새로운 모델이 대신 ProfilePictures 의 사진이라고 내 모델 User하지만 Trip없는 점이다.

보기의 <img src="<?= asset($picture->photo->url('thumbnail')) ?>"> 에는 업로드 된 마지막 사진이 표시됩니다.

Trip에 속하는 Picture을 보여 드리고 싶습니다. 내가 어떻게 이걸 할 수 있니?

감사합니다.

답변

5

이렇게 '여행'과 '사진'의 두 가지 모델이 있습니다.

public function pictures(){ 
    return $this->hasMany('Picture'); 
} 

그런 다음, 사진 모델, 당신은 스테이플러 첨부 정의 : 당신 이제

// Be sure and use the stapler trait, this will not work if you don't: 
use Codesleeve\Stapler\Stapler; 

// In your model's constructor function, define your attachment: 
public function __construct(array $attributes = array()) { 
    // Pictures have an attached file (we'll call it image, but you can name it whatever you like). 
    $this->hasAttachedFile('image', [ 
     'styles' => [ 
      'thumbnail' => '100x100#', 
      'foo' => '75x75', 
      'bar' => '50x50' 
     ] 
    ]); 

    parent::__construct($attributes); 
} 

를 '당신의 여행 모델에서는 사진 모델로'hasMany의 '관계를 정의 할 필요가 그림 모델에서 첨부 파일을 정의 했으므로 Picture 개체에 액세스 할 때마다 파일 첨부에 액세스 할 수 있습니다. 당신이 여행 기록을 가지고 가정하면, 당신이 할 수 있습니다 :

<img src="<?= asset($picture->image->url()) ?>"> 
// or 
<img src="<?= asset($picture->image->url('original')) ?>"> 

는 사실, 당신은 당신이 정의 된 스타일에 액세스 할 수 있습니다 :

<?php foreach ($trip->pictures as $picture): ?> 
    <img src="<?= asset($picture->image->url('thumbnail')) ?>"> 
<?php endforeach ?> 

는이 같은 원본 이미지에 액세스 할 수 있습니다

<img src="<?= asset($picture->image->url('foo')) ?>"> 
<img src="<?= asset($picture->image->url('bar')) ?>"> 

희망이 도움이됩니다.

관련 문제