Laravel播种机问题

时间:2017-10-01 20:02:19

标签: php laravel

我是Laravel的新手并且遇到了一个问题。首先,我使用faker为我的照片库中的类别ID,并且一切都很好:( ProductSeeder)

public function run()
{
    $faker = $this->faker->create();

    $categories_ids = $this->category->pluck('id');

    foreach (range(1, 10) as $x) {


      $this->photo->create(
        [
          'title' => $faker->name(),
          'image_url' => $faker->imageUrl(800, 600, 'animals'),
          'description' => $faker->sentence(100),
          'price' => $faker->numberBetween(0.5, 20 ),
          'category_id' => $categories_ids->random(),
        ]
      );
  }

}

但是现在我正在尝试使用create()函数创建一个包含数据的真实图库。但我无法处理category_id。也许有人知道如何改变random() - 我不需要它。我有自己创建的category-id。提前谢谢

  public function run()
{
  $categories_ids = $this->category->pluck('id');
      DB::table('photos')->insert(
        [
          'title' => '',
          'image_url' => '',
          'description' => '',
          'category_id' => $categories_ids->random()
        ]);

}

我正在更新信息。我正在战斗很多个小时,并且发现我的照片重复8次,并且有类别。 CategorySeeeder看起来像这样

public function run()
{
    DB::table('categories')->insert([
      'title'=>'Mediniai baldai'
    ]);

    DB::table('categories')->insert([
      'title'=>'Laiptai ir durys'
    ]);

    DB::table('categories')->insert([
      'title'=>'Malonios detalės'
    ]);

    DB::table('categories')->insert([
      'title'=>'Apdailos plytelių klojimas'
    ]);

    DB::table('categories')->insert([
      'title'=>'Dėmesys detalėms'
    ]);

    DB::table('categories')->insert([
      'title'=>'Vonia, dušas, pirtis'
    ]);

    DB::table('categories')->insert([
      'title'=>'Židinių apdaila'
    ]);

    DB::table('categories')->insert([
      'title'=>'Autoriniai dirbiniai'
    ]);
}

然后在photos_table中进行迁移

e  public function up()
{
    Schema::create('photos', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('image_url');
        $table->text('description');
        $table->integer('category_id')mi
        $table->timestamps();

        $table->foreign('category_id')->references('id')->on('categories');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('photos');
}

和我的索引刀片 - 在哪里显示我的所有问题如下:

<ul class="portfolio-categ filter">
  <li>Kategorijos:</li>
  <li class="all active">
    <a href="#">Visos nuotraukos</a></li>
  @foreach($categories as $category)
        <li class="cat-item-{{$category->id}}">
        <a href="#" title="Category {{$category->id}}">{{$category->title}}</a>
     </li>
  @endforeach
    </ul>

    @component('components.create',[ 'url' =>
    route('photos.create')])
    @endcomponent

      @foreach($categories as $category)
           <ul class="portfolio-area">

      @foreach($category->photos as $photo)
      <li class="portfolio-item2" data-id="id-{{$photo->category_id}}" data-type="cat-item-{{$photo->category_id}}">

                <div>
            <span class="image-block">
                        <a class="image-zoom" href="{{$photo->image_url}}" rel="prettyPhoto[gallery]" title="{{$photo->title}}">
                <img width="230" height="170" src="{{$photo->image_url}}" alt="Finding Nemo" title="{{$photo->title}}" />
              </a>
            </span>
            <div class="home-portfolio-text">
                      <h3 class="post-title-portfolio">
                <div class="">
                  @component('components.delete', [ 'url' =>route
                  ('photos.destroy', $photo->id)])
                  @endcomponent
                </div>
                <div class="">
                  @component('components.edit', [ 'url' =>route
                 ('photos.edit', $photo->id)])
                 @endcomponent
                </div>

                <a href="#" rel="bookmark" title="Finding Nemo">{{$photo->category_id}}</a>
              </h3>
              <p class="post-subtitle-portfolio">{{str_limit($photo->description,10) }}</p>
                     </div>
                  </div>
     </li>
      @endforeach

     <div class="column-clear"></div>
     </ul><!--end portfolio-area -->
  @endforeach

很抱歉显示这么多文字。主要问题是 - 我将新照片放入数据库,它获得一个单一的ID,但在网站上它显示8张具有相同单一ID的照片。感谢您的耐心和帮助。

0 个答案:

没有答案