Laravel + CodeSleeve订书机捆绑

Nic*_*oot 0 php laravel laravel-4

我正在使用Codesleeve Stapler,我有一个小问题.

我完成了本页描述的最后一个例子:https: //github.com/CodeSleeve/stapler

不同的是,我的新的模式被称为图片,而不是ProfilePictures 和我的模型是不是User,但Trip.

<img src="<?= asset($picture->photo->url('thumbnail')) ?>"> 视图上显示已上传的最后一张照片.

我想展示Picture属于每个人的Trip.我怎么能这样做?

谢谢.

小智 5

所以,你有两个模型:'Trip'和'Picture'.在Trip模型中,您需要定义与Picture模型的'hasMany'关系:

public function pictures(){
    return $this->hasMany('Picture');
}
Run Code Online (Sandbox Code Playgroud)

然后,在图片模型中,您定义订书钉附件:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

现在您已经在Picture模型上定义了附件,每次访问Picture对象时,您都可以访问文件附件.假设你有旅行记录,你可以这样做:

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

您可以像这样访问原始图像:

<img src="<?= asset($picture->image->url()) ?>">
// or
<img src="<?= asset($picture->image->url('original')) ?>">
Run Code Online (Sandbox Code Playgroud)

实际上,您可以访问您定义的任何样式:

<img src="<?= asset($picture->image->url('foo')) ?>">
<img src="<?= asset($picture->image->url('bar')) ?>">
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.