gfactor
gfactor

Hi, Am trying to attach a specific image/file to a record using the $attachOne relation. From the controller, everything works fine, the image upload successfully using the field.yaml file, but i can't seem to get the file uploaded from the component. Here is a piece of my code.

Model file:

use Model;
use Form;

class School extends Model
{
    use \October\Rain\Database\Traits\Validation;

 /**
 * @var string The database table used by the model.
 */
 public $table = 'schools';

  /**
      * @var array Guarded fields
  */
 protected $guarded = ['*'];

/**
   * @var array Fillable fields
 */
 protected $fillable = [
       'school_name', 
       'curriculums',
       'avatar',
    ];

   /**
     * @var array Relations
   */
    public $attachOne = ['avatar' => ['System\Models\File']];
    public $attachMany = [];
 }

Component file:

  /**
   * Add a school to the list
   */
   public function onCreateSch()
   {
      $school_data = [
          "school_name" => post('school_name'),
          "curriculums" => post('school_cur'),
          "avatar" => post('school_logo'),
      ];        

      $school = SchModel::create($school_data);
   }

default.htm file

 <form data-request="onCreateSch">
 <div class="form-group">  
    <input name="school_name" type="text" placeholder="{{ 'Enter school name'|_ }}" class="form-control" required="required" />
  </div>
  <div class="form-group">  
      <input name="school_cur" type="text" placeholder="{{ 'Enter school Curriculum'|_ }}" class="form-control" required="required" />
   </div>
   <div class="form-group">
       <input name="school_logo" type="file"  />
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-info"><strong>{{ 'SUBMIT'|_ }}</strong></button>
    </div>
</form>
Scott
Scott

I've never tried to attach files from the frontend before, but you could try something like this...

use System\Models\File;

public function onCreateSchool()
{
    $avatar = new File;
    $avatar->fromPost(post('school_logo'));
    $avatar->save();

    $school = SchModel::create([
        'school_name' => post('school_name'),
        'curriculums' => post('school_cur'),
    ]);

    $school->avatar()->add($avatar);
}

If passing in the post() helper to the File model doesn't work, you might need to use something like Input::file('school_logo') instead.

Last updated

gfactor
gfactor

Thanks Scott. But that only worked half way. I can see some records inserted in the system_files table. The row was added but these major fields came back empty: disk_name, file_name, file_size and content_type. It could not generate a thumbnail nor the actual image size in the controller view either. I also checked the storage directory, '/storage/app/uploads' no file was added.

This is what i have in the component file:

 public function onCreateSch()
{
    $avatar = new File;
    $avatar->fromPost(post('school_logo'));
    $avatar->save();

    $school_data = [
        "school_name" => post('school_name'),
        "curriculums" => post('school_cur'),
        //"avatar" => post('school_logo'),
    ];        

    $school = SchModel::create($school_data);

    $school->avatar()->add($avatar);
];
Scott
Scott

Ok, try changing the file creation to this...


use Input;

public function onCreateSch()
{
    $avatar = new File;
    $avatar->fromPost(Input::file('school_logo'));
    $avatar->save();

    // the rest should all be the same
}
gfactor
gfactor

Hi Scott, It returned the same empty fields. No file was added to the storage directory either

Scott
Scott

Try changing your opening <form> tag to this...

{{ form_open({ request: 'onCreateSch', files: true }) }}
    ...
</form>

Last updated

gfactor
gfactor

Great Scott!!! It worked. Both the storage directory and DB. In that case I would stick to the Laravel way of handling html rather than the normal html syntax(s).

Thank You.

Scott
Scott

Glad you got it working, and you could still write out the html manually if you want, the form function i posted is just a little html helper... This should produce the same result

<form data-request="onCreateSch" method="post" enctype="multipart/form-data">
    ...
</form>
gfactor
gfactor

Thanks Scott. Actually, my first try was

   <form data-request="onCreateSch" method="post" enctype="multipart/form-data">

and it didn't work. It brought back the same empty fields. But the previous one did. I mean:

   {{ form_open({ request: 'onCreateSch', files: true }) }}

Last updated

shoguniphicus
shoguniphicus

Nice, the document should have this in

{{ form_open({ request: 'onCreateSch', files: true }) }}
  <input type="file" name="file_input" />
{{ form_close() }}

Last updated

phamvuduycuong15027
phamvuduycuong15027

hi all, I was try as: {{ form_open({ request: 'onCreateSch', files: true }) }} But the form not load as ajax, it reload when I submit, and not run into my request function. Can someone help me, please!?

hussain.akhss201033621
hussain.akhss201033621

must include 'data-request-files' in the form tag, I love you octobarians

1-12 of 12