Laravel Controller error - php

I'm learning how to do a simple Laravel app, an social network and I came across an error
[Fri Aug 26 00:46:30 2016] PHP Fatal error: Class 'App\Http\Controllers\Post' not found in C:\xampp\htdocs\social-network\app\Http\Controllers\PostController.php on line 13
My PostController.php is this:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function postCreatePost(Request $request)
{
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
My Post model is this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
}
I don't understand what I did wrong. Can anyone help me figure out the error. Thanks in advance

You need to add the model.
Try to add following line in your controller after namespace
use App\Post;
This is what the error message exactly telling you.

You forgot to use tell your controller to use App\Post:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller{
public function postCreatePost(Request $request){
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
Also, typically good idea to make sure you run composer dump-autoload after adding new classes.

Related

inheritance i laravel :Class name must be a valid object or a string

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.

Class 'App\Http\Controllers\customer' not found Laravel 5.2

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\customer;
use App\Http\Requests;
class CustomerController extends Controller
{
public function index()
{
return view('insert');
}
public function create()
{
}
public function store(Request $request)
{
$customer = new customer;
$customer->name = $request->name;
$customer->sex = $request->sex;
$customer->pob = $request->pob;
$customer->tel = $request->tel;
$customer->email = $request->email;
$customer->save();
}
public function show()
{
return view('show');
}
}
?>
I m getting error as Class 'App\Http\Controllers\customer' not found Laravel 5.2.
I have use App\customer;
use Inputs;
Why am I getting error ?
What is the problem in the code?
If your customer class is in App namespace, you need to use:
use App\customer;
instead of
use App\Http\Controllers\customer;
This is simple You need to understand. Make sure you have created a model Customer.php in app/Customer.php and another important thing you must create your table with customers that's all. After that you need to add this line before class like:-
use App\Customer;
Hope it helps :)

My Controller isn't found

route in routes.php
Route::get('korisnici', array('uses'=>'MojPrviKontroler#prvaAkcija'));
//
my controler in Controllers
<?php
use App\User;
use App\Http\Controllers\Controller;
class MojPrviKontrolerController extends Controller
{
public $restful = true;
public function get_prvaAkcija()
{
return View::make('prviViewovi.PrviView.php');
}
}
Can somebody tell my why my controller isn't found?
Seems like a wrong namespace problem.
Change to this:
<?php
namespace App\Http\Controllers;
use App\User;

Laravel "Class MyClass not found"

My problem:
I have a controller that calls a method from another controller for some information. However, Laravel isn't able to locate the class in that controller.
FatalErrorException in TradesController.php line 35: Class 'Profile'
not found
What I have tried:
I am using Laravel 5.2 and have created the controller with php artisan make:controller Profile to ensure that any possible internal pointers (in lack of better vocabulary) are created - even though my understanding is that Laravel 5.2 does this automatically as long as controllers are in the \app directory.
They both reside within \app\Http\Controllers
My code
TradesController calls class Profile
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Profile;
use Session;
use Auth;
class TradesController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function showInventory (Request $request){
.....
// Following three calls all fail
$profile = new Profile;
$profile = Profile()->linkToProfile();
$profile = Profile::linkToProfile();
.....
return($output);
}
}
MY class Profile-controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use Auth;
class ProfileController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function linkToProfile (Request $request) {
return("test");
}
}
php artisan make:controller xxx creates a class xxxController therefore your class is ProfileController not Profile.

How to call models in Laravel 5?

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)

Categories