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

Attribute Casting causes error #66

Closed
iedmrc opened this issue Jul 22, 2016 · 12 comments
Closed

Attribute Casting causes error #66

iedmrc opened this issue Jul 22, 2016 · 12 comments
Milestone

Comments

@iedmrc
Copy link

iedmrc commented Jul 22, 2016

Hi, I use eloquent attribute casting and I have
protected $casts = [ 'mentions' => 'array', ];

in my model. It causes

ErrorException in 90d0d21803ea756d9c9e49ba82191f0eef2d2e2d.php line 97: strip_tags() expects parameter 1 to be string, array given (View: ......\vendor\backpack\crud\src\resources\views\list.blade.php)

error when I hit the admin/somecrudpage .If I remove $casts, there is no problem. So, I think Backpack/Crud needs to support eloquent attribute casting.
I will be happy if you help me to makeshift for this issue because I have a project to done. Or I prefer if you commit for solving this problem :)

@tabacitu
Copy link
Member

Hi @dmrc1143 ,

You're right, Backpack should support attribute casting. And I think the change is as easy as replacing $entry->{$column['name']} with $entry->getOriginal({$column['name']}).

The change isn't very difficult, but needs to be done in a lot of blade files (all field types, all column types, all button types).

I'm on it, but it will take a few days to do it all and properly test it. If you're in a rush, you can do it yourself for the few field types you need, until I get to change them all.

Cheers!

@iedmrc
Copy link
Author

iedmrc commented Jul 22, 2016

Thank you for your interest,
I'm goingt to try fix with $entry->{$column['name']} with $entry->getOriginal({$column['name']}) for blade files that I need and after your fix, I will update project when you have fixed completly. I will be happy if you inform me.

@tabacitu
Copy link
Member

Great. Will do!

@tabacitu
Copy link
Member

tabacitu commented Jul 23, 2016

Hi @dmrc1143 ,

It's done in 3.0.1.. The fix was easier than anticipated:

  • I created a new column type that I think is perfect for your use case (it's called array, see the docs);
  • I edited the date and datetime field types to also support attribute casting;

As far as I can figure it out, we've now covered most attribute castings:

  • integer - works;
  • real - works;
  • float - works;
  • double - works;
  • string - works;
  • boolean - works;
  • object - DOES NOT WORK;
  • array - works;
  • collection - DOES NOT WORK;
  • date - works;
  • datetime - works;

But I don't think we should support casting to objects or collections. They can be so different that it's super-difficult to create fields or columns for them... I think for collections and objects, the developers should build their own column or field types.

@iedmrc
Copy link
Author

iedmrc commented Jul 23, 2016

Thank you for fixing,
i will update it 👍

@tabacitu tabacitu added this to the 3.0 milestone Jul 28, 2016
@NenadZivanovic
Copy link

Hi,
I have the same problem but I'm not sure if my settings are correct. I have
one field in migration declared as text for article images. I used upload_multiple
input for images. I inserted this in my model

protected $casts = [ 'images' => 'array' ];

Everything works fine, just one part. When I insert new article with images, but when I want
to access that article in table veiw, system is generate error. When I comment $casts part, I can
see table records. Problem is that my database field is text, which table type I must use?

@tabacitu
Copy link
Member

Hi @NenadZivanovic

If you error only shows up on the table view, it means the problem is with the column type you're using to display that array. What column type are you? If you use a "text" column, naturally it will throw an error, because you're trying to display an array as text. You should use the array column type or write your own if none fit your needs.

Cheers!

@NenadZivanovic
Copy link

NenadZivanovic commented Oct 16, 2016

Hi @tabacitu,

Thank you for fast response. I am using

$this->crud->addField([  // Select2
            'label' => "Category",
            'type' => 'select2',
            'name' => 'category_id', // the db column for the foreign key
            'entity' => 'article_category', // the method that defines the relationship in your Model
            'attribute' => 'name', // foreign key attribute that is shown to user
            'model' => "App\Models\Article_category" // foreign key model
         ]);

in my controller, and how I can add array column type? In migration or model?

@tabacitu
Copy link
Member

If you were editing Cars, for example, you'd have a CarCrudController. Right? Well, in your CarCrudController, in the __construct() or setup() method you're defining how the CRUD looks and works. Part of that is defining what fields you have and what columns you have.

So that's where you need to poke around - CarCrudController. You can find the link to the array column type in my previous comment. Take a look at all available functions to understand what you can do inside __construct or setup().

Cheers!

@NenadZivanovic
Copy link

Sorry, in last post I added wrong code. In my controller I have this code in constructor
$this->crud->addField([ // Upload 'name' => 'images', 'label' => 'Photos', 'type' => 'upload_multiple', 'upload' => true, 'disk' => 'uploads' ]);
and my column type in migration is text. How I can add array column type, when I used addField method for that field?

@NenadZivanovic
Copy link

I founded solution, I realized that i addField addColumn are different methods. Thank you very much!

@sayankanatov
Copy link

Hello! I have one problem with multiple uploadings, error say strip_tags() expects parameter 1 to be string, array given (View: C:\xampp\htdocs\vendor\backpack\crud\src\resources\views\columns\text.blade.php) Pliz help me< thank u

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

No branches or pull requests

4 participants