我有PostAd模型和PostImage模型。他们有一对多的关系。一个PostAd有多个PostImage。
现在我想要显示来自PostImage的PostAd图像,条件是mainimage == 1。
PostAd model
public function postimage()
{
return $this->hasMany(PostImage::class,'postad_id');
}
Controller of PostAd
$data['userads'] = PostAd::with('postimage')-&
我有一张桌子message_thread
id
sender_id
recipient_id
我希望在我的User模型中声明一个关系,它将按如下方式获取所有消息线程:
SELECT *
FROM message_thread
WHERE sender_id = {user.id}
OR recipent_id = {user.id}
我尝试了以下几点:
public function getMessageThreads()
{
return $this->hasMany(MessageThread::className(), ['sender_id' =&
首先,我的英语不太好,很抱歉。 在我的例子中,我有很多关系--很多关系是从a表到b表,通过a_b表。a上的行可以在b上列一行,并用a_b上的type列来区分它们。 class A extends Model
{
public function bs
{
return $this->belongToMany(B::class)
->withPivot(['type']);
}
}
class B extends Model
{
public function as
{
我无法在保存到数据库中的多到多多态关系中获得相关的模型。
$photo = Photo::find(1);
$photo->articles()->attach(2);
像这样的东西是行不通的
错误:调用未定义的方法Illuminate\Database\Query\Builder::articles()‘
怎么做才是正确的呢?
模型
class Tag extends Eloquent
{
public function articles()
{
return $this->morphedByMany('Article'
我的Python项目包括数据处理程序、模型,可以在不同的模式下运行。
在我的代码中,我使用如下语句
if mode=='mode1':
# or
if isinstance(model, Model1):
适应培训脚本和绘图脚本的流程。
我相信我已经通过为模型和数据处理程序提供不同的子类来使用策略模式。
但我不知道如何正确地训练模型和绘图结果,因为这些脚本使用模型、数据处理程序和模式。
例如,绘图函数将根据数据处理程序有不同的子图排列,并且根据模型产生不同的内容。
我需要将评论模型与两个ids同时关联,但不知道如何关联。这就是我的情况。我正在建立一个,需要能够让老师对特定课程中的特定学生在特定学期(评分期)发表评论。
class Course
has_many :course_terms
has_many :enrollments
end
class CourseTerm
belongs_to :course
end
class Student
has_many :enrollments
has_many :courses, :through => :enrollments
end
class Enrollment <
我有三个数据模型,其中一个扩展了另一个:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Opinion extends Model
{
public function reactions()
{
return $this->morphMany('App\Models\Reaction', 'reactable');
}
...
}
namespace App\Models\Activity;
use App\Mo
我正试图使用多态关系来处理suppliers和products之间的多到多关系。
products
id
name
suppliers
id
name
product_supplier
id
product_id // belongsToMany easily takes care of this id
supplier_id // and this id
price // this can be fetched using withPivot('price')
deliverymethod
我对belongsTo()关系的问题如下。
我的模型
class TypeOne extends Model{
}
===============================
class TypeTwo extends Model{
}
===============================
class Post extends Model{
public function typeOne(){
return $this->belongsTo('Type','type_id')->where('obje
假设我有3个表,分别名为user、admin、post
我的帖子表结构是
-id
-poster_id
-poster_type //if value is 1 then poster_id will be releted with user table. if value is 2 then poster_id releted with admin table.
现在如何根据poster_type值来写两个表之间belongsTo关系
我想在Post模型中这样做
public function Author(){
return $this->belongsTo('Us