show your uplod logo file? from App\Jobs\UploadLogo
App\Jobs Class not found
I am handling an image upload through a Job, somehow my Controller can't find the Job class, even though I am using it.
What could be the problem? I am also facing problems with my queues table, where no jobs will be stored, for example my SendVerificationMail Job worked, but was not stored within the table.
uploadLogo method
use App\Jobs\UploadLogo;
public function uploadLogo(Request $request, Partner $partner){
// Validation
if($request->hasFile('logo')){
if ($request->file('logo')->isValid()) {
$request->file('logo')->move(
storage_path() . '/uploads',
$fileId = uniqid(true) . '.png'
);
dispatch(new UploadLogo($partner, $fileId));
}
}
return redirect('partner.profile.logo');
}
Error:
Class 'App\Jobs\UploadLogo' not found
I had a typo within the Job file, solved it now, but the redirect always fails. I'm handling my Partner Uploads with this route:
Route::get('partner.profile.logo', [
'uses' => 'PartnerProfileController@getPartnerLogo',
'as' => 'partner.profile.logo',
'middleware' => ['auth:partner'],
]);
Route::post('partner.profile.logo', [
'uses' => 'PartnerProfileController@uploadLogo',
'middleware' => ['auth:partner'],
]);
It uploads the file within my storage folder, but then redirects to the 'partner.profile.logo' route, but within my default guard system, which means it spits an 404.blade view @rin4ik
UploadLogo (Job)
public function handle()
{
$path = storage_path() . '/uploads/' . $this->fileId . '.png';
$filename = $this->fileId . '.png';
Image::make($path)->fit(250, 250, function ($c) {
$c->upsize();
})->save();
if (Storage::disk('s3')->put('/uploads/partners/logos/'. $filename, fopen($path, 'r+'))) {
\File::delete($path);
}
$partner->logo_filename = $filename;
$partner->save();
}
you can't redirect to post route. create get route to redirect. or redirect to existing route
Sorry, meant the get Route, the redirect still fails @rin4ik
How can I see if the Job works? I run php artisan queue:work but nothing will be corresponded to the queues table..
Also restarted php artisan queue:restart
you want your images stored in db? did you create jobs table?
Yes jobs table was created. No images should be stored locally first, then the Job will be dispatched to store it long-term within s3 and the the local file will be deleted. You showed me how to manage it @rin4ik
@splendidkeen do you have ShouldQueue interface in you job class?
Yes @rin4ik
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class UploadLogo implements ShouldQueue
Default queue connection name is
'default' => env('QUEUE_DRIVER', 'sync'),
Should it be database?
of course set it in .env
QUEUE_DRIVER=database
it will never save job to db if you don't set queue driver to database.!
Now the redirect also works @rin4ik
great @splendidkeen
Somehow it won't save the file to s3 and delete the local (storage) file.
$path = storage_path() . '/uploads/' . $this->fileId . '.png';
$filename = $this->fileId . '.png';
Image::make($path)->fit(250, 250, function ($c) {
$c->upsize();
})->save();
if (Storage::disk('s3')->put('/uploads/partners/logos/'. $filename, fopen($path, 'r+'))) {
\File::delete($path);
}
Is there a way to see where the file gets lost?
if (Storage::disk('s3')->put('/uploads/partners/logos/'. $filename, fopen($path, 'r+'))) {
\File::delete($path);
}
this line means if this file uploaded to s3 then delete the file. it should be there /uploads/partners/logos/just refresh s3 bucket. and also don't forget run worker
php artisan queue:work
Ah now I see the workflow with Jobs. @rin4ik
After running queue:work it processed the Job, but failed.
Exception
ModelNotFoundException: No query results for model App\Models\Partner
I can't say anything here without your code . something wrong here with your partner model.
Do I need to use the Job within Partner.php? @rin4ik
nope. show me how you dispatch the job in your controller
public function uploadLogo(Request $request, Partner $partner){
$this->validate($request, [
'logo' => 'required|mimes:jpg,jpeg,png,bmp|max:800',
'logo.required' => 'Please upload an image',
'logo.mimes' => 'Only jpeg,png and bmp images are allowed',
'logo.max' => 'Sorry! Maximum allowed size for an image is 20MB'
]);
if($request->hasFile('logo')){
if ($request->file('logo')->isValid()) {
$request->file('logo')->move(
storage_path() . '/uploads',
$fileId = uniqid(true) . '.png'
);
$this->dispatch(new UploadLogo($partner, $fileId));
}
}
return redirect()
->route('partner.profile.logo')
->with('info', 'Your logo has been uploaded.');
}
Thank you @rin4ik
and now show UploadLogo job with all data. you are welcome. you now what to do after I solve your problem ;)
use App\Models\Partner;
use Image;
use File;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class UploadLogo implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $fileId;
public $partner;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Partner $partner, $fileId)
{
$this->partner = $partner;
$this->fileId = $fileId;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$path = storage_path() . '/uploads/' . $this->fileId . '.png';
$filename = $this->fileId . '.png';
Image::make($path)->fit(250, 250, function ($c) {
$c->upsize();
})->save();
if (Storage::disk('s3')->put('/uploads/partners/logos/'. $filename, fopen($path, 'r+'))) {
\File::delete($path);
}
$partner->logo_filename = $filename;
$partner->save();
}
}
I think Partner $partner is empty. are you updating partner logo right?
Yes updating logo_filename column within partners_table. @rin4ik
dd($partner) please in your controller. what you get?
Returns the fillable array but empty so only lists the possible columns, should I add $partner = Auth::user(); @rin4ik
show me partner model. maybe you have changed getRoutKeyName in your model? before it worked? show me your route for upload logo as well please
Partner.php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Auth\Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class Partner extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, Notifiable;
protected $guard = 'partner';
protected $table = 'partners';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name', 'last_name', 'email', 'password', 'name', 'email_token', 'logo_filename'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Route
Route::get('partner.profile.logo', [
'uses' => 'PartnerProfileController@getPartnerLogo',
'as' => 'partner.profile.logo',
'middleware' => ['auth:partner'],
]);
Route::post('partner.profile.logo', [
'uses' => 'PartnerProfileController@uploadLogo',
'middleware' => ['auth:partner'],
]);
what url u have when you go to upload logo in browser ? I don't really understand this kinda route setups.
<form enctype="multipart/form-data" action="{{route('partner.profile.logo')}}" method="POST">
u are not passing partner that's your problem. when you go to browser partner page what's the url. localhost:8000/partner/profile/logo or what?
it is /partner/{partnername}
When I try
if($request->hasFile('logo')){
if ($request->file('logo')->isValid()) {
$request->file('logo')->move(
storage_path() . '/uploads',
$fileId = uniqid(true) . '.png',
$partner = Auth::user(),
$partner->logo_filename = $fileId,
$partner->save()
);
$this->dispatch(new UploadLogo($partner, $fileId));
}
}
it will process jobs until 262 tries...
Please or to participate in this conversation.