欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

模型 select 得到模型对象 必须包含主键,customer_id 否则的话,获取到的数据里面就不是customer对象了 要想获得关联关系, select 里面必须要有关联关系的id ,,要想得到象的属性的名称,则select中必须包含这个列名,有大用 有大大用 有大大大用 有大大大大用

$customer =
   Customer::
select('city_name as city_name_join','store_name as store_name_join',
       
'customers.customer_id as customer_id', 'customer_name',\DB::raw('SUM(operation_num) as xiao_fei_num_join'),
       \DB::
raw('SUM(shoukuan_amount) AS xiao_fei_amounts_join'),\DB::raw('SUM(trade_yiji) as trade_yiji_join'))
->joinCity()->joinStore()->leftJoinOperation()->leftJoinShoukuan()->leftJoinTrade()->groupby(
"customers.customer_id");


dump($customers[0]);exit;  // 下图 看到会有 customer_id 这个了


有了customer_id 之后 ,,,,才能够调用 $customers 集合里面的对象的方法


image.png

 



$customer =
   Customer::
select('city_name as city_name_join','store_name as store_name_join',
       
'customers.customer_id as customer_id','customers.city_id as city_id','customer_name',\DB::raw('SUM(operation_num) as xiao_fei_num_join'),
       \DB::
raw('SUM(shoukuan_amount) AS xiao_fei_amounts_join'),\DB::raw('SUM(trade_yiji) as trade_yiji_join'))
       ->joinCity()->joinStore()->leftJoinOperation()->leftJoinShoukuan()->leftJoinTrade()->groupby(
"customers.customer_id");


由上面代码 因为有了 'customers.city_id as city_id'  ,,,所以 调用 $customers[0]->city 的值才不为空




$customer =
   Customer::select('city_name as city_name_join','store_name as store_name_join',
       'customers.customer_id as customer_id','customers.city_id as city_id','customer_name',\DB::raw('SUM(operation_num) as xiao_fei_num_join'),
       \DB::raw('SUM(shoukuan_amount) AS xiao_fei_amounts_join'),\DB::raw('SUM(trade_yiji) as trade_yiji_join'))
       ->joinCity()->joinStore()->leftJoinOperation()->leftJoinShoukuan()->leftJoinTrade()->groupby("customers.customer_id");


由上面代码 正因为有了 customer_name  ,,,所以 调用 $customers[0]->customer_name 的值才不为空




普通分类: