0

In laravel 9 I have component which I created with command

php artisan make:component  ProductCardReport

In app/View/Components/ProductCardReport.php I get some data with eloquent :

<?php

namespace App\View\Components;

use App\Library\ReportProduct;
use Illuminate\View\Component;

class ProductCardReport extends Component
{
    public string $productId;
    public function __construct(string $productId)
    {
        $this->productId = $productId;
    }

    public function render()
    {
        $productData = getProductData($this->productId);
        ...
        return view('components.product-card-report', [
            'productId' => $this->productId,
            'productData'          => $productData,

        ]);
    }
}

and template of this component resources/views/components/product-card-report.blade.php which includes form for data submiting:

<x-slot name="header"> 
  Product Card
</x-slot>

 
    <form method="POST" action="{{ route('admin.generatePdfByContent') }}" accept-charset="UTF-8"
          id="form_print_to_pdf_content" name="form_print_to_pdf_content"
          enctype="multipart/form-data">
        {!! csrf_field() !!}

        <a @click.prevent="generateIntoFile()" class="editor_form_btn_save mr-4">
            {!! AppContent::showIcon(App\Enums\SvgIconType::GENERATE, App\Enums\SvgIconSize::MEDIUM) !!}
            Generate
            </a>
        <input type="__hidden" id="pdf_content" name="pdf_content"
               value=" pdf_content lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ">
    </form>

    <div id="div_card_report_content" class="w-full d">

By submitting this form acion of generatePdfByContent app/Http/Controllers/Admin/ProductController.php is triggered and in this method all job is done, but I dislike a lot that I have to call method generatePdfByContent of ProductController from component view file. But reading this https://laravel.com/docs/9.x/blade#rendering-components docs I do not see how can I by submitting form in view of component to call method of the component ?
Is it possible somehow?

Maybe some other desision is possible? What else can be triggered(which action) by form submitting?

Thanks!

mstdmstd
  • 2,195
  • 17
  • 63
  • 140
  • 1
    A form submission is not calling a function, but posting to a route and in that is triggering a controller method. You can read the post request in the component, but thats not good practice. If you don't want to leave the page when posting, use ajax. Either way, handeling a form post belongs in a controller, not in a component – Gert B. Nov 04 '22 at 08:40
  • Maybe you are right and thats not good practice. But in which can I do "You can read the post request in the component" ? Please ref to related docs. – mstdmstd Nov 04 '22 at 11:01
  • 1
    It's not only bad practice, you always have to post to a post route. You can read the request with the `request()` helper function. But you should not. – Gert B. Nov 04 '22 at 11:11

0 Answers0