Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorException Undefined variable: #40

Closed
Zheka2011 opened this issue Nov 2, 2020 · 8 comments
Closed

ErrorException Undefined variable: #40

Zheka2011 opened this issue Nov 2, 2020 · 8 comments
Labels
no-bug This doesn't seem like a bug question User is asking for help.

Comments

@Zheka2011
Copy link

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
Copy link
Owner

  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
Copy link
Author

Zheka2011 commented Nov 3, 2020

  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
Copy link
Owner

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
Copy link
Author

Zheka2011 commented Nov 3, 2020

{{$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
Copy link
Owner

tanthammar commented Nov 3, 2020

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

  • Then you are not passing the variable to your view.
  • Review your route and your controller.

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

  • Then you should not add the form tag to a view
  • Review your route definition
  • Reed more about Model Binding in the wiki

@tanthammar tanthammar added no-bug This doesn't seem like a bug question User is asking for help. labels Nov 3, 2020
@santhika29
Copy link
Contributor

santhika29 commented Nov 5, 2020

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
Copy link
Owner

tanthammar commented Nov 5, 2020

@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
Copy link
Owner

Closing due to lack of response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-bug This doesn't seem like a bug question User is asking for help.
Projects
None yet
Development

No branches or pull requests

3 participants