Laravel ReflectionException - php

I have a class called CustomerController with a delete function:
class CustomerController extends Controller
{
public function getAllCustomer()
{
return \App\model\Customer::get();
}
public function destroy (Customer $id)
{
$id->delete();
}
This is the route:
Route::delete('customer/{id}' , 'CustomerController#destroy');
I get this error:
Class App\Http\Controllers\Customer does not exist
I already tried Composer update and Composer dump-autoload with no success.
A screenshot:
Thank you very much!

When you do not include classes using use statements, php will try to find the class in the current namespace.
So, the function function destroy (Customer $id) will look for the class Customer in the App\Http\Controllers namespace. To avoid this, add a use statement for the App\model\Customer class above on top of your controller class. For example:
<?php
namespace App\Http\Controllers;
use App\model\Customer;
class CustomerController extends Controller
{
public function getAllCustomer()
{
return Customer::get();
}
public function destroy (Customer $id)
{
$id->delete();
}
}
Now you can also use a shorter name in the getAllCustomer() function.

Change this :
return \App\model\Customer::get();
to this:
add namespace at the top:
use App\Customer;
return Customer::get();
as you are using route model binding on delete action by injecting Customer class to the method therfore its failing so make sure you add model namespace at the top of the file(CustomerController).
The error is happening beacuse CustomerController is trying to look for Customer model in controllers namespace which means that your model namespace is wrong.

Related

Class App\Http\Controllers\EditareProfilController does not exist

I am very new in Laravel and this is my first project. I have this problem and I don`t know how to fix it.
This is my directory
I am sure I have tried all I know
This is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class EditareProfilController extends Controller
{
public function __construct()
{
$tasdhis->middlewasdare('auasdth');
}
public function index()
{
return vasdasdiew('editaasdasdasdeprofil');
}
}
and this is my route:
Route::get('/editarasdasdeprofil', 'EditareProfilasdasdasdontroller#index')->name('editarepasdasdasdrofil');
The default location for controllers is app\Http\Controllers if you add a folder just add it to your route in order to laravel localize it. In your code you are storing your controller under the Auth folder.
Update your route to:
Route::get('/editareprofil', 'Auth\EditareProfilController#index')->name('editareprofil');
And also don't forget to update the namespace.
namespace App\Http\Controlllers\FOLDER;

Laravel: Model Class not found in relationships

Using Relationships first time and having issue to understand it.
I have a Student Class and a Course Model Class. A student can take many courses. In Student Model I do this:
public function course()
{
return $this->belongsTo('Course');
}
and in controller when I do this: .$student->course()->course_title it gives an error:
FatalThrowableError in Model.php line 779:
Class 'Course' not found
What am I doing wrong?
replace your code with it
public function course()
{
return $this->belongsTo(Course::class);
}
Make sure your namespace in your Course class matches 'App\Models'. You might be trying to access an "empty" namespace.
Namespaces are declared at the top of your class file. So for example:
Course.php:
namespace App\Models;
use Illuminate\Http\Request;
...
class Course extends Model { ... }
OtherClass.php
namespace App\Models;
use Illuminate\Http\Request;
...
class OtherClass extends Model {
...
public function course()
{
return $this->belongsTo('App\Models\Course')
}
}
Please note that the namespace declared at the top of Course.php matches the path provided to the relationship method (return) belongsTo(). You can also provide Course::class to the belongsTo method, but you need to import aka use the class in your php class.
I hope this helps!
I had the same problem and i solved it by adding a name space in the top of my model
for your case,
use \App\Course ; // Your name space of your class in relation
Hope this help others
NB : I used Laravel 5.0

Laravel can't find class in namespace

I using Laravel, I have a Model class under App/Models
<?php
namespace App\Models;
class TodoList extends \Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
In my Controller I have included the namespace as follows:
namespace App\Http\Controllers;
...
use App\Models\TodoItem;
use App\Models\TodoList;
class TodoListController extends Controller
My method looks like this:
public function show($id)
{
$list=TodoList::findOrFail($id);
return \View::make('todos.show')->with('list', $todo_list);
}
but when I call to a request i get the error:
FatalErrorException in TodoListController.php line 75: Class
'App\Models\TodoList' not found
Trying running composer dump-autoload. Basically your classes become cached so you need to tell Laravel to look for newly added classes.
I'm not sure, just try this :
namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class TodoList extends Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
Here is the code from the docs, but for your example. Note the use Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TodoList extends Model {
//insert public function listItems() here
}
Hope this is helpful!
Make sure the file is in the correct folder and has the same name caption as the Class you want to import.
If the caption of the file "TodoList" is not TodoList.php but Todolist.php for example, Laravel wont find it.
Depending on your setup running "composer dump" might help to refresh autoload files.

How to share Auth::user among all views in Laravel 5?

I need to share the currently logged in user to all views. I am attempting to use the view->share() method within AppServiceProvider.php file.
I have the following:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Auth\Guard;
class AppServiceProvider extends ServiceProvider {
public function boot(Guard $guard)
{
view()->share('guard', $guard);
view()->share('user', $guard->user());
}
//..
}
However, when I hit up a view (after logging in), the user variable is null. Strangely, the $guard->user (the protected attribute, not the public method user()) is not. The output I get is the following:
Note the guard->user variable is populated, but the user variable is null.
Better off using View Composers for this one.
In your ComposerServiceProvider in boot():
view()->composer('*', 'App\Http\ViewComposers\GlobalComposer');
In HTTP/ViewComposers create a class GlobalComposer, and create the function:
public function compose( View $view )
{
$view->with('authUser', Auth::user());
}
http://laravel.com/docs/5.0/views#view-composers
You can solve this problem without creating a file.
Add these codes to boot() action of your ServiceProvider and that's it.
view()->composer('*', function($view){
$view->with('user', Auth::user());
});
Source also same: http://laravel.com/docs/5.0/views#view-composers
Look Wildcard View Composers.

How do I call a model in Laravel 5?

I'm trying to get the hang of Laravel 5 and have a question which is probably very simple to solve but I'm struggling.
I have a controller named TestController and it resides in \app\Http\Controllers
This is the controller:
<?php
namespace App\Http\Controllers;
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Then I have a model I created that resides in /app:
<?php
namespace App;
class diamonds extends Model {
}
Put all other mistakes aside which I'm sure there are, my problem is that laravel throws an error:
FatalErrorException in TestController.php line 10: Class
'App\Http\Controllers\diamonds' not found
So, how do I get the controller to understand I'm pointing to a model and not to a controller?
Thanks in advance...
You have to import your model in the Controller by using namespaces.
E.g.
use App\Customer;
class DashboardController extends Controller {
public function index() {
$customers = Customer::all();
return view('my/customer/template')->with('customers', $customers);
}
}
In your case, you could use the model directly App\diamonds::find(1); or import it first with use App\diamonds; and use it like you already did.
Further, it is recommended to use UpperCamelCase class names. So Diamonds instead of diamonds. You also could use dd() (dump and die) instead of var_dump to see a nicely formatted dump of your variable.
//Your model file
<?php
namespace App\Models;
class diamonds extends Model {
}
// And in your controller
<?php
namespace App\Http\Controllers;
use App\Models\
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Try adding the following lines above your class decleration in your controller file...
use App\Diamonds;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
(this assumes your model file is called Diamonds.php)
Ultimately, there were a few issues here and all of you helped, however I was missing the following on the model page:
use Illuminate\Database\Eloquent\Model;

Categories