【问题标题】:Acess belongsTo by where通过 where 访问 belongsTo
【发布时间】:2019-04-13 09:00:32
【问题描述】:

我得到了这个代码:

public function Locate($name){
    $locate= Products::where('name', 'like', "$name%") ->get();

    return response()->json($locate, 200);
}

我想通过这个$name访问belongsTo并把它放到json中

这是我的模型:

class Products extends Model
{
    protected $fillable = ['name', 'code', 'price'];

    protected $hidden = ['id'];

    public function Group()
    {
        return $this->belongsTo('App\pGroup', 'product_id', 'id');
    }

}

【问题讨论】:

    标签: json laravel


    【解决方案1】:

    如果你想建立关系:

    $locate= Products::where('name', 'like', "$name%")->with('group')->get();

    【讨论】:

    • 有。我又问了一个问题。有没有办法访问这个 relashionp 中的外键并在 json 中返回?如果我使用您的代码,则 json 将返回所有组,但带有外键号
    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2018-11-04
    • 2012-10-10
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多