0
我在Laravel是新的,我有2类模型(表客户和订单):laravel 5 - 机锋关系的两个表
table Customer, with columns:
- id_cust (PK)
- name
table Order, with columns:
- id_order (PK)
- id_cust (FK to table Customer)
- product_name
- qty
我尝试做模型 “客户”:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $primaryKey = 'id_cust';
protected $table = 'customer';
}
和模型类 '订单':
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $primaryKey = 'id_order';
protected $table = 'order';
}
在SQL:
select a.id_cust, a.name, b.product, b.qty
from customer a, order b
where a.id_cust = b.id_cust;
我想这样的输出:
========================================
|id_cust |name |product |qty |
|======================================|
|10 |jeff |Plastic |10 |
|10 |jeff |Book |2 |
|11 |james |Laptop |1 |
|11 |james |TV |1 |
|12 |davy |shoe |1 |
========================================
我尝试在控制器,但没有成功
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\customer;
use App\order;
class ShowDetail extends Controller
{
public function index(){
$aa = customer->where('customer.id_cust','=','order.id_cust')->order-get();
var_dump($aa);
}
}
什么建议吗?
谢谢。
顺序模型
喜Xhulio,谢谢你的回答,但还是失败了,你能告诉我哪个部分解释了“... customer.id_cust = order.id_cust ...”之间的关系吗?谢谢。 – Firdi
在你原来的代码中可以试试这个:'$ aa = customer-> where('customer.id_cust','order.id_cust') - > get();' – xhulio
@Firdi这个关系在'customer()和'order()'方法。但是你也应该在数据库模型中定义它们 – xhulio