簡體   English   中英

在eloquent集合中添加新元素

[英]Append new element to eloquent collection

我有兩張桌子的帖子和照片。 每個帖子都有5張照片。 我想在每個帖子中列出一張照片(個人資料圖片),第一張圖片。

$published_post = Post::where('created_by',auth()->user()->id)
                        ->where('status','published')
                        ->get();

$photo = Photo::where('post',$published_post->id)->get();

這兩個給了我兩個不同的收藏。 如何將特定帖子的第一張照片添加到其數組中,以便在視圖中我可以使用foreach循環顯示。

這就是我想要的方式:

@foreach($published_post as $item)
    {{ $item->title }}
    {{ $item->profile_photo }}
@endforeach

我嘗試過put和push,但似乎沒有工作。 不確定我們究竟是如何將新的鍵值對附加到對象上的。

我的兩個型號:

Schema::create('posts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('title');
    $table->timestamps();
});

Schema::create('photos', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('image');
    $table->integer('post');
    $table->timestamps();
});

class Post extends Model
{
    protected $table = 'posts';
}


class Photo extends Model
{
    protected $table = 'photos';
    protected $fillable = ['image', 'post'];
}

郵政模式:

<?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;


    class Post extends Model
    {
        /*This is used to specify the table which this model is associated with*/
        protected $table = 'posts';

        protected $fillable = [
          'title'
        ];
        public $timestamps = true;

        public function photos(){
            return $this->hasMany(Photos::class,'post');
            //post is the foreign key for posts table
        }
    }

照片型號:

<?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;


    class Photo extends Model
    {
        /*This is used to specify the table which this model is associated with*/
        protected $table = 'photos';

        protected $fillable = [
          'image', 'post'
        ];
        public $timestamps = true;

    }

視圖:

@foreach($published_post as $item)
    {{ $item->title }}
    {{ $item->photos->first()->image }} // photos relation is invoked and fetched first image
@endforeach

您需要創建2個模型,一個用於帖子,一個用於照片。

php artisan make:model Post
php artisan make:model Photo
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Posts extends Model
{
    //
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    //
}

然后在Post模型上創建一個hasMany關系以鏈接到Photo模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use App\Photo;

class Post extends Model
{
    public function photos()
    {
        return $this->hasMany(Photo::class);
    }
}

然后在您的視圖中,您可以隨時懶散地加載照片

@foreach($posts as $post)
    {{ $post->title }}
    {{ $post->photo[0]->name}}
@endforeach

在您的視圖中進行的語法將略有不同,但這使您可以很好地了解功能應如何工作。

好的,首先你應該改變你的Post模型:

class Post extends Model
{
    protected $table = 'posts';

    public function photos()
    {
        return $this->hasMany(Photo::class, 'post');
    }
}

然后,將以下內容添加到您的Photo模型中:

class Photo extends Model
{
    protected $table = 'photos';

    public function post()
    {
        return $this->belongsTo(Post::class, 'post');
    }
}

有了這個,您已經創建了模型之間的關系,現在您可以通過這種方式獲取數據:

$published_post = Post::where('created_by',auth()->user()->id)
                      ->where('status','published')
                      ->with('photos')
                      ->get();

在您看來,您可以通過這種方式獲得第一張照片:

@foreach($published_post as $item)
    {{ $item->title }}
    {{ $item->photos()->first()->name }}
@endforeach

有關關系的更多信息,您可能需要閱讀文檔

我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM