将新元素追加到 eloquent 集合
Append new element to eloquent collection
我有两张桌子 post 和照片。每个 post 有 5 张照片。我想在每个 post 视图中列出一张照片(个人资料照片),第一张照片。
$published_post = Post::where('created_by',auth()->user()->id)
->where('status','published')
->get();
$photo = Photo::where('post',$published_post->id)->get();
这两个给了我两个不同的合集。如何将特定 post 的第一张照片添加到其数组中,以便在视图中使用 foreach 循环显示。
这就是我想要的视图:
@foreach($published_post as $item)
{{ $item->title }}
{{ $item->profile_photo }}
@endforeach
我试过放置和推送,但似乎没有用。不确定我们如何将新的键值对附加到对象。
我的两个模型:
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'];
}
您需要创建 2 个模型,一个用于 Posts,一个用于照片。
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 关系到 link 到照片模型
<?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 型号:
<?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
好的,首先你应该像这样改变你的 Post 模型:
class Post extends Model
{
protected $table = 'posts';
public function photos()
{
return $this->hasMany(Photo::class, 'post');
}
}
然后,将以下内容添加到您的照片模型中:
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
有关关系的更多信息,您可能需要阅读 docs。
希望对您有所帮助!
我有两张桌子 post 和照片。每个 post 有 5 张照片。我想在每个 post 视图中列出一张照片(个人资料照片),第一张照片。
$published_post = Post::where('created_by',auth()->user()->id)
->where('status','published')
->get();
$photo = Photo::where('post',$published_post->id)->get();
这两个给了我两个不同的合集。如何将特定 post 的第一张照片添加到其数组中,以便在视图中使用 foreach 循环显示。
这就是我想要的视图:
@foreach($published_post as $item)
{{ $item->title }}
{{ $item->profile_photo }}
@endforeach
我试过放置和推送,但似乎没有用。不确定我们如何将新的键值对附加到对象。
我的两个模型:
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'];
}
您需要创建 2 个模型,一个用于 Posts,一个用于照片。
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 关系到 link 到照片模型
<?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 型号:
<?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
好的,首先你应该像这样改变你的 Post 模型:
class Post extends Model
{
protected $table = 'posts';
public function photos()
{
return $this->hasMany(Photo::class, 'post');
}
}
然后,将以下内容添加到您的照片模型中:
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
有关关系的更多信息,您可能需要阅读 docs。
希望对您有所帮助!