laravel 详细的文档有吗,英文版的也可以

比如model里
belongsTo第四个参数怎么用。belongsTo后还可以怎么接条件之类的。

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案
  1. 看源码
  2. API 文档
3年前 评论
gonghui (楼主) 3年前
讨论数量: 3
panda-sir

没有 放弃吧 兄弟 :smirk:

3年前 评论

看源码不就知道了

/**
 * Define an inverse one-to-one or many relationship.
 *
 * @param  string  $related
 * @param  string|null  $foreignKey
 * @param  string|null  $ownerKey
 * @param  string|null  $relation
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
{
    // If no relation name was given, we will use this debug backtrace to extract
    // the calling method's name and use that as the relationship name as most
    // of the time this will be what we desire to use for the relationships.
    if (is_null($relation)) {
        $relation = $this->guessBelongsToRelation();
    }

    $instance = $this->newRelatedInstance($related);

    // If no foreign key was supplied, we can use a backtrace to guess the proper
    // foreign key name by using the name of the relationship function, which
    // when combined with an "_id" should conventionally match the columns.
    if (is_null($foreignKey)) {
        $foreignKey = Str::snake($relation).'_'.$instance->getKeyName();
    }

    // Once we have the foreign key names, we'll just create a new Eloquent query
    // for the related models and returns the relationship instance which will
    // actually be responsible for retrieving and hydrating every relations.
    $ownerKey = $ownerKey ?: $instance->getKeyName();

    return $this->newBelongsTo(
        $instance->newQuery(), $this, $foreignKey, $ownerKey, $relation
    );
}

加查询条件可以:

return $this->belongsTo(Pricing::class, 'pricing_id', 'id')->where('', ''); //全局加上
Product::with(['pricings' => function($query) {
    $query->where('your quiry condition')
}])
3年前 评论
gonghui (楼主) 3年前
  1. 看源码
  2. API 文档
3年前 评论
gonghui (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!