this is my parent class which is a user class that has the main crud operations
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\UserRepository; //<----------- Here
class UserController extends Controller
{
protected $model;
public function index()
{
$users = $this->model::all();
return view('users.index', compact('users'));
}
}
this is my child class which is one of my user roles , it have the same crud operation but it need some more functinality
<?php
namespace App\Http\Controllers;
use App\Models\Teacher;
use App\Http\Controllers\UserController;
class TeacherController extends UserController
{
public function __construct()
{
$this->model = Teacher::class;
}
}
when I try to access the route i get this error : Class name must be a valid object or a string
at :
$users = $this->model::all();
Well, it seems my Laravel project used old cached routes. Just run
php artisan route:clear
from time to time before debugging anything.
I am new to laravel just displaying all the data of a single table using model named 'Product.php' inside my 'app' folder. Unfortunatly i'm getting above error while doing so. Following is the controller code:
<?php
namespace App\Http\Controllers;
use App;
use App\Product;
use Illuminate\Http\Request;
use App\Http\Requests;
class productController extends Controller
{
function show(){
return Product::all();
}
}
and model code is following:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class products extends Model
{
protected $table='product';
}
Two mistakes:
1) remove letter "s".
2) Add first Upper case
...
class Product extends Model
{
protected $table='product';
}
To create a Model always use
php artisan make:model your_model_name
And your mistake is in your spelling
class products extends Model
It should be
class Product extends Model
I am new to Laravel, I am trying things around while going through a tutorial. This is where I am facing an unexpected behaviour.
I have a model tweet and a controller named tweetsController; when I call tweet::find() or any similar method I found this:
FatalErrorException in tweetsController.php line 13:
Class 'App\Http\Controllers\tweet' not found
I have also tried App\tweet::find().
Everything seems fine through tinker.
Please explain.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class tweetsController extends Controller
{
public function show(){
$data = tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
public function delete($id){
return "here we dele the tweet ".$id;
}
public function add(){
return "i add your tweet to database then show you all the tweets";
}
}
tweet.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tweet extends Model
{
protected $fillable = array(
'tweetHead',
'tweetBody'
);
}
?>
A few options the maybe generating this error:
The model/controller namespace is incorrect;
The name of the file and the class name for the model needs to be "Tweet" with the first letter in uppercase;
If you set the right namespace on the model "Tweet.php" and import that on your "TweetController.php"
I hope that helps :)
UPDATE:
In the TweetController.php add this
use App\Tweet;
Before the class declaration, like this
use App\Tweet;
class tweetsController extends Controller
{
And remember to change the controller name in the class declaration like this
class TweetsController extends Controller
{
And the controller filename will become "TweetsController.php"
The Model also has to be named "Tweet" and not "tweet" in the class declaration and the filename
class tweet extends Model
will become
class Tweet extends Model
and the file will be named "Tweet.php"
and everytime you need to call the model you will do this
public function show(){
$data = App\Tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
Add use Add\User below namespace in your controller
I am new to laravel. I am trying to organise my controller by putting it inside a folder, but it doesn't seem to work.
My folder structure is like this:
/app
/Http
/Controllers
/Admin
ShowDashboard.php
My ShowDashboard.php file is like this:
<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class ShowDashboard extends Controller {
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
public function init()
{
return 'Hi there!';
}
}
My route is like this
Route::get('/admin', 'Admin\ShowDashboard#init');
When I tred to access http://localhost:8000/admin I get the following error:
Class App\Http\Controllers\Admin\ShowDashboard does not exist
My autolaoder section:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
}
Am I missing something?
The best way to create a controller is to use the built in Laravel utility, Artisan. From a command prompt, browse to the directory your laravel project is located. For example: c:\development\htdocs\www.example.dev
At the prompt, type: php artisan make:controller admin/showDashboard --plain
This will generate a file named showDashboard.php within an admin directory under your controllers. The file will have the following code by default:
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class showDashboard extends Controller
{
//
}
Now that you have created your controller, add a method for init:
public function init()
{
return 'Hi there!';
}
Your controller will now look like this:
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class showDashboard extends Controller
{
public function init()
{
return 'Hi there!';
}
}
Now, setup your route in your routes.php as follows:
route::get('admin', 'admin\showDashboard#init');
Save your work, and launch your page. When browsing to www.example.dev/admin you should see the message: Hi there!
I hope this helps!
I don't know why this was happening, but adding this in my route fixed it.
Route::group(['namespace' => 'Admin'], function()
{
// Controllers Within The "App\Http\Controllers\Admin" Namespace
Route::get('/admin','ShowAdminDashboard#index');
});
Everything is already explained but one more try can be done by
adding controller suffix to showDashboard and run composer dump-autoload.
I think then your controller will run.
Rename your controller ShowDashboardController
php artisan make:controller Admin/ShowDashboardController
File name should be ShowDashboardController.php
I don't see anything wrong with what you posted. If you changed the namespace-to-folder mappings in composer.json, make sure you ran the 'composer dump-autoload' command.
The following code is working.. Try once
created a file ShowDashboard.php in folder admin like app/http/controller
now ,
ShowDashboard.php
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
class ShowDashboard extends Controller {
public function init()
{
return 'Hi there!';
}
}
Added Route::get('admin', 'admin\ShowDashboard#init'); in routes.php
and then run composer update on cmd..
Then run http://localhost:8000/admin .
it says.. Hi there!
Create a new controller in subfolder, for example: app/Http/Controllers/User/UserController.php
In this controller, at the end of namespace must include folder name
Like this: namespace App\Http\Controllers\User;
The important thing is under namespace must write use App\Http\Controllers\Controller;
finally in routes.php Route::get ( '/user', 'User\UserController#login' );
UserController.php contents:
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
class UserController extends Controller {
public function login() {
return 'this login';
}
}
routes.php contents:
Route::get ( '/user/login', 'User\UserController#login' );
// or use this
Route::group ( [
'namespace' => 'User'
], function () {
Route::get ( '/user/login', 'UserController#login' );
} );
So, in L5 I created folder like app/Models/Blog where is file Posts.php which looks like:
<?php namespace App\Models\Blog;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model {
protected $table = 'posts';
}
After it I executed composer dump and then in my controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
It throws me an error:
FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found
Try changing this:
use Models\Blog\Posts as Posts;
To this:
use App\Models\Blog\Posts;
In Laravel 5.2 it's just:
use App\Blog;
or
use App\Blog\Posts;
Change the following
class Posts extends Model {
to
class Posts extends \Eloquent {
You need to check two points :
the namespace have to be in first
the using must be use App\Models\Blog in your case
Like this :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Blog;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
(tested with Laravel 5.4)