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

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

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案
  1. 看源码
  2. API 文档
6年前 评论
gonghui (楼主) 6年前
讨论数量: 3
panda-sir

没有 放弃吧 兄弟 :smirk:

6年前 评论

看源码不就知道了

/**
 * 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')
}])
6年前 评论
gonghui (楼主) 6年前
  1. 看源码
  2. API 文档
6年前 评论
gonghui (楼主) 6年前

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