Unable to upload/save image



  • I am trying to upload image using new created module "Testimonial" .

    I use following in create blade
    @include('media::admin.fields.new-file-link-single', [
    'zone' => 'user_img'
    ]);

    Included "MediaRelation" Trait in entity but no success. I can add image which get displayed in form but didn't save when form is submitted.

    I get same issue when I upload image in WYSIWYG editor in page module.


  • admin

    Hello,

    Did you follow the steps outlined in the documentation?

    1. adding trait
    2. adding partial
    3. trigger event that implements interface

    Do you see records in the imageables table ?



  • I have added Trait and partial, I guess the issue is with triggering event. How do we trigger event? documentation lack example and I am new to this.


  • admin

    You can look at the blog module events: https://github.com/AsgardCms/Blog/blob/master/Events/PostWasCreated.php

    These are triggered in the EloquentPostRepository.


  • Global Moderator

    Marked as solved.
    Due to no response from OP on potential/actual solution proposal for over 1 month.

    If OP or someone else thinks this is not still solved, please respond with problem/concern.



  • @nWidart said in Unable to upload/save image:

    You can look at the blog module events: https://github.com/AsgardCms/Blog/blob/master/Events/PostWasCreated.php

    These are triggered in the EloquentPostRepository.

    Having same problem described in this post.
    Question may appear as dummy, but i really don't know how to trigger created event.
    Can you please write the code line. And where it should be put. Thank you.



  • I don't know too how to trigger created event., but i try to understand how to edit function method work find in youtube, and after read function i write in my store function in controller admin

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request, FileRepository $FileRepository)
    {
        $entity = $this->testimonial->create($request->all());
    
        $entity->files()->attach($request->medias_single['gallery'], [
            'imageable_type' => 'Modules\\Testimonials\\Entities\\Testimonial',
            'zone' => 'gallery',
        ]);
        flash()->success(trans('core::core.messages.resource created', ['name' => trans('testimonials::testimonials.title.testimonials')]));
        return redirect()->route('admin.testimonials.testimonial.index');
    }
    

    It's work, now when i create a new testimonial, a new row is create in table media__imageables.
    But i don't know if is a good way to do it.



  • @Bob Thank you. It really works, but for @mediaSingle('gallery') only.
    If I'm using @mediaMultiple('gallery') then it's not working properly, I've tried to type
    $request->medias_multiple['gallery'] in code it didn't worked either.


  • Global Moderator

    The code posted by @Bob is not the recommended way of doing attaching media (even if it works).

    At the step of linking media to the entity:

    1. You have to create event class that implements Modules\Media\Contracts\StoringMedia
      Example of event class MyModule/Events/FakeMediaEvent.php:
    <?php
    
    namespace Modules\MyModule\Events;
    
    use Modules\Media\Contracts\StoringMedia;
    
    class FakeMediaEvent implements StoringMedia
    {
        public $entity;
        public $data;
    
        public function __construct($entity, $data)
        {
            $this->entity = $entity;
            $this->data = $data;
        }
    
        public function getEntity()
        {
            return $this->entity;
        }
    
        public function getSubmissionData()
        {
            return $this->data;
        }
    }
    
    
    1. Trigger this event in store and update method of controller that works with media for example MyModule/Http/Controllers/Admin/FakeController.php:
    <?php
    
    namespace Modules\MyModule\Http\Controllers\Admin;
    
    use Modules\MyModule\Events\FakeMediaEvent;
    
    class FakeController  extends AdminBaseController 
    {
        public function store(StoreFakeRequest $request)
        {
            $fake = $this->fake->create($request->all());
    
            event(new FakeMediaEvent($fake, $request->all()));
    
            return redirect()->route('admin.mymodule.mymodule.index')
                ->withSuccess(trans('core::core.messages.resource created', ['name' => trans('mymodule::mymodules.title.mymodules')]));
        }
    }
    

    Thats it, whenever you store or update record media will be linked and relinked correctly.

    P.S: Parts of controller stripped



  • @armababy Thank you so much! It works perfectly. :-)


Log in to reply