tanthammar / tall-forms

Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.
https://github.com/tanthammar/tall-forms/wiki
MIT License
693 stars 86 forks source link

ErrorException Undefined variable: #40

Closed Zheka2011 closed 4 years ago

Zheka2011 commented 4 years ago

Hello. I am new to laravel. I want to use your package for my project. But something does not work out, I have already read the entire wiki. Created a form php artisan make:tall-form createOrder --model=Orders --path=Forms --action=create

/app/Http/Livewire/Forms/createOrder.php

namespace App\Http\Livewire\Forms;

use App\Models\Orders;
use Livewire\Component;
use Tanthammar\TallForms\Input;
use Tanthammar\TallForms\TallForm;

class createOrder extends Component {

    use TallForm;

    public function mount( ? Orders $orders) {
        //Gate::authorize()
        $this->fill([
            'formTitle' => trans('global.create') . ' ' . trans('crud.orders.title_singular'),
            'wrapWithView' => false, //see https://github.com/tanthammar/tall-forms/wiki/installation/Wrapper-Layout
            'showGoBack' => false,
        ]);

        $this->mount_form($orders); // $orders from hereon, called $this->model
    }

    // Mandatory method
    public function onCreateModel($validated_data) {
        // Set the $model property in order to conditionally display fields when the model instance exists, on saveAndStayResponse()
        $this->model = Orders::create($validated_data);
    }

    // OPTIONAL method used for the "Save and stay" button, this method already exists in the TallForm trait
    public function onUpdateModel($validated_data) {
        $this->model->update($validated_data);
    }

    public function fields() {
        return [
            Input::make('Name')->rules('required'),
        ];
    }
}

resources/views/livewire/orders.blade.php

<livewire:forms.create-order :orders="$orders" />

I get an error

ErrorException
Undefined variable: orders (View: /var/www/autokit/resources/views/livewire/orders.blade.php)

Laravel 8 Livewire 2.3.1

tanthammar commented 4 years ago
  1. Regarding <livewire:forms.create-order :orders="$orders" /> Your error looks like it's coming from the view orders.blade.php and not the form generator. Do you have any model tied to that variable?

  2. Try to changing this:

    public function mount( ? Orders $orders)

to

public function mount(?Orders $orders)
  1. And make sure $orders is just ONE item, not multiple. (sounds like plural to me)
Zheka2011 commented 4 years ago
  1. Tried it in a different view, the same result.
  2. Changed. The same mistake.
  3. One

Changed the name of the variable to $order, writes the same, Undefined variable: order

tanthammar commented 4 years ago

Can you please temporarily remove the form tag and just echo out the $order to make sure it is nothing wrong with your variable.

Zheka2011 commented 4 years ago

{{$order}} -> Undefined variable: order I'm trying to figure out why I can't see the variable.

It seems to me that the variable is not passed from here

public function mount(?Orders $order) {
        //Gate::authorize()
        $this->fill([
            'formTitle' => trans('global.create') . ' ' . trans('crud.orders.title_singular'),
            'wrapWithView' => false, //see https://github.com/tanthammar/tall-forms/wiki/installation/Wrapper-Layout
            'showGoBack' => false,
        ]);

        $this->mount_form($order); // $orders from hereon, called $this->model
    }
tanthammar commented 4 years ago

If I understand you correctly you still get the error when you remove the form from your view?

Or are you trying to use a route that returns the Livewire full page component directly?

santhika29 commented 4 years ago

i got same problem... i hope you can give us some demo to use the element of this package... edited : i got my form work after declare public variable and add it to my main controller

tanthammar commented 4 years ago

@Zheka2011 and @santhika29

The problem you are experiencing is not due to this package. It is due to that you are trying to pass an undefined variable to the component.

It is standard php, that you cannot use an undefined variable.

In basic Laravel you create a route, you create a controller where you define a variable and you create a view, where you can echo that variable. You can also create routes with optional model binding.

With default Livewire you create components. You can pass variables to those components. But the variable must exist.

The value of the variable can be null.

Please read Laravel documentation on how to pass variable to Blade components. Then read Livewire documentation on how to pass variables to Livewire inline components AND how to use Livewire routes with full-page components

This package is just a Trait for a Livewire component. It behaves like a standard Livewire component.

  1. you can put it in a view and pass it a variable (inline component)
  2. you can create a route that points directly to the component (full-page component)
  3. apart from that, this package offers a way to wrap your components with a view and/or dynamically define a layout, both techniques are described on the wrapper wiki page. The layout definition is also described in the Livewire link below.

Livewire documentation on how to render inline and full-page components: https://laravel-livewire.com/docs/2.x/rendering-components

You have not told me if you are trying to render a full-page component or if you are applying the component in a view. It is impossible for me to help you further before you understand the Livewire basics.

But I can assure you that the problem you are describing does not exist in this package.

tanthammar commented 4 years ago

Closing due to lack of response.