2017-03-09 7 views
0

データベースのテキスト列からunserializeオブジェクトを含むブレードビューからExcelファイルをエクスポートしようとしましたが、エラーが発生します。オブジェクトのエクスポートunserialize Excelブレードビュー

エラー:ここ

Call to undefined method Illuminate\Database\Query\Builder::transform()

は私のコントローラである:ここでは

public function ExportExcel($id) 
{ 
    $order = Order::find($id); 

    $order->transform(function ($order , $key){ 

     $order->cart = unserialize($order->cart); 
     return $order; 

    }); 


    Excel::create('facture', function($excel) use ($order) { 
     $excel->sheet('Excel', function($sheet) use ($order) { 
      $sheet->loadView('cotisation_structure.factureExcelsingle')->with(['order' => $order]); 
     }); 
    })->export('xls'); 

} 

は私の刃図である:

<html> <body> 
<thead> 
<tr> 
    <th>N° Facture</th> 
    <th>N° licence</th> 
    <th>Nom</th> 
    <th>Prénom</th> 
    <th>Type d'activité</th> 
    <th>Saison</th> 
    <th>Mode de paiement</th> 
    <th>Montant</th> 
    <th>Date Achat</th> 
</tr> 
</thead> 
<tr> 
    <td>{{$order->num_facture}}</td> 
    <td>{{$order['item']->num_licence}}</td> 
    <td>{{$order['item']->lb_nom}}</td> 
    <td>{{$order['item']->lb_prenom}}</td> 
    <td>{{$order['item']->activite_licencie->lb_activite}}</td> 
    <td>{{$order['item']->saison->lb_saison}}</td> 
    <td>{{$order->payment_method}}</td> 
    <td>{{$order['price']}}</td> 
    <td>{{$order->date_achat}}</td> 
</tr> 

その変換Methodeのオブジェクトのために動作しないようですコレクションの場合のみ。

誰かが問題を解決するためのアイデアを持っていますか?ありがとう、私は事前に進める

+0

トランスフォームの代わりに、 'json_encode($ order)'を使用してください。 – Onix

+0

あなたの返信に感謝します! json_encode($ order)でこのエラーが発生しました。未定義のメソッドを呼び出してください。\ Database \ Query \ Builder :: json_encode() –

答えて

1

Eloquent findメソッドは単一のモデルインスタンスを返すモデルのコレクションを返す代わりに。

変換メソッドは、単一モデルインスタンスではなく、コレクションでのみ動作します。

はこの1

public function ExportExcel($id) 
{ 
    $order = Order::find($id); 
    $order->cart = unserialize($order->cart); 


    Excel::create('facture', function($excel) use ($order) { 
    $excel->sheet('Excel', function($sheet) use ($order) { 
     $sheet->loadView('cotisation_structure.factureExcelsingle')->with(['order' => $order]); 
    }); 
    })->export('xls'); 

} 

注意してみてください:変換関数を単一のオブジェクト上にないコレクションに取り組んでいます。

+0

あなたの返信に感謝します!今すぐあなたのコードで私はこのエラーが表示されます:未定義の変数:アイテム –

+0

あなたの構造に問題があるので、実際に何が結果に戻ってそこにプリントされているかを注文結果をデバッグすることができます。私はあなたのために多くのアイテムを持っている場合、それは実際に –

+0

を返しているものを、あなたの意見では、以下のようにアクセスしてくださいというあなたの構造をチェックする必要が ​​{{$ order->アイテム[0] - > num_licence}} ​​{ {$ order->項目[0] - > lb_nom}} ​​{{$ order->項目[0] - > lb_prenom}} ​​{{$ order->項目[0] - > activite_licencie-> lb_activite}} ​​{{$ order->項目[0] - >> saison-> lb_saison}} –

関連する問題