Laravel - Public variable in controller - laravel

I want to have one public variable $users = User::all(); so i could use it in different methods inside controller and it doesn't work this way:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users = User::all();
public function __construct() {
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
return view('admin.users', compact('users'));
}
}
i get this error: Constant expression contains invalid operations
What am i doing wrong?

Try adding the assignment into the __construct() function instead:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users;
public function __construct() {
$this->users = User::all();
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
$users = $this->users;
return view('admin.users', compact('users'));
}
}

You need to initialize $users in your constructor:
<?php
public $users;
public function __construct() {
$this->middleware('auth');
$this->users = User::all();
}

Related

Class 'App\Http\Controllers\Post' not found in laravel-5.8

hi m trying to show data at index page but it says: Class 'App\Http\Controllers\Post' not found
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function index()
{
$data = Post::all();
return view('index', compact('data'));
}
You need to add the following line:
use App\Post;
now your code should look like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class IndexController extends Controller
{
public function index()
{
$data = Post::all();
return view('index', compact('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.

I get the Object not found error while trying to output all users details from database

I am trying to out all users on my admin dashboard by parsing the result through to my route
Here is my controller;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$user = User::all()->orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}
I get the "Method Illuminate\Database\Eloquent\Collection::orderBy does not exist." error
You have applied wrong query:
You don't need to add all() as you are using paginate
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$users = User::orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}

Can't get the laravel custom repository to work

I can't get my repository working, when i'm just trying the get the entire list of documents it returns nothing
Here's my DocumentRepository
<?php
namespace App\Repositories\Document;
interface DocumentRepository
{
public function getall();
public function getById($id);
public function create(array $attributes);
public function update ($id, array $attributes);
public function delete ($id);
}
Here's the functions
<?php
namespace App\Repositories\Document;
class EloquentDocument implements DocumentRepository
{
private $model;
public function __construct(Document $model)
{
$this->model = $model;
}
public function getall()
{
return $this->model->all();
}
public function getById($id)
{
return $this->findById($id);
}
public function create(array $attributes)
{
return $this->model->create($attributes);
}
public function delete($id)
{
$this->getById($id)->delete();
return true;
}
public function update($id array $attributes)
{
$document = $this->model->findOrFail($id);
$document->update($attribute);
return $document;
}
}
and here's the controller
<?php
namespace App\Http\Controllers;
use App\Repositories\Document;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class DocumentController extends Controller
{
/**
* #var DocumentRepository
*/
private $document;
/**
* TodoController constructor.
*/
public function __construct(DocumentController $document)
{
$this->document = $document;
}
public function getalldocuments()
{
return $this->document->getAll();
}
}
For your information there's two rows of data in my Documents table/model so i just want to get both of them by just simply returning but in my case it simply returns nothing.
Here's the route
Route::get('/documents', 'DocumentController#getalldocuments');
here's the registration part insite AppServiceProviders.php
public function register()
{
$this->app->singleton(DocumentRepository::class, EloquentDocument::class);
}
You are type-hinting DocumentController instead of your actual repository.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\Document\DocumentRepository;
class DocumentController extends Controller
{
/**
* #var DocumentRepository
*/
private $document;
public function __construct(DocumentRepository $document)
{
$this->document = $document;
}
public function getalldocuments()
{
return $this->document->getAll();
}
}
Now, assuming you have properly binded the interface to resolve to your document repository implemented, this should work.
For more information on how to bind interfaces to implementation, read this: https://laravel.com/docs/5.7/container#binding-interfaces-to-implementations
Edit: You have some syntax issues in your repository's interface. You are missing function:
<?php
namespace App\Repositories\Document;
interface DocumentRepository
{
public function getall();
public function getById($id);
public function create(array $attributes);
public function update($id, array $attributes);
public function delete($id);
}
Edit 2: Your binding is correct. However, I noticed that you are not binding your App\Document model to the implementation correctly.
<?php
namespace App\Repositories\Document;
use App\Document;
class EloquentDocument implements DocumentRepository
{
private $model;
public function __construct(Document $model)
{
$this->model = $model;
}
//
//
//
}
You need to add the correct use statement at the top. Assuming your document model resides in App\Document this should work.

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);
}

Resources