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。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools