将数据从 Blade 视图传递给 Blade 组件

huangapple go评论73阅读模式
英文:

Pass data from a Blade View to a Blade component

问题

I have the following controller which loads a view that contains a component along with the data fetched from the database. The view shows up with $data correctly. The issue I'm having is that the data I'm passing from the view to the component is not showing up, giving me the error "Undefined variable $data." Please can anyone suggest me a solution.

TransactionController

class TransactionController extends Controller
{
    public function index(){
        $data = TransactionType::all();
        return view('addtransaction', ['data' => $data]);
    }   
}

AddTransaction.blade.php (This is the view)

<!DOCTYPE html>
<html data-theme="light">
<head>
    <title>Transactions</title>
    <meta name="description" content="Our first page" />
    <meta name="keywords" content="html tutorial template" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css" />
    <link rel="stylesheet" href="{{ URL::asset('css/app.css') }}" />
</head>
<body>
    <main class="container">
        {{$data}}
        <x-user-nav></x-user-nav>
        <x-add-transaction :data="$data"></x-add-transaction>
    </main>
</body>
</html>

add-transaction.blade.php (This is the component)

<div style="box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px, rgba(0, 0, 0, 0.08) 0px 0px 0px 1px; padding: 30px;">
    <div class="grid">
        <form action="/handleTransaction" method="POST">
            @csrf
            {{$data}}
            <h3>Add Transaction</h3>
        </form>
    </div>
    <!-- Rest of your component HTML -->
</div>

Your AddTransaction Component class

<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class AddTransaction extends Component
{
    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function render(): View|Closure|string
    {
        return view('components.add-transaction');
    }
}

The code appears to be fine, but if you're encountering an "Undefined variable $data" error, ensure that the $data variable is properly passed to the view in your controller and that it's spelled correctly.

英文:

I have the following controller which loads a view that contains a component along with the data fetched from database, the view shows up $data correctly, the issue I'm having is the data I'm passing from the view to component is not showing up giving me the error "Undefined variable $data". Please can anyone suggest me a solution.

TransactionController

class TransactionController extends Controller
{

  public function index(){

      $data = TransactionType::all();
      return view(&#39;addtransaction&#39;, [&#39;data&#39; =&gt; $data]);

  }   

    
}

AddTransaction.blade.php (This is the view)

&lt;!DOCTYPE html&gt;
&lt;html data-theme=&quot;light&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;Transactions&lt;/title&gt;
        &lt;meta name=&quot;description&quot; content=&quot;Our first page&quot; /&gt;
        &lt;meta name=&quot;keywords&quot; content=&quot;html tutorial template&quot; /&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css&quot; /&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ URL::asset(&#39;css/app.css&#39;) }}&quot; /&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;main class=&quot;container&quot;&gt;
       {{$data}}
        &lt;x-user-nav&gt;&lt;/x-user-nav&gt;

        
        
          &lt;x-add-transaction :data=&quot;$data&quot;&gt;&lt;/x-add-transaction&gt;

           
            
        &lt;/main&gt;
    &lt;/body&gt;
&lt;/html&gt;

add-transaction.blade.php (This is the component)

&lt;div style=&quot;box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px, rgba(0, 0, 0, 0.08) 0px 0px 0px 1px; padding: 30px;&quot;&gt;
&lt;div class=&quot;grid&quot;&gt;
    &lt;form action=&quot;/handleTransaction&quot; method=&quot;POST&quot;&gt;
        @csrf
       {{$data}}
        
                &lt;h3&gt;Add Transacton&lt;/h3&gt;
            &lt;/div&gt;
            &lt;div class=&quot;grid&quot;&gt;
                &lt;div&gt;
                    &lt;label for=&quot;date&quot;&gt;
                        Date
                        &lt;input type=&quot;date&quot; id=&quot;date&quot; name=&quot;date&quot; /&gt;
                    &lt;/label&gt;
                &lt;/div&gt;
                &lt;div&gt;
                    &lt;label for=&quot;Type&quot;&gt;Type&lt;/label&gt;
                    &lt;select name=&quot;type&quot; required&gt;
                        &lt;option value=&quot;&quot; selected&gt;Select a type&lt;/option&gt;
                     
                    &lt;/select&gt;
                &lt;/div&gt;
                &lt;div&gt;
                    &lt;label for=&quot;From&quot;&gt;From&lt;/label&gt;
                    &lt;select name=&quot;from&quot; required&gt;
                        &lt;option value=&quot;&quot; selected&gt;Select an Account&lt;/option&gt;
                        &lt;option&gt;Income&lt;/option&gt;
                        &lt;option&gt;Expense&lt;/option&gt;
                        &lt;option&gt;Transfer&lt;/option&gt;
                    &lt;/select&gt;
                &lt;/div&gt;
                &lt;div&gt;
                    &lt;label for=&quot;To&quot;&gt;To&lt;/label&gt;
                    &lt;select name=&quot;to&quot; required&gt;
                        &lt;option value=&quot;&quot; selected&gt;Select an Account&lt;/option&gt;
                        &lt;option&gt;Income&lt;/option&gt;
                        &lt;option&gt;Expense&lt;/option&gt;
                        &lt;option&gt;Transfer&lt;/option&gt;
                    &lt;/select&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;div class=&quot;grid&quot;&gt;
                &lt;div class=&quot;col-4&quot;&gt;
                    &lt;label for=&quot;Description&quot;&gt;
                        Description
                        &lt;input type=&quot;text&quot; id=&quot;desc&quot; name=&quot;desc&quot; placeholder=&quot;Description&quot; required /&gt;
                    &lt;/label&gt;
                &lt;/div&gt;
                &lt;div&gt;
                    &lt;label for=&quot;Reference&quot;&gt;
                        Reference
                        &lt;input type=&quot;text&quot; id=&quot;ref&quot; name=&quot;ref&quot; placeholder=&quot;Reference&quot; /&gt;
                    &lt;/label&gt;
                &lt;/div&gt;
                &lt;div&gt;
                    &lt;label for=&quot;Amount&quot;&gt;
                        Amount
                        &lt;input type=&quot;text&quot; id=&quot;amount&quot; name=&quot;amount&quot; placeholder=&quot;Amount&quot; /&gt;
                    &lt;/label&gt;
                &lt;/div&gt;
                &lt;div&gt;
                    &lt;br /&gt;
                    &lt;input type=&quot;submit&quot; value=&quot;Add&quot; /&gt;
                &lt;/div&gt;
                &lt;/form&gt;
            &lt;/div&gt;
&lt;/div&gt;

My AddTransaction Component class

&lt;?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class AddTransaction extends Component
{
    /**
     * Create a new component instance.
     */

     public $data;
    public function __construct($data)
    {

       $this-&gt;data = $data;
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view(&#39;components.add-transaction&#39;);
    }
}

答案1

得分: 1

"我找到的解决方案是:“data是一个保留关键字,这就是为什么它对你不起作用。” 由driesvints提供(https://github.com/driesvints)"

英文:

The solution I found was "data is a reserved keyword, which is why this doesn't works for you." by driesvints (https://github.com/driesvints)

答案2

得分: 0

当使用 php artisan make:component AddTransaction 创建组件时,它会在 app/View/Components 中创建一个 Blade 文件和一个组件类,代码如下:

&lt;?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class AddTransaction extends Component
{
    /**
     * 创建一个新的组件实例。
     */
    public function __construct()
    {
        //
    }

    /**
     * 获取代表组件的视图/内容。
     */
    public function render(): View|Closure|string
    {
        return view('components.add-transaction');
    }
}

要能够将数据传递到组件中,您需要在 __construct() 函数中添加如下代码:

public function __construct(
    public string $data
) 
{
    //
}

请确保调整变量的类型。如果您不确定类型,可以将其省略。

希望对您有所帮助。 将数据从 Blade 视图传递给 Blade 组件

英文:

When creating a component with php artisan make:component AddTransaction it will create the blade file and a component class in app/View/Components which looks like this:

&lt;?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class AddTransaction extends Component
{
    /**
     * Create a new component instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view(&#39;components.add-transaction&#39;);
    }
}

To be able to pass data into the component, you have to add this to your __construct() function:

public function __construct(
    public string $data
) 
{
    //
}

Make sure to adjust the type of the variable.
If you’re unsure about the type you can just leave it out.

I hope this helps. 将数据从 Blade 视图传递给 Blade 组件

Source: https://laravel.com/docs/10.x/blade#passing-data-to-components

huangapple
  • 本文由 发表于 2023年5月14日 18:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定