I want to add data to the database after a successful validation,but i get this error.'
FatalThrowableError in AboutController.php line 51:
Class 'App\About' not found.
My Controller
<?php
namespace App\Http\Controllers;
use App\About;
use Illuminate\Http\Request;
use App\Http\Requests;
class AboutController extends Controller
{
public function store(Request $request)
{
//
$about = $request->about;
$validation = \Validator::make($about, About::$rules);
if($validation->passes())
{
About::create($about);
return route('about/admin')->compact(about);
}
}
my Model
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
//
protected $guarded = array('id');
protected $fillable = array('about');
public static $rules = array('about' => 'required|5');
}
location of controllers and Model:
App\Http\Controllers\AboutController
App\About
I have tried to run
php artisan cache:clear
php artisan clear-compiled
composer dump-autoload
I'm stuck can anyone tell me what is causing this?
As #webNeat said you should change the namespace that you are using in your Model.
Your Model About
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
Controller
<?php
namespace App\Http\Controllers;
use App\About; // You have declared App\Http\Controllers in your Model
Model About Fixed
<?php
namespace App; // change to this namespace
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
If you're a bit lost with Laravel or namespaces I strongly recommend you to use php artisan with each of its commands, and see and study what they do by reading all the code generated. For this case with:
php artisan make:model About
You will get a fresh new About model prepared for receive all your code with the correct namespace.
changing the namespace of your model to App should fix the issue.
<?php
namespace App; // <- here
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
Related
please help. I got this error "Target class [FrontendController] does not exist."
This is my route :
<?php
use Illuminate\Support\Facades\Route;
Route::get('/','FrontendController#home');
My Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class FrontendController extends Controller
{
public function home()
{
return view ('index');
}
}
Why this happend? can anyone help me please? :(
You have to refer to the controller, you have 2 options
Laravel 7 and lower
Route::get('/','App\Http\Controllers\FrontendController#home');
If you are using Laravel 8 then you have to change the structure of the route
Route::get('/', [App\Http\Controllers\FrontendController::class, 'home']);
I created a new Laravel controller named "StoriesController" using the CLI:
php artisan make:controller StoriesController
When I open app/Http/Controllers/StoriesController.php, I see:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StoriesController extends Controller
{
//
}
My question is about the line:
class StoriesController extends Controller
Where is the "Controller" script located? Which directory?
{your-project]/app/http/controllers/{StoriesController}
I am making a PostController and getting data from posts table
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Posts;
class PostController extends Controller
{
public function index()
{
$posts=Posts::latest()->paginate(5);
// print_r($posts);exit;
return view('post.index',compact($posts))
->with('i',(request()->input('page',1)-1)*5);
}
and my view page code is
#foreach($posts as $post)
<?php echo $post->title; ?>
#endforeach
It's giving me Undefined variable Post in views.
My model code is:-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $fillable=['title','description'];
}
I found the issue in your code.
You may have to write like this for rendering data to view.
return view('post.index',compact($posts))
->with('i',(request()->input('page',1)-1)*5);
From above , to below . change the code.
return view('post.index',compact('posts'))
->with('i',(request()->input('page',1)-1)*5);
It will work.
I have created a controller and the coding in my controller file is as shown below
This is the controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use DarthSoup\Whmcs\Facades\Whmcs;
use DarthSoup\Whmcs\WhmcsServiceProvider;
class GetProductController extends Controller
{
public function show(){
$products = Whmcs::GetProducts([
]);
return view('SME_Hosting',['products'=>$products]);
}}
This is my Route
Route::get('/SME_Hosting','GetProductController#show');
I'm getting same error even after clearing the cache by using the below functions:
php artisan config:cache
php artisan config:clear
I am beginner in laravel. And I am using Laravel 5.0.16 in my wamp server. I have been learning laravel by free video tutorial available in laracasts.com. I have been trying to fetch data from database. I have checked that my app is already connected to database.
I do have below structure in app folder:
-app
-Http(folder)
-Other folders (folder)
-Article.php (file)
-User.php (file)
In side Http folder:
-Controllers (folder)
-Middleware (folder)
-Requests (folder)
-Kernel.php (file)
-routes.php (file)
In Controllers folder:
-ArticleController.php (file)
Below is code in side routes, controllers and model file:
/*routes*/
Route::get('articles','ArticleController#index');
/*Controller file*/
use App\models\Article;
namespace App\Http\Controllers;
use App\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller {
public function index()
{
$users = Article::all();
return $users;
}
}
/*Model file - Article.php*/
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = 'users';
protected $fillable = ['id','firstname', 'lastname', 'email','reg_date'];
}
Where users is DB table with fields.
I am getting below arror:
FatalErrorException in ArticleController.php line:
Class 'App\Http\Controllers\Article' not found
I have check other SO forums but they didn't help me, can anyone suggest me what am I missing?
There are two issues here. One the namespace declaration should happen before any use statements.
Second your models uses the model namespace but your models aren't in a model directory. The namespace should match the directory structure. So you either need to change the namespace to use App\Article (also change the namespace in the model file) or move the model files into a models directory.
So to fix this without moving files update the code to look like this
namespace App\Http\Controllers;
use App\Article;
use App\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller {
public function index()
{
$users = Article::all();
return $users;
}
}
/*Model file - Article.php*/
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = 'users';
protected $fillable = ['id','firstname', 'lastname', 'email','reg_date'];
}
Add this line to the top of a controller:
use App\models\Article;
Or use the full namespace when working with this model:
\App\models\Article::all();