如何在控制器的 laravel 存储功能中进行保存和打印?

Posted

技术标签:

【中文标题】如何在控制器的 laravel 存储功能中进行保存和打印?【英文标题】:How can I do a save and print in laravel store function in controller? 【发布时间】:2021-10-23 02:09:54 【问题描述】:

我有一个执行保存和打印的按钮。我已经可以在store() 函数中保存,但不能打印。当我的意思是打印时,它意味着打开传递数据的打印对话框。

这是我的按钮:

<button type="button" id="save-and-print" name="action_create" value="save_and_print">Save and print</button>
<button type="submit" name="action_create" value="save">Save</button>

ReceiveItemsController.php

public function index() 
        return view('receive-items.index', ['has_id' => false]);
    

    public function indexWithId($product_id) 
        return view('receive-items.index', ['product_id' => $product_id, 'has_id' => true]);
    

    public function create() 
        return view('receive-items.index');
    

    public function store(Request $request)   
        $allow_print = true;
        $product_ids = array_map('intval', $request->product_id);
        $qty_per_item = array_map('intval', $request->qty);

        ReceiveItems::create([
            'created_at' => Carbon::now()->timestamp,
            'type' => 'Receiving',
            'product_id' => $product_ids,
            'no_of_items' => $request->no_of_items,
            'qty_per_item' => $qty_per_item,
            'total' => $request->total,
            'payment' => 'Already paid'
        ]);


        $print_notification = array(
            'message' => 'Saved successfully! Printing now...',
            'alert-type' => 'success'
        );

        $success_notification = array(
            'message' => 'New products has been added successfully!',
            'alert-type' => 'success'
        );

        $receive_items = $this->printVoucher($product_ids, $qty_per_item);

        if ($request->post('action_create') == 'save_and_print') 
            return view('receive-items.voucher', ['receive_items' => $receive_items,'allow_print' => $allow_print]);
         elseif ($request->get('action_create') == 'save') 
            return redirect()->route('receive-items.index')->with($success_notification);
        
    

public function printVoucher($product_ids, $qty_per_item) 
        $items = [];
        foreach($product_ids as $product_item_no => $product_id) 
            $products = Product::where('id', '=', $product_id); 
            $voucher_cost = $products->value('cost');

            $items[] = array(
                'product_item_no' => $product_item_no + 1,
                'product_id' => $product_id,
                'product_name' => $products->value('name'),
                'size' => $products->value('size'),
                'qty_addend' => $qty_per_item[$product_item_no],
                'voucher_cost' => $voucher_cost,
                'ext_cost' => number_format($voucher_cost * $qty_per_item[$product_item_no], 2),

            );
        
        return $items;
    

路线:

 Route::post('/print/voucher', function()  return view('receive-items.voucher'); );

voucher.blade.php

 <!DOCTYPE html>
<html lang="en">
    <head>
        <title>Voucher</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css" media="screen">
            * 
                font-family: Arial, Helvetica, sans-serif;
            
            html, body
                box-sizing: border-box;
                margin: 0;
                padding: 20px;
                height: 100%;
                font-size: 13px;
                font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
                line-height: 1.5;
            
            h4 
                 margin-top: 0;
                 margin-bottom: 0.5rem;
            
            p 
                 margin-top: 0;
                 margin-bottom: 1rem;
            
            strong 
                 font-weight: bolder;
            
            img 
                 vertical-align: middle;
                 border-style: none;
            
            table 
                 border-collapse: collapse;
            
            th 
                 text-align: inherit;
            
            h4, .h4 
                 margin-bottom: 0.5rem;
                 font-weight: 500;
                 line-height: 1.2;
            
            h4, .h4 
                 font-size: 1.5rem;
            
            .table 
                 width: 100%;
                 margin-bottom: 1rem;
                 color: #212529;
            
            .table th, .table td 
                 padding: 0.75rem;
                 vertical-align: top;
                 border-top: 1px solid #dee2e6;
            
            .table thead th 
                 vertical-align: bottom;
                 border-bottom: 2px solid #dee2e6;
            
            .table tbody  tbody 
                 border-top: 2px solid #dee2e6;
            
            /**
            * =utils
            **/
            .border-0 
                border: none !important;
            
            .mt-5 
                 margin-top: 3rem !important;
            
            .pr-0, .px-0 
                 padding-right: 0 !important;
            
            .pl-0, .px-0 
                 padding-left: 0 !important;
            
            .text-right 
                 text-align: right !important;
            
            .text-center 
                 text-align: center !important;
            
            .text-uppercase 
                 text-transform: uppercase !important;
            
            /**
            * =layout
            **/
            .coupon 
                position: relative;
                display:  inline-block;
                overflow:  hidden;
                border-radius: 10px;
            

            .coupon-con 
                position: relative;
                width: 324px;
                height: 210px;
            

            .coupon-con::before 
                content: '';
                position: absolute;
                left: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(0, auto, 210px, auto);
            

            .coupon-con::after 
                content: '';
                position: absolute;
                right: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(0, auto, 210px, auto);
            

            .cover-top 
                background-color: #eee;
                position: absolute;
                top: 0;
                width: inherit;
                height: 75px;
                border-top-left-radius: 10px;
                border-top-right-radius: 10px;
            

            .coupon-btm 
                position: relative;
                width: 324px;
                height: 110px;
            

            .coupon-btm::before 
                content: '';
                position: absolute;
                top: -210px;
                left: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(210px, auto, auto, auto);
            

            .coupon-btm::after 
                content: '';
                position: absolute;
                top: -210px;
                right: -210px;
                display: block;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                border: 200px solid #eee;
                clip: rect(210px, auto, auto, auto);
            

            .cover-btm 
                background-color: #eee;
                position: absolute;
                bottom: 0;
                width: inherit;
                height: 75px;
                border-bottom-left-radius: 10px;
                border-bottom-right-radius: 10px;
            
        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="coupon">
                <div class="coupon-con">
                    <div class="cover-top"></div>
                    <div style="position: absolute; z-index: 9999; width: 100%;">
                        <img src=" asset('custom') /img/logo.png"   />
                        <table class="table">
                            <thead>
                                <th>Item</th>
                                <th>Qty.</th>
                                <th>Cost</th>
                                <th>Ext. Cost</th>
                            </thead>
                            <tbody>
                                -- Items --
                                @foreach($receive_items as $item)
                                <tr>
                                    <td class="pl-0">
                                        <center> $item['product_item_no'] </center>
                                        <br />
                                        <center></center> $item['product_name'] </center>
                                    </td>
                                    <td>
                                        <center> $item['qty_addend'] </center>
                                    </td>
                                    <td>
                                        <center> $item['voucher_cost'] </center>
                                    </td>
                                    <td>
                                        <center> $item['ext_cost'] </center>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td><strong>Total</strong></td>
                                    <td><strong></strong></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
                <div class="coupon-btm">
                    <div style="position: absolute; z-index: 9999; width: 100%;">
                        <table class="table">
                            <tbody>
                                <tr>
                                    <td class="border-0 pl-0" >
                                        <center><p><strong>Receiving Voucher</strong></p></center>
                                        <center><p> $receive_items[0]['product_item_no'] </p></center>
                                    </td>
                                    <td class="border-0 pl-0" >
                                        <p><center><strong>Voucher Date</strong></center></p>
                                        <p><center></center></p>
                                    </td>
                                </tr>      
                            </tbody>
                        </table>
                    </div>
                    <div class="cover-btm"></div>
                </div>
            </div>
        </div>
    </body>
    @if($allow_print)
    <script type="text/javascript">
        (function() 
          window.print()
        )();
    </script>
    @endif
</html>

此代码仅打开带有$received_items 数据的voucher.blade.php,但不会打开打印对话框。可能路线有问题?

非常感谢任何帮助。

【问题讨论】:

【参考方案1】:

您似乎正在使用两个刀片文件。所以只需将window.print() 添加到receive-items.voucher.blade.php 中,如下所示

<script>
(function() 
  window.print()
)();
</script>

这样一旦加载了 dom,打印窗口就会打开。

其他信息

你也可以在下面短你的喜欢

if ($request->post('action_create') == 'save_and_print')
    return view('receive-items.voucher', ['receive_items' => $receive_items]);

  return redirect()->route('receive-items.index');
    

更新 由于要求打印在index.blade.php

将额外变量传递给 index.blade 以便您可以使用 print 选项。类似下面的东西

public function index() 
   
$allowPrint = true; // Your logic goes here
        return view('receive-items.index', ['receive_items' => $receive_items,'allowPrint' => $allowPrint]);
   

index.blade.php

@if($allowPrint)
<script>
(function() 
  window.print()
)();
</script>
@endif

【讨论】:

它现在可以工作了。谢谢@Nipun Tharuksha。我怎样才能让它像它不会打开凭证.blade.php 并且只保留在 index.blade.php 页面上,但仍会打开打印对话框? 如果有效希望你能接受我的回答谢谢 我已经更新了通过 index.blade.php 打印的答案 谢谢@Nipun Tharuksha。我的意思是我仍然希望它在 store() 函数中保存数据,但仍会显示索引页面。我该怎么做? 所以它会显示索引并且应该能够打印

以上是关于如何在控制器的 laravel 存储功能中进行保存和打印?的主要内容,如果未能解决你的问题,请参考以下文章

如何返回并显示保存到数据库laravel的json数据

如何在 Laravel 中调整图像大小并同时将数据保存在数据库中

如何在用户登录laravel上保存PassportToken

如何通过laravel中的控制器将数据保存在数据库中

laravel应用程序不会保存图像,但会将其文件名存储到数据库中

如何在 PHP Laravel Web 服务中获取照片路径