ReflectionException in laravel. Class does not exist - laravel

Why does laravel cannot detect my class this time. In my previous tries it worked but this time it does not. Here are my codes :
DataEntry.php
<?php namespace App\Server ;
abstract class DataEntry{
/*This class facilitates the
storing of data to the server...
*/
abstract protected function storeData($data);
abstract protected function updateData($data ,$id);
}
MedicineDosageEntry.php
<?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) {
}
}
MedicineController.php
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\DB\MedicineRepository;
use App\Http\Requests\MedicineRequest ;
use App\Dosage ;
use App\Medicine;
use Auth ;
use App\Server\MedicineDosageEntry ;
use Flash ;
class MedicineController extends Controller {
public function store(MedicineRequest $request, MedicineDosageEntry $dosage ){
$requestData = $request->all();
$dosage->storeData($requestData);
}
}
Do i have to register this class. Why do I encounter this. It worked out in my previous tries but after doing some refactoring and coding. It crashed..

Related

Laravel Class 'Iyzipay\Options' not found

<?php
namespace App\Http\Controllers\Web;
use Iyzipay\Options;
class IndexController extends Controller
{
.
.
.
public function iyzico(Request $request){
$name = $request->get('name');
$card_no = $request->get('card_no');
$expire_month = $request->get('expire_month');
$expire_year = $request->get('expire_year');
$cvc = $request->get('cvc');
$user = Auth::user();
//options
$options = new Options();
$options->setApiKey("***");
$options->setSecretKey("***");
$options->setBaseUrl("***");
}
.
.
.
}
I'm doing something like this on controller.But I get the error Class 'Iyzipay\Options' not found. I checked the file path and it is correct. No matter what I did I couldn't fix the error
If you want to use iyzipay package, you should add to your composer:
composer require iyzico/iyzipay-php
Edit For Manual Usage:
If you want to use manual you can create Library folder under app directory. And paste iyzipay folder here.
Then create a file such as Iyzipay.php in Library folder.
app
-Library
--iyzipay-php
--Iyzipay.php
And Iyzipay.php (i recommend, don't use transaction process in your controller
<?php
namespace App\Library;
require_once dirname ( __FILE__ ) . '/iyzipay-php/IyzipayBootstrap.php';
class Iyzipay
{
public static function boot ()
{
\IyzipayBootstrap::init ();
}
public static function pay ( $apiInfo, $cartInfo, $price, $shippingTaxPrice = 0 )
{
....
....
}
}
and use in your controller like this:
<?php
...
...
use App\Library\Iyzipay;
...
...
Iyzipay::boot ();
$payment = Iyzipay::pay ( $apiResources, $cartInfo, $price) );

How to add custom methods to Laravel Carbon

I've been searching this problem for 2 days but no luck. I want to add custom methods to laravel carbon. I use the syntax below:
namespace App\Http\Controllers\Helpers;
use Carbon\CarbonInterface;
interface CarbonJalaliInterface extends CarbonInterface {
public function toJalaliDateTimeString($unitPrecision = 'second');
public function toJalaliDateString($unitPrecision = 'second');
}
abstract class CarbonJalali implements CarbonJalaliInterface {
public function toJalaliDateTimeString($unitPrecision = 'second') {
list($part1, $part2) = explode(' ', $this->rawFormat('Y-m-d '.static::getTimeFormatByPrecision($unitPrecision)));
list($year, $month, $day) = explode('-', $part1);
list($hour, $minute, $second) = explode(':', $part2);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
include_once ('jdf.php');
$jalali_date = jdate("H:i:s - Y/m/d",$timestamp);
return $jalali_date;
}
public function toJalaliDateString($unitPrecision = 'second') {
$timezone = 0;
list($part1, $part2) = explode(' ', $this->rawFormat('Y-m-d'));
list($year, $month, $day) = explode('-', $part1);
$timestamp = mktime($month, $day, $year);
include_once ('jdf.php');
$jalali_date = jdate("Y/m/d",$timestamp);
return $jalali_date;
}
}
But i get the error:
Symfony\Component\Debug\Exception\FatalErrorException DateTimeInterface can't be implemented by user classes
I want to use it like this :
CarbonJalali::now()->toJalaliDateTimeString();
Any suggestions? Thanks.

Laravel-api for multiple file upload

I want to make laravel api for multiple file upload when i am uploading then its gives error $data is undefined variable.please help me how to remove this error..?
FileUploadController.php
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Detail;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
class FileUploadController extends Controller
{
public function uploadFile(Request $request){
$this->validate($request, [
'user_sharing_image' => 'required',
'user_sharing_image.*' => 'mimes:doc,pdf,docx,zip'
]);
if($request->hasfile('user_sharing_image'))
{
foreach($request->file('user_sharing_image') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/files/', $name);
$data[] = $name;
}
}
$file= new Detail();
$file->title = $request->title;
$file->info = $request->info;
$file->user_id = $request->user()->id;
$file->user_sharing_image=json_encode($data);
$file->save();
return back()->with('success', 'Data Your files has been successfully added');
}
}
I am using laravel passport for auth and want to store user_id but do not geting please help me how to resolve both problem from this code
Give it a try
$data = [];
if($request->hasfile('user_sharing_image'))
{
foreach($request->file('user_sharing_image') as $key=>$file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/files/', $name);
$data[$key] = $name;
}
}
$file= new Detail();
$file->title = $request->title;
$file->info = $request->info;
$file->user_id = Auth::user()->id;
$file->user_sharing_image=json_encode($data);
$file->save();
You are getting this error because $data is not defined.
Before foreach loop you can declare it as $data = array();
i think you should use files method to return array of files :
foreach($request->files('user_sharing_image') as $file)
Hello fellow developers
This will work properly
$files = $request->allFiles('imgs');
foreach ($files as $key => $img) {
# code...
$filename = request()->getSchemeAndHttpHost() . '/assets/images/users/upload/profile/photos/' . time() . '.'. $img->extension();
$img->move(public_path('/assets/images/users/upload/profile/photos/'), $filename);
$photos = UserPhoto::create([
'user_id' => $user->id,
'name' => $filename
]);
}
return response()->json([
'status' => true,
'data' => 'Photos Uploaded Successfully!!'
]);

Save dynamic data to two tables using relationship in Laravel

I have two tables Students and Hobbies. I've all details in one form but I want to save details to separate tables on submit. How can I achieve this?
My Student model
<?php
namespace App;
class Students extends Model {
protected $fillable = ['name', 'address'];
public function hobbies() {
return $this->hasMany(Hobby::class);
}
}
Hobby model:
<?php
namespace App;
use App\Product;
class Hobby extends Model
{
protected $fillable = [
'entertainment', 'sports'
];
public function hobby()
{
return $this->belongsTo(Student::class);
}
}
Controller:
public function save(Request $request, Student $student)
{
$students= new Student;
$students->name= request('name');
$students->address= request('address');
$students->save();
if($students->save())
{
$hobbies= [];
$images = $request->file('hob_img');
$hob_desc = $request->hob_desc;
foreach ($request->hob_name as $key => $hobby) {
$hob_img = '';
{
$hob_img = uniqid() . '.' . $files[$key]->getClientOriginalExtension();
$files[$key]->move(public_path('/assets/images/'), $hob_img);
}
$hobbies[] = [
'hob_name' => $hobby,
'hob_desc' => $hob_desc[$key],
'hob_img' => $hob_img
];
}
$hobbies[] = new Hobby;
$hobbies->hob_name = request('hob_name')[$key];
$hobbies->hob_desc= request('hob_desc')[$key];
$hobbies->hob_img = request($hob_img)[$key];
$hobbies->save();
}
}
But I cannot save it. It says SQLSTATE[HY000]: General error: 1364 Field 'student_id' doesn't have a default value
Try the insert to save many records at onece :
public function save(Request $request, Student $student)
{
$student= new Student;
$student->name= request('name');
$student->address= request('address');
if($student->save())
{
$hobbies= [];
$images = $request->file('hob_img');
$hob_desc = $request->hob_desc;
foreach ($request->hob_name as $key => $hobby) {
$hob_img = '';
$hob_img = uniqid() . '.' . $files[$key]->getClientOriginalExtension();
// $files i think it should be $images
$files[$key]->move(public_path('/assets/images/'), $hob_img);
$hobbies[] = [
'hob_name' => $hobby,
'hob_desc' => $hob_desc[$key],
'hob_img' => $hob_img,
'student_id'=> $student->id
];
}
Hobby::insert($hobbies); // useing Eloquent
// Or you can use Query Builder
// DB::table('hobbies')->insert($hobbies);
}
}

extends multiple system class of codeigniter

I want to handle error on codeigniter so I need to extends both the class "CI_Controller" and "CI_Exceptions" so how to implement it. something like below. if it's possible to handling error globally in hooks or other way for whole system?
class Language extends CI_Controller , CI_Exceptions{
public function __construct()
{
parent::__construct();
parent::CI_Exceptions();
}
}
There is a nice post on how to do this using a hook. I copied the samples from the post, but I would go there and read up on the solution to get going int the right direction.
Hook
$hook['pre_controller'][] = array(
'class' => 'ExceptionHook',
'function' => 'SetExceptionHandler',
'filename' => 'ExceptionHook.php',
'filepath' => 'hooks'
);
Exception Class
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::CI_Exceptions();
}
public function show_php_error($severity, $message, $filepath, $line)
{
$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
$filepath = str_replace("\\", "/", $filepath);
// For safety reasons we do not show the full file path
if (FALSE !== strpos($filepath, '/'))
{
$x = explode('/', $filepath);
$filepath = $x[count($x)-2].'/'.end($x);
}
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/error_php'.EXT);
$buffer = ob_get_contents();
ob_end_clean();
$msg = 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line;
log_message('error', $msg , TRUE);
mail('dev-mail#example.com', 'An Error Occurred', $msg, 'From: test#example.com');
}
}

Resources