[英]Laravel: Order a model by "Has One Of Many" relationship
I have a customer model that has many contacts.我有一个客户 model 有很多联系人。 I defined a relationship to get the most recent contact of the customer using the "Has One Of Many" relationship in Laravel 8:我使用 Laravel 8 中的“Has One Of Many”关系定义了一个关系以获取客户的最新联系:
Models楷模
class Customer extends Model
{
use HasFactory;
public function contacts()
{
return $this->hasMany(Contact::class);
}
public function latestContact()
{
return $this->hasOne(Contact::class)->ofMany('contacted_at', 'max')->withDefault();
}
}
class Contact extends Model
{
use HasFactory;
protected $casts = [
'contacted_at' => 'datetime',
];
public function customer()
{
return $this->belongsTo(Customer::class);
}
}
Migration (contact model)迁移(接触模型)
class CreateContactsTable extends Migration
{
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->softDeletes();
$table->foreignID('customer_id');
$table->string('type');
$table->dateTime('contacted_at');
});
}
}
In my view, I want to show all customers and order them by their latest contact .在我看来,我想向所有客户展示并通过他们最近的联系人订购他们。 However, I can't figure out how to do that.但是,我不知道该怎么做。
I tried to achieve it via the join method but then I obviously get various entries per customer.我试图通过 join 方法来实现它,但显然每个客户都有不同的条目。
$query = Customer::select('customers.*', 'contacts.contacted_at as contacted_at')
->join('contacts', 'customers.id', '=', 'contacts.customer_id')
->orderby('contacts.contacted_at')
->with('latestContact')
Knowing Laravel there must be a nice way or helper to achieve this.知道 Laravel必须有一个很好的方法或助手来实现这一点。 Any ideas?有任何想法吗?
I think the cleanest way to do this is by using a subquery join:我认为最干净的方法是使用子查询连接:
$latestContacts = Contact::select('customer_id',DB::raw('max(contacted_at) as latest_contact'))->groupBy('customer_id');
$query = Customer::select('customers.*', 'latest_contacts.latest_contact')
->joinSub($latestContacts, 'latest_contacts', function ($join){
$join->on([['customer.id', 'latest_contacts.customer_id']]);
})
->orderBy('latest_contacts.latest_contact')
->get();
More info: https://laravel.com/docs/8.x/queries#subquery-joins更多信息: https://laravel.com/docs/8.x/queries#subquery-joins
I suspect there is an issue with your migration, the foreign key constraint is defined like this:我怀疑您的迁移存在问题,外键约束的定义如下:
Check the documentation:https://laravel.com/docs/8.x/migrations#foreign-key-constraints检查文档:https://laravel.com/docs/8.x/migrations#foreign-key-constraints
Method 1: define foreign key constraint方法一:定义外键约束
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->foreignId('consumer_id')->constrained();
$table->string('type');
$table->dateTime('contacted_at');
$table->timestamps();
$table->softDeletes();
});
}
Method 2: define foreign key constraint方法二:定义外键约束
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('customer_id');
$table->foreign('customer_id')->references('id')->on('customers');
$table->string('type');
$table->dateTime('contacted_at');
$table->timestamps();
$table->softDeletes();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.