Giter VIP home page Giter VIP logo

Comments (10)

jasekz avatar jasekz commented on June 16, 2024

Hey Devin,
Thanks for bringing this to my attention. The reason that this wasn't working for you, was because of a bug in the javascript file. This is now fixed, if you want to update your code. Alternatively, you can use the event handler method below if you don't feel like doing an update. With that being said, here is how you can accomplish your goal:

EVENT HANDLER (preferred - this keeps your code nicely decoupled)

  1. In your app/Providers/EventServiceProvider.php file, add a listener:
protected $listen = [
  'Jasekz\Laradrop\Events\FileWasUploaded' => [
    'App\Handlers\Events\LaradropFileWasUploaded',
  ],
];
  1. Generate the handler with:
    artisan handler:event 'App\Handlers\Events\LaradropFileWasUploaded' --event='Jasekz\Laradrop\Events\FileWasUploaded'

  2. In the newly generated handler file - app/Handlers/Events/LaradropFileWasUploaded.php, change:
    use App\Events\Jasekz\Laradrop\Events\FileWasUploaded
    to
    use Jasekz\Laradrop\Events\FileWasUploaded

  3. In the handle() function, you can now access the uploaded file object and perform your business logic as needed. For example:

public function handle(FileWasUploaded $event) {
  $file = $event->data['file'];
  $file->findimage = 3;
  $file->update();
}

CONTROLLER OVERRIDE METHOD

  1. Create the controller (as you have), but you can skip all the laradrop code and just add your business logic. The parent returns a file object you can utilize. For example:
public function store() {
  try {
    $file = parent::store();
    $file->findimage = 1;
    $file->update();
  } 
  catch (Exception $e) {
    return $this->handleError($e);
  }
}
  1. Make sure your controller is name spaced correctly:
    namespace App\Http\Controllers;
  2. In your view, all you need is the upload handler.
<div class="laradrop"
    laradrop-upload-handler="/mediaStore"
    laradrop-csrf-token="{{ csrf_token() }}">
</div>

I didn't get into the Auth::user() functionality because that's outside of the scope, but you should be able to use it in the normal Laravel way. Let me know if this helps and/or if the issue persists.

Thanks,
Zig

from laradrop.

devingray avatar devingray commented on June 16, 2024

Hi Zig and thanks for the response. I tried both methods and they seem to not be working. I would prefer the controller override method, as I can then get it to work as I need it, but I am still not hitting my controller.

I have this code below. My Goal is to assign findimage the users ID. So that I can collect only images assigned to that user. I do not want to use folders. Its one image per user.

<div class="laradrop"
              laradrop-file-source="/mediaIndex" 
              laradrop-upload-handler="/mediaStore"
              laradrop-csrf-token="{{csrf_field()}}">
          </div>

This is where I instantiate my laradrop.

and my routes

//User Media Files 
Route::get('mediaIndex', 'UserMediaController@index');
Route::post('mediaStore', 'UserMediaController@store');

But for some reason it is still not getting to that controller. I also want to crop and resize the image in the controller so over riding it would be my best option.

But am at a loss as to why this is not happening. New controller below

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Jasekz\Laradrop\Http\Controllers\LaradropController;
use Input, Auth;

class UserMediaController extends LaradropController {
    
    /**
     * Upload and store new file.
     *
     * @return JsonResponse
     */
    public function store()
    {

        if (!Auth::user()->currentTeam()){
            $eitherTeamOrUser = \Auth::user()->finduser;
        }

        elseif(Auth::user()->currentTeam()){
            $eitherTeamOrUser = \Auth::user()->currentTeam()->findteam;
        }

        try {
            $file = parent::store();
            $file->findimage = $eitherTeamOrUser;
            $file->update();
          } 
          catch (Exception $e) {
            return $this->handleError($e);
          }

    }

from laradrop.

devingray avatar devingray commented on June 16, 2024

Oh yes and this command php artisan handler:event 'App\Handlers\Events\LaradropFileWasUploaded' --event='Jasekz\Laradrop\Events\FileWasUploaded' isnt working for some reason. Am on 5.3

from laradrop.

jasekz avatar jasekz commented on June 16, 2024

I'm testing this on 5.3 now and expect to have a solution for you shortly, but let me know if you find something before then. Thanks for taking the time to work through this.

from laradrop.

jasekz avatar jasekz commented on June 16, 2024

Try this:

  1. Delete the /public/vendor/jasekz directory.
  2. Re-install the latest version of laradrop.
  3. Re-run artisan vendor:publish

Let me know how it goes.

from laradrop.

devingray avatar devingray commented on June 16, 2024

Hi Zig. It is wierd, am still not hitting my controller. I think the solution to my problem will be to create a trait for the images, then on successfull upload, send the image ID to my trait and just run a raw DB query to update this field in the Database, then return back the way you have it working. Am trying it out now. Such a rad package :D

from laradrop.

devingray avatar devingray commented on June 16, 2024

Okay :D This is my final answer, yes I am sure, Lock it in! ;) Found best solution here https://laracasts.com/discuss/channels/general-discussion/l5-service-provider-for-sharing-view-variables

from laradrop.

jasekz avatar jasekz commented on June 16, 2024

Ok, thanks, Dev. Yeah, that's strange because I can get this to work as expected on my end. I'll go ahead and close this out, but one more thing you may want to verify in your browser's network inspector, is that when you do an upload, the ajax call actually goes to /mediaStore, and not /laradrop. If it doesn't, it's most likely that you're running off of the old laradrop.js file.

from laradrop.

devingray avatar devingray commented on June 16, 2024

Just a last bit of info regarding this. I was an idiot and overlooked the easiest option.

onSuccessCallback(){
do an ajax request to my controller
}

from laradrop.

jasekz avatar jasekz commented on June 16, 2024

👍

from laradrop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.