Class does not exist laravel 5.3 - php

namespace MyApp\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateAdminGroup extends FormRequest
{
public function authorize()
{
return false;
}
public function rules()
{
return [
//
];
}
}
My Controller:
namespace MyApp\Http\Controllers\Administration\Resource;
use MyApp\Http\Requests\Admin\Admin\CreateAdminGroup;
But i get an error that Class SwapSome\Http\Requests\Admin\Admin\CreateAdminGroup does not exist
Why do I get this error, the request is there, everything shuold be good.
My request in the App\Http\Requests\Admin\Admin\ folder
Thank you!

Change request's namespace to:
namespace MyApp\Http\Requests\Admin\Admin;

Related

How can i fix " Class 'App\Service' not found "

in local host i haven't this problem but in linux host in CPannel i receive this error
Class 'App\Service' not found
it's just an example i have same problem in some models..
my relations doesnt work properly in original host but in local host i haven't got any problem
my models:
<?php
namespace App;
use App\Category;
use App\Project;
use App\Service;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = [
'title', 'parent_id','title_en',
];
public function category(){
return $this->hasMany(Category::class,'parent_id');
}
public function parent(){
return $this->category('parent');
}
public function project(){
return $this->belongsToMany(project::class);
}
public function service(){
return $this->belongsToMany(service::class);
}
}
my controller :
namespace App\Http\Controllers;
use App\Service;
use Illuminate\Http\Request;
class ServiceController extends Controller
{
public function landpage(){
$services=Service::with('cat')->get();
return view('services.index',compact('services'));
}
public function detail($id){
$services=service::with('cat')->findOrFail($id);
return view('service_detail.index',compact('services'));
}
}
ok as i found .. the models are sensitive to capital words..
i changed all of the models to capital words..
App/service => App/Service
and it worked

Laravel 5.7: Class 'App\Http\Controllers\MailableClass' not found

I created a Mailable called Class UserRequest
I'm trying to call it from inside a controller buy this is the error I get:
Class 'App\Http\Controllers\UserRequest' not found
I also tried ->send(new \UserRequest($msgdata)); but it still doesn't work.
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request)
{
$msgdata = array('subject'=>$request->subject,'email'=>$request->email, 'name'=>$request->name,'body'=>$request->body);
try
{
Mail::to('dddddddd#dddsdsf.com')
->send(new UserRequest($msgdata));
}
catch(Exception $e)
{
}
}
}
Include your class at the top like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\UserRequest; // including your class
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request){
$msgdata = array('subject'=>$request->subject,'email'=>$request->email,
'name'=>$request->name,'body'=>$request->body);
try {
Mail::to('dddddddd#dddsdsf.com')->send(new UserRequest($msgdata));
}catch(Exception $e){
// Log your exception
}
}
}
add 'App\Http\Controllers\UserRequest' to the head
use App\Http\Controllers\UserRequest;
at the top.
You will need to add the right path to the top as stated by other.
Also check the namespace in the UserRequest class

Class App\Http\Controllers\Panel does not exist

I'm following the Laracasts series and have run into an issue on the episode Laravel 5.4 From Scratch: Route Model Binding.
Laravel version:
Laravel Framework 5.6.13
The error:
Class App\Http\Controllers\Panel does not exist
This shows on both the /panel and /panel/1 pages
App\Http\Controllers\PanelController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
routes/web.php
// Main panel view
Route::get('/panel', 'PanelController#index');
// Individual panel view
Route::get('/panel/{panel}', 'PanelController#show');
App/Panel.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Panel extends Model
{
public static function activePanels()
{
return static::where('status', 1)->get();
}
}
Add this line in panel controller before the class
use App\Panel;
You need to add use App\Panel; to top of class
Or call it by full namespace $panels = App\Panel::all();
You don't included your model to class.
Add App\Panel to main include section:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panel;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
or load model in your class method manually:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PanelController extends Controller
{
public function index()
{
$panels = App\Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}

How to call a model function inside the controller in Laravel 5

I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function

Make a global validation for all requests in Laravel

I have to make validation of all of Date format only using class/request in Laravel. Can I make validation for all of the requests ? I think i do it in request.php abstract class.
You may try something like this, at first create a BaseController like the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BaseController extends Controller {
public function __construct(Request $request) {
$this->request = $request;
$this->hasValidDate();
}
protected function hasValidDate()
{
if($this->request->method() == 'POST') {
// Adjust the rules as needed
$this->validate($this->request, ['date' => 'required|date']);
}
}
}
Then in your other controllers, extend the BaseController like this example:
namespace App\Http\Controllers\User;
use App\Http\Controllers\BaseController;
class UserController extends BaseController {
public function index()
{
// ...
}
}
Hope you got the idea. Use it wisely.

Categories