Passing the model class name in laravel using dynamic array - php

I have an array ($models) which has the names of my modal classes :
<?php
namespace App\Http\Controllers\systemActions;
use Storage;
use App\Accident;
use App\Company;
use App\Driver;
use App\Employee;
use App\Expert;
use App\InputTransaction;
use App\Notification;
use App\Receipt;
use App\Person;
use App\Transaction;
use App\Group;
use App\User;
use App\Permission;
use App\Witness;
use App\Companynote;
use App\RejectedReceipt;
use App\DataEntryNote;
use App\Task;
use App\Setting;
use App\RealEstate;
use App\Oldreportpayment;
use App\Freelancerdataentryaccident;
use App\UserFlow;
use App\Page;
use App\Log;
use App\Damagedetection;
use App\Datarequest;
use App\Eperson;
use App\Edriver;
use App\Eaccident;
use App\Erealestate;
use App\Subcompany;
use App\Subcompanyaccident;
use Faker\Provider\tr_TR\DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Hash;
use Validator;
ob_start();
session_start();
class readController extends Controller
{
$modals=['Accident','Company','Companynote','Damagedetection','Dataentrynote','Datarequest',
'Driver','Eaccident','Edriver','Eperson','Erealestate','Freelancerdataentryaccident','Garage'
,'Group','Kashef','Notification','Oldreportpayment','Person','Realestate','Receipt','Setting',
'Rejectedreceipt','Subcompany','Subcompanyaccident','Task','Transaction','Userflow','Witness'];
i was trying to use them in the following code statement :
$modals[$x]::create($DB2_Data[$x]);
but iam getting this error :
Class 'Accident' not found
on the other hand, if I put this or anything that is in the model's array, it works fine
Accident::create($DB2_Data[$x]);
so can anyone give me a solution for this problem.

so as our friend suggested up there we need to add them in the array as following : $modals=['App\Accident']
thank you

Related

When I had created verify phone number, I also get this like

I got the message : Class 'App\Http\Controllers\Auth\RouteServiceProvider' not found
Class in Verify controller
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use Nexmo;
use App\Providers;
use User;
Calss in Longin Controller
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;
use App\Http\Controllers\Redirect;
use App\Http\Requests;
use Nexmo;
use App\Providers;
How do you solve class problems?

Class 'App\Http\Controllers\Auth\User' not found [duplicate]

This question already has an answer here:
Class 'App\Http\Controllers\admin\Auth' not found in Laravel 5
(1 answer)
Closed 10 months ago.
I'm trying to implement Socialiate for Laravel 5.5 using this guide https://scotch.io/tutorials/laravel-social-authentication-with-socialite. Data is returned properly from the provider, but I am having trouble defining Use and Namespaces.
With this configuration:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Socialite;
The result is:
Class 'App\Http\Controllers\Auth\User' not found
Triggered by:
$authUser = User::where('providerId', $user->id)->first();
But, if I add App\User:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Socialite;
The result is:
Class 'App\Http\Controllers\Auth\Auth' not found
Triggered by:
Auth::login($authUser, true);
Any help is much appreciated.
You didnt import Auth namespace the right way.
The proper namespace is Illuminate\Support\Facades\Auth;
Add use Illuminate\Support\Facades\Auth; at the top of your class.
Try Also, adding use App\User;
namespace App\Http\Controllers\Auth;
use Socialite;
use App\User;
in my case help came when i see that included files were only user and not the socialproviders table in the name space i use two tables Users and Social_providers .. it took 2 days to resolve eaten 4 gb data.
enter code here
namespace App\Http\Controllers\Auth;
use Socialite;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Models\socialProvider;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
//use Laravel\Socialite\Facades\Socialite;
class RegisterController extends Controller

Error In Laravel ClassLoader.php File

Error:
Parse error: syntax error, unexpected '$type' (T_VARIABLE), expecting ',' or ')'","exception":"Symfony\Component\Debug\Exception\FatalThrowableError","trace":[{"file":"/home/fiedel/Documents/fol/docs/develop/vendor/composer/ClassLoader.php","line":322,
These are the things included:
use App\Models\Category;
use App\Models\RegistrySession;
use Auth;
use DB;
use App\Models\Registry;
use App\Models\Project;
use App\Models\Enterprise;
use App\Models\User;
use App\Models\Group;
use League\Period\Period;
use Marcelgwerder\ApiHandler\Facades\ApiHandler;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Carbon\Carbon;
use PHPExcel;
use PHPExcel_IOFactory;
use Barryvdh\DomPDF\Facade as PDF;
use App\Models\DailyLog\CaptionDailyUser;
use App\Models\DailyLog\CaptionDailyUserProject;
use App\Models\DailyLog\UserActivityDaily;

Laravel mail sending returns error

I'm trying to use the laravel 5.3 mailable for email verification, but it's not working and returns me this error -
FatalThrowableError in RegisterController.php line 84:
Class 'App\Http\Controllers\Auth\Mail' not found
The top of the RegisterController.php looks like -
namespace App\Http\Controllers\Auth;
use App\Mail\VerifyEmail;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
VerifyEmail is my mailable class generated using make:mail.
Line 82-84 of RegisterController.php -
$email = new VerifyEmail($verification_code);
Mail::to($data['email'])
->send($email);
The full stack trace is pasted here http://pastebin.com/r9sQpPdy.
Your problem is that you're trying to use the Mail facade in class that's in another namespace, without importing (use) it.
You either do:
\Mail::to($data['email'])
->send($email);
OR you should use it:
use App\Mail\VerifyEmail;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Mail;
class RegisterController extends Controller

How to use methods from one controller in another

I have installed waavi package for manipulation of translation files. I need to use methods from it's controller to mine? I tried something like this but it doesn't work
LanguageRepository::findByLocale(1);
This is what I am using in beginning of my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
use Waavi\Translation\Repositories\TranslationRepository;
use Illuminate\Foundation\Application;
If you have successfully done all the steps in here, you should be able to access to LanguageRepository using depedency injection(" It is recommended that you instantiate this class through Dependency Injection")
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
class DefaultController extends Controller
{
private $language_repository;
function __construct(LanguageRepository $language_repository)
{
$this->language_repository = $language_repository;
}
public function index()
{
dd($this->language_repository->findByLocale("en"));
}
}
Note: you need pass language string instead of id to findByLocale method. see line 97

Categories