You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the best method to store multiple files with a single record. I have a need to store an image, an mp3, and an mp4 file and associate them with a single record. I don't really need a one-many relationship since there will never be more than one of each type of file being stored. My implementation works great if I just select one file with my form, however, if I select more than one the save function errors out with this error:
You should be able to do this by defining three different attachment types (imgfile, mp3file, and mp4file) on your sermon model and then passing those fields in as properties from your form (which is what I assume you're doing).
There are a couple of things to keep in mind here though:
Your mp3file and mp4file attachments shouldn't have any styles defined for them (since they aren't images), so you shouldn't even need to populate the styles array in the attachment config when declaring those (inside the constructor). The default settings should work.
Your properties on your form (that you're setting on the sermon model inside your handleCreate method) need to be named the same as their corresponding attachment declarations inside the Sermon model.
You need to create fields in your database table for each attachment you define on the model (imgfile_file_name, imgfile_file_size, etc). This is covered in the documentation. You should have 12 of them, 4 for each of your 3 attachment types. Make sure they're added to the fillable array of your Sermon model.
The SQL error you're receiving about the title field being null isn't related to stapler. When you call save on the sermon model, it's only going to work on properties that are defined as attachments on the model. It won't touch any of the other properties (such as title, desc, etc). If you want, I can write up an example (model, form, and controller) of how I would handle this situation (multiple attachments on the same model).
An example would be fantastic! I believe I have followed the procedure you laid out above but both the 'create' form and the 'edit' form fail after storing the first file. Here are the code snippets for this app:
Model:
class Sermon extends Eloquent
{
use Codesleeve\Stapler\Stapler;
I've built out a sample app using your code and am unable to replicate the problem. What version of Laravel are you using? Also, what's does a dd(Input::all()) give you inside your controller (when you attempt to upload multiple files)? Is the title property there inside your Input array?
Here is the dump from Input::all as you suggested. I did discover an issue with my configuration. I didn't allow for a large enough file size for apache server. I have increased the file size and the save did work. I am conducting further testing now., there seems to be an error being thrown in the output below for each file. I have separated these with line breaks below. Do you know what these are?:
array(8) { ["title"]=> string(15) "The Bucket List" ["subtitle"]=> string(15) "The Bucket List" ["speaker"]=> string(15) "The Bucket List" ["desc"]=> string(15) "The Bucket List" ["msgdate"]=> string(16) "2014-01-18T01:00" ["imgfile"]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(23) "Remembering911_logo.png" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "image/png" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(128526)
Those are Symfony Uploaded File objects. They're part of Symfondy\Httpfoundation and they're what Laravel uses by default now (since version 4) for handling uploaded files. I don't see any errors there.
Thank you for the kind words. When I was building a test app using the code that you gave me, I couldn't help but notice that you're manually setting all of your properties both your handleEdit and handleCreate controller methods. I know I told you in my previous comment that you should set the database specific fields (12 of them for your case) to the fillable array, but I forgot that since the last two releases this isn't necessary anymore and in fact, if you add the name of your stapler attachments to the fillable array (along with the other fields on the Sermon model), you can do something like this:
In your Sermon.php model, set your fillable array:
/*** An array of properties that are fillable on this model.* * @var array*/protected$fillable = [
'title',
'subtitle',
'speaker',
'desc',
'msgdate',
'imgfile',
'mp3file',
'mp4file'
];
In your handleCreate method, simply pass in the attributes when creating your Sermon instance:
In essence, there's really no reason for you to have to manually set each of your fields in your controller (even your uploaded file fields; stapler will take care of those for you). Just a thought. Good luck with your app and thanks again for the kind words.
Activity
tabennett commentedon Jan 15, 2014
You should be able to do this by defining three different attachment types (imgfile, mp3file, and mp4file) on your sermon model and then passing those fields in as properties from your form (which is what I assume you're doing).
There are a couple of things to keep in mind here though:
The SQL error you're receiving about the title field being null isn't related to stapler. When you call save on the sermon model, it's only going to work on properties that are defined as attachments on the model. It won't touch any of the other properties (such as title, desc, etc). If you want, I can write up an example (model, form, and controller) of how I would handle this situation (multiple attachments on the same model).
GDLaw commentedon Jan 15, 2014
An example would be fantastic! I believe I have followed the procedure you laid out above but both the 'create' form and the 'edit' form fail after storing the first file. Here are the code snippets for this app:
Model:
class Sermon extends Eloquent
{
use Codesleeve\Stapler\Stapler;
}
Sermon data table:
Field Type Null Key Default Extra
id "int(10) unsigned" NO PRI NULL auto_increment
title varchar(128) NO NULL
subtitle varchar(255) NO NULL
desc varchar(512) NO NULL
speaker varchar(128) NO NULL
msgdate datetime NO NULL
created_at timestamp NO "0000-00-00 00:00:00"
updated_at timestamp NO "0000-00-00 00:00:00"
imgfile_file_name varchar(255) YES NULL
imgfile_file_size int(11) YES NULL
imgfile_content_type varchar(255) YES NULL
imgfile_updated_at timestamp YES NULL
mp3file_file_name varchar(255) YES NULL
mp3file_file_size int(11) YES NULL
mp3file_content_type varchar(255) YES NULL
mp3file_updated_at timestamp YES NULL
mp4file_file_name varchar(255) YES NULL
mp4file_file_size int(11) YES NULL
mp4file_content_type varchar(255) YES NULL
mp4file_updated_at timestamp YES NULL
Sermon Controller for create and edit:
Create Form:
Edit Form:
Sorry for the lengthy comment. Thank you so much for your help!
tabennett commentedon Jan 15, 2014
I've built out a sample app using your code and am unable to replicate the problem. What version of Laravel are you using? Also, what's does a dd(Input::all()) give you inside your controller (when you attempt to upload multiple files)? Is the title property there inside your Input array?
GDLaw commentedon Jan 15, 2014
Travis,
Here is the dump from Input::all as you suggested. I did discover an issue with my configuration. I didn't allow for a large enough file size for apache server. I have increased the file size and the save did work. I am conducting further testing now., there seems to be an error being thrown in the output below for each file. I have separated these with line breaks below. Do you know what these are?:
array(8) { ["title"]=> string(15) "The Bucket List" ["subtitle"]=> string(15) "The Bucket List" ["speaker"]=> string(15) "The Bucket List" ["desc"]=> string(15) "The Bucket List" ["msgdate"]=> string(16) "2014-01-18T01:00" ["imgfile"]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(23) "Remembering911_logo.png" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "image/png" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(128526)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0)
["pathName":"SplFileInfo":private]=> string(24) "C:\xampp\tmp\php2668.tmp" ["fileName":"SplFileInfo":private]=> string(11) "php2668.tmp" } ["mp3file"]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#10 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(35) "2010-09-19 Bucket List - Part 1.mp3" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "audio/mp3" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(20759363)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0)
["pathName":"SplFileInfo":private]=> string(24) "C:\xampp\tmp\php2688.tmp" ["fileName":"SplFileInfo":private]=> string(11) "php2688.tmp" } ["mp4file"]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#11 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(70) "I Miss You Performed By Uriah Shelton and Dash Mihok (With Lyrics).mp4" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "video/mp4" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(12378137)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0)
["pathName":"SplFileInfo":private]=> string(24) "C:\xampp\tmp\php284E.tmp" ["fileName":"SplFileInfo":private]=> string(11) "php284E.tmp" } }
tabennett commentedon Jan 15, 2014
Those are Symfony Uploaded File objects. They're part of Symfondy\Httpfoundation and they're what Laravel uses by default now (since version 4) for handling uploaded files. I don't see any errors there.
GDLaw commentedon Jan 15, 2014
Great! - Thanks for your guidance Travis. I love your product and I will spread the word.
Thanks again for your help.
Glen
tabennett commentedon Jan 16, 2014
Thank you for the kind words. When I was building a test app using the code that you gave me, I couldn't help but notice that you're manually setting all of your properties both your handleEdit and handleCreate controller methods. I know I told you in my previous comment that you should set the database specific fields (12 of them for your case) to the fillable array, but I forgot that since the last two releases this isn't necessary anymore and in fact, if you add the name of your stapler attachments to the fillable array (along with the other fields on the Sermon model), you can do something like this:
In your Sermon.php model, set your fillable array:
In your handleCreate method, simply pass in the attributes when creating your Sermon instance:
Similarly, your handleEdit method could look like this:
In essence, there's really no reason for you to have to manually set each of your fields in your controller (even your uploaded file fields; stapler will take care of those for you). Just a thought. Good luck with your app and thanks again for the kind words.
GDLaw commentedon Jan 16, 2014
Thanks for this info Travis, it worked perfectly. One other thing, is there any way to validate a specific mime type on a Input::file?
tabennett commentedon Jan 16, 2014
I would just use Laravel's built in validation for that: http://laravel.com/docs/validation#rule-mimes