찾다
php教程php手册orm获取关联表里的属性值 - 微笑女孩

ORM——关系对象模型

laravel中的Eloquent ORM用于和数据表互动,其中每个数据库表会和一个对应的「模型」互动,想要了解请查看官方文档或自行百度。获取关联表里的属性值代码如下:

    /**
     * [getContactAttribute 获取卖家联系人]
     */
    public function getContactAttribute()
    {
        return $this->hasOne('App\Http\Models\Supplier', 'SupplierId', 'SupplierId')->first()->contact;
    }

  2、获取订单商品信息

    public function getProductInfoAttribute()
    {
        $info = $this->items()->get()->map(function ($item) {
            $json = $item->format(['title', 'quantity']);//$json为数组
            return implode('x', array_values($json));
        })->toArray();//toArray()将object转换为数组
        if (count($info) > 1) {
            $info = implode(';', $info);
        }else {
            $info = $info[0];
        }
        return $info;
    }

    /**
     * [items 订单购买的商品]
     * @return [type] [description]
     */
    public function items()
    {
        return $this->hasMany('App\Http\Models\OrderItem', 'OrderId', 'OrderId');
    }

  其中getProductInfoAttribute()方法用于获取订单商品的信息,最后效果是:商品1名字x数量;商品2名字x数量。eg:奥力根电链锯x1;金戈铁马台钻x3。

 

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구