ReflectionException in compiled.php line 1049: Class App\Http\Controllers\User does not exist - laravel-5

i test laravel 5 that i have controller name Usercontroller.php have code:
class UserController extends Controller
{
protected $user = null;
public function __construct(User $user)
{
$this->user = $user;
}
public function allUsers()
{
return $this->user->allUsers();
}
}
and i create model name user.php have code:
class User extends Model
{
public function allUsers()
{
return self::all();
}
}
when i run it show error like below
ReflectionException in compiled.php line 1049: Class App\Http\Controllers\User does not exist
please help me solve about this problem, thanks you.

The User model is found in the namespace App and you have to reference it like that:
public function __construct(\App\User $user)
{
$this->user = $user;
}
Or add an import statement at the top:
use App\User;
class UserController extends Controller

Related

How to share $request variable between other function from other url in same controller on laravel

I want to share $request variable between other function from other url in same controller on laravel like below but how ?
Controller
class ABCController extends Controller
{
public function validation(Request $request)
{
---------------
}
public function save()
{
Log::debug($request)
}
api.php
Route::post('/abc',[ABCController::class,'validation']);
Route::get('/save',[ABCController::class,'save']);
What I tried
class ABCController extends Controller
{
public function validation(Request $request)
{
Session::put('data', $request)
session(['data' => $request]);
Log::debug(Session::get('data'));
Log::debug(session('data'));
}
public function save()
{
Log::debug(Session::get('data'));
Log::debug(session('data'));
}
I tried above but Log::debug in save function show me null in log.
Please give me advice.
Create a global variable
class ABCController extends Controller
{
private $data;
public function validation(Request $request)
{
$this->data =$request;
}
public function save()
{
echo $this->data;
}
}

Laravel repository Class App\Repository\User does not exist

Hello I want to use my own repository class in my Laravel 5.8 project
I created my file Repository in the App File and in this file I added A class called ConversationRepository
This is my class:
<?php
namespace App\Repository;
class ConversationRepository{
private $user;
public function __construct(User $user){
$this->user=$user;
}
public function getConversation(int $userId){
return $this->user->newQuery()
->select('name','id')
->where('id','!=',$userId)
->get();
}
}
And then when I use it on my controller :
<?php
namespace App\Http\Controllers;
use App\User;
use Auth;
use Illuminate\Http\Request;
use App\Repository\ConversationRepository;
class ConversationsController extends Controller
{
private $r;
private $auth;
public function __construct(ConversationRepository $conversationRepository,AuthManager $auth){
$this->r = $conversationRepository;
$this->auth = $auth;
}
public function index(){
return view('conversation.index',[
'users'=>$this->r->getConversation($this->auth->user()->id)
]);
}
public function show(User $user){
return view('conversation.show',['users'=>$this->r->getConversation(
$this->auth->user()->id),
'user'=>$user
]);
}
public function store(User $user){
}
}
I get the error
Class App\Repository\User does not exist
Apparently, you forgot to add use App\User; in the class ConversationsController file.

Update the plugin will be something wrong

When I update the Laravel plugin, if I use some static method e.g. User::find($id) it will show a warning: "Non-static method User::find() should not be called statically", this very bad!
Example:
namespace App\Http\Controllers\Web;
use App\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;
class TestController extends Controller
{
public function __construct(Application $wechat)
{
parent::__construct($wechat);
}
public function index(Request $request)
{
$user = User::find(12);
dd($user);
}
}
public function index(Request $request)
{
$user = User::all()->find(12);
dd($user);
}

Laravel Multiple module using one Controller

Now I have few modules created like Product, Sale, Category. I found out they actually using same function with similar process. For example update() in Controller :
Category
public function update($id)
{
$instance = Category::findOrFail($id);
$instance->fill(Input::all())->save();
}
Product
public function update($id)
{
$instance = Product::findOrFail($id);
$instance->fill(Input::all())->save();
}
How can I join it together to BaseController by just make the Model dynamic?
Something like this:
abstract class ResourceController extends BaseController
{
protected $entity;
public function __construct(Model $entity){ //or Eloquent, depending on your import alias
$this->entity = $entity;
}
public function update($id)
{
$instance = $this->entity->findOrFail( $id );
$instance->fill( Input::all() )->save();
}
}
class ProductController extends ResourceController{
public function __construct(Product $entity){
parent::__construct($entity);
}
}
class CategoryController extends ResourceController{
public function __construct(Category $entity){
parent::__construct($entity);
}
}

Type hinting parent::__construct() arguments in controllers

I've got a BaseController in a Laravel Framework based App with the following code:
class BaseController extends Controller {
public function __construct(Credentials $credentials) {
$this->credentials = $credentials;
}
Then, all my other controllers will Extend the BaseController:
class PostController extends BaseController {
public function __construct(PostRepository $post)
{
$this->post = $post;
parent::__construct();
}
However, I'd need to type hint the Credentials Class in the parent::__construct(); of all my controllers. Is there any way to avoid that?
Thanks in advance
I can solve it using the following code:
class BaseController extends Controller {
public function __construct()
{
$this->credentials = App::make('Credentials'); // make sure to use the fully qualified namespace of Credentials if need be
}
}

Resources