question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Field with same name as a function in model but not related.

See original GitHub issue

Bug report

If a model has a method with the same name as the db, it assumes that it is a relationship and this method is called.

What I did

My table have a searchable column and one of my fields name (in CrudController) is called searchable. However, Backpack thinks that searchable is a relationship, since it is also a method in my Model (from Scout to be precise).

What I expected to happen

A field without a relationship should be treated as a field without a relationship

What happened

A field without a relationship was guessed as a field with a relationship. The method is called and the app crashes.

What I’ve already tried to fix it

The only solution would be change my db to a name that is not the same as a method in my model.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP 7.4.5 (cli) (built: Apr 23 2020 02:25:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies

### LARAVEL VERSION:
v7.13.0@6fa69bfbd57744a5bbec5538ce483919b3fd625f

### BACKPACK VERSION:
4.1.6@cbd4143d3eb8302916012af205565cd3183f274f

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pxpmcommented, Jun 2, 2020

Hello @robertotcestari

Indeed this is something we changed in 4.1 to allow us to add fields quicker, with less attrition to the developer.

When we did it, I did not expect and I still don’t expect that much people use a field in the database with the same name of a relation that the field is not related with.

We introduced this change mainly to allow us to ‘guess’ relationship data from model. If you do: CRUD::addField('relation_name') we will check if there is a function in the model with the same name as relation_name, but we only do this check if there is no entity in field.

I think there are three possible solutions here:

- Change database field name if you are still developing and can afford it.
- Use a different field name and then use a mutator/acessor to manipulate the field.
- Use 'entity' => false in your field definition.

I think using 'entity' => false solves the problem. But we could agree that it’s not an elegant solution, nor a ver intuitive one.

Me and @tabacitu already talked about it and we discussed the possibility of checking if field name exists as a database column. It might introduce other unwanted caveats, so maybe not the best solution. Also this might not need a solution at all, I think this is a very uncommon scenario.

I will leave this open for a while, please give me your feedback on the solutions provided @robertotcestari so if anyone bumps in the same error they could fix it too.

Also if anyone else bumps into this same issue, please comment bellow 😃

Best,

0reactions
tabacitucommented, Jul 6, 2020

Update: #2971 just got merged, which fixes this by allowing entity => false. Thanks a lot @robertotcestari !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set Attribute With Same Name As Databse Field in Laravel ...
You already have a field named contact_number , but you want to defined a attribute contract_number, this is a mistake, try to use...
Read more >
How to correct a #NAME? error - Microsoft Support
You are using a function that requires a specific add-in that is not enabled in Excel · Click File > Options. · Click...
Read more >
Create a custom field - Atlassian Support
Learn how to create custom fields in Jira Cloud.
Read more >
Bug descriptions — spotbugs 4.7.3 documentation
This class defines a hashCode() method but not an equals() method. Therefore, the class may violate the invariant that equal objects must have...
Read more >
Effective Dart: Usage - Dart programming language
DON'T import libraries that are inside the src directory of another package. ... If a variable has a non-nullable type, Dart reports a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found