https://twill.io logo
Join Discord
Powered by
# ❓questions
  • k

    kalle

    03/10/2023, 11:27 AM
    Which Twill version do you use?
  • n

    Nechalon

    03/10/2023, 11:27 AM
    2.3.0.
  • k

    kalle

    03/10/2023, 11:29 AM
    Okey, just to clarify, you want current model in forms (blade) or somewhere else?
  • n

    Nechalon

    03/10/2023, 11:29 AM
    current model in forms (blade), yes
  • n

    Nechalon

    03/10/2023, 11:30 AM
    I can print out the data from database, id, and other columns, but I want to get email or name from another table through that id
  • n

    Nechalon

    03/10/2023, 11:30 AM
    its a foreign key
  • n

    Nechalon

    03/10/2023, 11:32 AM
    I should add a function in the model like
    getUserEmail
    and call that? 😄
  • k

    kalle

    03/10/2023, 11:35 AM
    Its doable for sure, in your Controller you can override parent method
    formData
    and that model/resolved query will be availble in the forms.
    Copy code
    php
        /*
         * Add anything you would like to have available in your module's form view
         * For example, relationship lists for multiselect form fields
         */
        protected function formData($request)
        {
            // Here make your query
            return [
                'model' => Model::getUserEmail(),
            ];
        }
    And in your form.blade.php you can access it by variable
    $model
    .
  • n

    Nechalon

    03/10/2023, 11:40 AM
    hmm, its throwing an error:
    Undefined variable $model
  • k

    kalle

    03/10/2023, 11:43 AM
    Have you set
    formData
    in the same module controller?
  • n

    Nechalon

    03/10/2023, 11:43 AM
    okay, my bad, I was doing it in the model, now it finds it, but its empty, even if it is a simple text, like
    'model' => 'test',
  • k

    kalle

    03/10/2023, 11:50 AM
    Okey, sorry, you have a easier way to get the model in the form, since its already passed. Try to
    @dd($item)
    in the form.
  • k

    kalle

    03/10/2023, 11:51 AM
    But still, above example should work and that version of Twill is pretty old, so debugging and searching now is waste of time.
  • n

    Nechalon

    03/10/2023, 11:53 AM
    dd($item) works, it gives back the data, if I try to access a fillable through that, like
    $item->id
    it still doesnt display it, only if I do
    'id'
    , any idea why?
  • k

    kalle

    03/10/2023, 11:57 AM
    No idea, that may be Laravel related issue.
  • i

    ifox

    03/10/2023, 11:59 AM
    What does dd($item) show? It should be an eloquent model instance, and if it is, I really double that ->id wouldn't work on it
  • k

    kalle

    03/10/2023, 12:00 PM
    I tested it on my latest 2.x stable, and $item->id works, so it could be problems with the old Twill/Laravel version.
  • n

    Nechalon

    03/10/2023, 12:02 PM
    it is, so thats why
  • n

    Nechalon

    03/10/2023, 12:03 PM
    if I do dd($item->id) it does print it in the debug, but without dd, it is just empty in the form
  • i

    ifox

    03/10/2023, 12:03 PM
    how are you using it to display it in the form?
  • n

    Nechalon

    03/10/2023, 12:03 PM
    'name' => $item->id,
  • i

    ifox

    03/10/2023, 12:04 PM
    name is for form field names, you would see that in the html, not displayed to the end user
  • i

    ifox

    03/10/2023, 12:05 PM
    what are you trying to do exactly by calling a field with the id of the record you're editing?
  • n

    Nechalon

    03/10/2023, 12:05 PM
    it would be a disabled form to show the user's data, like email or name, I only have the id in that table, so I want to get the email and name through that id I have
  • i

    ifox

    03/10/2023, 12:10 PM
    I'm not following you. You are storing the id of a user in the model you're currently editing, and you'd like to show more data about the user?
    User::find($item->id)
    sounds like what you're looking for in that case
  • n

    Nechalon

    03/10/2023, 12:12 PM
    Yes, user id is in the model that im editing, I get the data through the id, but I cant display it in the form. Where should I display it if not in the
    name
    field?
  • k

    kalle

    03/10/2023, 12:15 PM
    name
    is reserved as a reference to the Vuex store, so when component are loaded, form inputs by that
    name
    get data to input values. You may be looking for
    label
    in your case?
  • n

    Nechalon

    03/10/2023, 12:16 PM
    The label is just the name of the field? I want the data to be displayed inside the field
  • i

    ifox

    03/10/2023, 12:16 PM
    wait, I think I got it, you're trying to show a disabled field labelled "User name" that shows the name of the user from the id?
  • i

    ifox

    03/10/2023, 12:16 PM
    in that case you'll need to do it from the repository class
    getFormFields
1...475476477...484Latest