Back

Stuck with registering service provider. What to do next ?


finger posted 4 days ago

This is my base abstract class.

<?php namespace App\Server ;
abstract class DataEntry{
	/*This class facilitates the
	 storing of data to the  server...
	 */
	abstract public function storeData($data);
	abstract public function updateData($data ,$id);
}

I have a class to inject which is

<?php 
namespace App\Server ; 
use Image; 
use App\Dosage ; 
use App\Photo ;
use App\Server\DataEntry ;  
use Request ; 
use Auth ;

class MedicineDosageEntry extends DataEntry{
	public function storeData ($data){
		$file = $data['photo'] ; 
		$fileName = uniqid().$file->getClientOriginalName() ; 
		if(!file_exists('medicine/images')){
			mkdir('medicine/images', 0777, true);
		}
		$file->move('medicine/images', $fileName);

		if(!file_exists('medicine/images/thumbs')){
			mkdir('medicine/images/thumbs', 0777, true);
		}

		$thumb = Image::make('medicine/images/' .$fileName)->resize(150,150)->save('medicine/images/thumbs/' . $fileName,50);
		$dosage = new Dosage;
		$dosage->dosage_name = $data['dosage_name'];
		$dosage->form = $data['form'];
		$dosage->medicine_id = $data['medicine_id'];
		$dosage->price = $data['price'];
		$dosage->save(); 

		$dosage->photo()->create([
			'dosage_id' => $data['id'];
			'file_name' => $fileName,
			'file_size' => $file->getClientSize(),
			'file_mime' => $file->getClientMimeType(),
			'file_path' => 'medicine/images/thumbs'. $fileName,
			'created_by'=> Auth::user()->id,
			]);
	}

	
	public function updateData($data , $id) {

	}
}

and so i created a provider with this :

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class MedicineDosageProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
       $this->app->bind('\App\Server\MedicineDosageEntry', function ($app) {
        return new \App\Server\MedicineDosageEntry();
    });
 }
}

I'm stuck on what to do next ? ?? Can you help me with this ?

astroanu replied 3 days ago

now you should be able to inject your class in to the controller like so

public function __construct(\App\Server\MedicineDosageEntry $entry){
    
    $this->entry = $entry;

}

public function storeData(Request $request) {

    $data = $request::get('data');

    $this->entry->storeData($data);
}

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us