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

Dynamic Snippet Form problem #17

Open
diaafares opened this issue Jun 3, 2021 · 23 comments
Open

Dynamic Snippet Form problem #17

diaafares opened this issue Jun 3, 2021 · 23 comments
Projects

Comments

@diaafares
Copy link

diaafares commented Jun 3, 2021

Hi,
At first, thanks for your efforts doing this great app.

Today I have an idea about to make Snippet Form dynamic, so I did something like this:

use App\Models\User;

$users = User::selectRaw('first_name as label, id as value')->get()->toArray();

$field_user = [
    'value' => '',
    'label' => 'User',
    'description' => 'Select a user.',
    'type' => 'select',
    'options' => $users, 
];

But as soon as I click the Snippet Form button, I get this white empty screen:
image

I was trying to populate the User select from the DB, also I was planning to use the $field_user value as an input for a second DB query, so the chosen value for $field_user will customize the result of the second query!

I know maybe the feature was not design to work as I wanted, but when I think about it, supporting dynamic Snippet Form will make the possibilities endless for Tinkerun

Also another request: I will be grateful if you implemented a self-update feature in Tinkerun, so we always have the latest and greatest without checking Tinkerun website every week or so

@billyct
Copy link
Member

billyct commented Jun 4, 2021

yes, it not support the dynamic Snippet Form. your idea is so cool.

I will try to do the auto-update.

@billyct
Copy link
Member

billyct commented Jun 5, 2021

@diaafares do you have any other use case of dynamic Snippet Form?

@diaafares
Copy link
Author

diaafares commented Jun 5, 2021

Hi @billyct
I explained my use case at the previous comment, that I want to create dynamic Snippet Form input that are populated from the Database, then use these inputs values as parameters for DB queries to get the final results.

For example: select a user to get how many posts he have and other statistics.
The possibilities are endless, you can whatever you want after getting this feature.

I have an idea about how you may implement this feature:
Maybe we can use a prefix for variables that we want to make them dynamic, like adding: $dynamic_ to the variable $dynamic_users and when you click the Snippet Form button, Tinkerun will search for these variables that have $dynamic_ prefix and run them in Tinker to get their values and cache it, after that it should be able to render the Snippet Form with the right values.

Just wanted to give you an idea how you can implement this feature, but maybe you have a better solution for this ☺️

@billyct
Copy link
Member

billyct commented Jun 5, 2021

@diaafares I have done a simple auto-update for mac in v0.2.4 😉

thx for your solution, 🤔 I will think about it.

@diaafares
Copy link
Author

@billyct oh, great work, thank you very much
BTW, in the v0.2.4, I tried the dynamic Snippet Form example code above and it didn't give me an empty screen! instead it display the select input but the select was empty:
image

@billyct
Copy link
Member

billyct commented Jun 6, 2021

yes, I have not done the dynamic Snippet Form yet.

how about writing the dynamic data in the closure

for your case above:

$field_user = [
    'value' => '',
    'label' => 'User',
    'description' => 'Select a user.',
    'type' => 'select',
    'options' => function() {
		return User::selectRaw('first_name as label, id as value')->get()->toArray();
	}, 
];

with the closure, so I can just run code in the closure only with tinker for getting the dynamic data.

@billyct billyct added this to TODO in Tinkrun Jun 6, 2021
@diaafares
Copy link
Author

diaafares commented Jun 6, 2021

how about writing the dynamic data in the closure

@billyct oh, great idea, way better than what I suggested!

It will be great if we can do it like this:

$options = function() {
    return User::selectRaw('first_name as label, id as value')->get()->toArray();
};

$field_user = [
    'value' => '',
    'label' => 'User',
    'description' => 'Select a user.',
    'type' => 'select',
    'options' => $options, 
];

Is that possible? ☺️

@billyct
Copy link
Member

billyct commented Jun 13, 2021

Dynamic Snippet Form

😉 Already available in v0.2.5

@diaafares
Copy link
Author

diaafares commented Jun 13, 2021

@billyct awesome work, thank you very much ^_^
Ca you please add it to the change log, I can't wait to tell my teammates about it ☺️

@diaafares
Copy link
Author

diaafares commented Jun 13, 2021

@billyct BTW, I tried to get v0.2.5 by using auto update feature, but it seems like it didn't work, I opened Tinkerun and it was downloading the new version in the background, I waited until the network activity stopped (it should mean that the download finished) but nothing happened, I restarted the app and I still have v0.2.4

Also when I tried the latest version v0.2.5, I get this error:
image

I suspect that the cause of the error is that I have many records in the users table, can you please 10000 fake records using UserFactory and test Tinkerun with that

@billyct
Copy link
Member

billyct commented Jun 14, 2021

@diaafares I will check the auto-updater.

And Tinkerun cannot handle 10000 records, because it is only simulating Tinker’s Terminal input.

if I do the 10000 records in the terminal, Tinker will just send me something like below:

App\Models\User {#5477 …6},
App\Models\User {#5478 …6},
App\Models\User {#5479 …6},
App\Models\User {#5480 …6},

I think you should add some conditions for your query 😂

@diaafares
Copy link
Author

diaafares commented Jun 14, 2021

@billyct Before I posted my last comment, I already added limit(3) to the query and I have the same error!
I think the cause of the problem is that when you did a query to table that have a large data, it will take some time to execute it even if the results are small.

Or maybe the problem is different than my thoughts I really don't know

@billyct
Copy link
Member

billyct commented Jun 14, 2021

@diaafares how about just run some code like below in tinker

json_encode((function() {
    return User::selectRaw('first_name as label, id as value')->limit(3)->get()->toArray();
})())

I get the dynamic data in the background by this code.

@diaafares
Copy link
Author

@billyct I think the problem is happen because of the import of the User model when Tinkerun execute the closures, as my App have custom setup and the User model is not placed as normal Laravel apps, I ran the code inside Tinker from terminal and it give me the following error:

Aliasing 'User' to 'User\Contracts\User' for this Tinker session.
PHP Error:  Call to undefined method User\Contracts\User::selectRaw() in /www/testappeval()'d code on line 2

The problem is I added the right import inside Tinkerun, but as I said before, it seems like Tinkerun doesn't recognize the imports inside the closures, what you think ☺️

@billyct
Copy link
Member

billyct commented Jun 15, 2021

@diaafares yes, Tinkerun can't recognize the things outside the closures.

so in your case, you should use

App\Models\User::selectRaw('first_name as label, id as value')->limit(3)->get()->toArray();

not good, but I think it will work 😂

Because Tinkerun does not depend on the PHP runtime, so it's quite not easy to do the Dynamic Snippet Form perfectly.

@diaafares
Copy link
Author

diaafares commented Jun 15, 2021

@billyct yes it worked, but it will be great if Tinkerun returned the error when you hit the Snippet form button, currently if any error happened inside the closure, Tinkerun freezes and you have to Force quit it without knowing whats the problem is.

Try this inside Tinkerun to see whats happen:

$users = function() {
    return User::selectRaw('non_exist_column as label, id as value')->limit(3)->get()->toArray();
};

$field_user = [
    'value' => '',
    'label' => 'User',
    'description' => 'Select a user.',
    'type' => 'select',
    'options' => $users, 
];

Notice that I added non_exist_column in the query, Tinkerun will freeze without display the error to the user, but if you ran the same closure inside terminal tinker you will get the following error message:
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'non_exist_column' in 'field list'

@billyct
Copy link
Member

billyct commented Jun 15, 2021

@diaafares yes, I think so. I will do it when I am free

@diaafares
Copy link
Author

@billyct great, can't wait ☺️ thank you so much

@diaafares
Copy link
Author

diaafares commented Sep 23, 2021

Hey @billyct
Since I upgrade to PHP8 I can't use Dynamic snippet form with something like User::selectRaw('first_name as label, id as value')->limit(3)->get()->toArray();

I get the same error as here and when I click OK, tinkerun stuck and I have to force quit it:
image

I will be grateful if you look at the issue and update Tinkerun to not stuck and show the real error message intead

billyct added a commit that referenced this issue Sep 23, 2021
billyct added a commit that referenced this issue Sep 24, 2021
@billyct
Copy link
Member

billyct commented Sep 24, 2021

@diaafares

I have added the error message now, no stuck 😄

@diaafares
Copy link
Author

diaafares commented Sep 24, 2021

@billyct Thank you so much!
Now Tinkerun show me this error:

Error invoking remote method 'parsePhpForm': Error: json_encode((function() {
... return Illuminate\Support\Facades\DB::table('admins')->selectRaw('email as label, id as value')->limit(3)->get()->toArray();
... })())
=> "[{"label":"santa.larson@example.com","value":46},{"label":"mschulist@example.org","value":47},{"label":"terry.bud@example.org","value":48}]"
>>>

I tried a different table and a different project and I have the same error always, I don't know why 😞

@billyct
Copy link
Member

billyct commented Sep 24, 2021

@diaafares I think it may be because Laravel/Tinker has no color by default.

laravel/tinker#127

checkout the new version, and let me know if you still have problems.

@diaafares
Copy link
Author

@billyct
it is working now, you are amazing
but I have another problem now, after I tested the dynamic snippet form feature, when I hit command + q to quit Tinkerun, Tinkerun is now freezing and I have to force quit it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants