Undefined property: App\Http\Controllers\PermissionController::$permission - php

there is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
class PermissionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function _construct(Permission $permission)
{
$this->permission = $permission ;
$this-> middleware("auth") ;
}
public function index()
{
$permissions = $this->permission::all();
return view("permission.index", ['permissions' => $permissions]);
}
public function getAllPermissions(){
$permissions = $this->permission::all();
return response()->json([
'permissions' => $permissions
], 200);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

Your _construct is missing one underscore. It should be __construct.
In your code, the class property $permission was supposed to be set by the constructor, but since you mistyped it, it never happened. That is why you get the undefined property error

Related

Incompatible repository declarations using pattern design but only in server in Laravel 7

I have an application in my local environment and in a production server. This application have a controller called ArticlesController with this code:
<?php
namespace App\Admin\Controllers;
use App\Core\Controllers\CoreController;
use App\Admin\Requests\ArticlesRequest;
use App\Admin\Interfaces\ArticlesRepositoryInterface;
class ArticlesController extends CoreController
{
/**
* #var ArticlesRepositoryInterface
*/
private $articleRepository;
public function __construct(ArticlesRepositoryInterface $articleRepository)
{
$this->articleRepository = $articleRepository;
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\View\View
*/
public function index()
{
$articles = $this->articleRepository->all();
return view('admin.articles.index')->with(compact('articles'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\View\View
*/
public function create()
{
return view('admin.articles.create');
}
/**
* Store a newly created resource in storage.
*
* #param \App\Admin\Requests\ArticlesRequest $request
* #return \Illuminate\Routing\Redirector
*/
public function store(ArticlesRequest $request)
{
$data = $request->validated();
$article = $this->articleRepository->create($data);
return redirect()->route('articles.edit', $article)->with('successMessage', 'Article created! Now you can edit the article with new information');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(int $id)
{
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$article = $this->articleRepository->find($id);
return view('admin.articles.edit')->with(compact('article'));
}
/**
* Update the specified resource in storage.
*
* #param \App\Admin\Requests\ArticlesRequest $request
* #param int $id
* #return \Illuminate\Routing\Redirector
*/
public function update(ArticlesRequest $request, int $id)
{
$data = $request->validated();
$this->articleRepository->update($data, $id);
return redirect()->route('articles.index')->with('successMessage', 'Article updated!');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$this->articleRepository->delete($id);
return redirect()->route('articles.index')->with('successMessage', 'Article deleted!');;
}
}
How you can see, this controller uses ArticlesRepositoryInterface. This is the code:
<?php
namespace App\Admin\Interfaces;
use App\Admin\Models\Article;
use Illuminate\Database\Eloquent\Collection;
interface ArticlesRepositoryInterface extends BaseRepositoryInterface
{
/**
* #return Collection
*/
public function all(): Collection;
/**
* #param array $data
* #return Article
*/
public function create(array $data): Article;
/**
* #param array $data
* #param int $id
* #return int
*/
public function update(array $data, int $id): int;
/**
* #param int $id
* #return int
*/
public function delete(int $id): int;
/**
* #param int $id
* #return Article
*/
public function find(int $id): ?Article;
}
Also, I have a provider that I use to instantiate the repositories with this code:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Admin\Interfaces\BaseRepositoryInterface;
use App\Admin\Interfaces\ArticlesRepositoryInterface;
use App\Admin\Interfaces\FilesRepositoryInterface;
use App\Admin\Repositories\BaseRepository;
use App\Admin\Repositories\ArticlesRepository;
use App\Admin\Repositories\FilesRepository;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(BaseRepositoryInterface::class, BaseRepository::class);
$this->app->bind(ArticlesRepositoryInterface::class, ArticlesRepository::class);
$this->app->bind(FilesRepositoryInterface::class, FilesRepository::class);
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
The code of the BaseRepository is this:
<?php
namespace App\Admin\Repositories;
use App\Admin\Interfaces\BaseRepositoryInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
class BaseRepository implements BaseRepositoryInterface
{
/**
* #var Model
*/
protected $model;
/**
* #param Model $model
*/
public function __construct(Model $model)
{
$this->model = $model;
}
/**
* #return Collection
*/
public function all(): Collection
{
return $this->model->all();
}
/**
* #param array $data
* #return Model
*/
public function create(array $data): Model
{
return $this->model->create($data);
}
/**
* #param array $data
* #param int $id
* #return int
*/
public function update(array $data, int $id): int
{
return $this->model->where('id', $id)->update($data);
}
/**
* #param int $id
* #return int
*/
public function delete(int $id): int
{
return $this->model->destroy($id);
}
/**
* #param int $id
* #return Model
*/
public function find($id): ?Model
{
return $this->model->find($id);
}
}
And finally, the code of the ArticlesRepository is this:
<?php
namespace App\Admin\Repositories;
use App\Admin\Interfaces\ArticlesRepositoryInterface;
use App\Admin\Models\Article;
use Illuminate\Database\Eloquent\Collection;
use App\Admin\Repositories\BaseRepository;
class ArticlesRepository extends BaseRepository implements ArticlesRepositoryInterface
{
/**
* #var Article
*/
protected $article;
/**
* #param Article $article
*/
public function __construct(Article $article)
{
$this->article = $article;
}
/**
* #return Collection
*/
public function all(): Collection
{
return $this->article->all();
}
/**
* #param array $data
* #return Article
*/
public function create(array $data): Article
{
return $this->article->create($data);
}
/**
* #param array $data
* #param int $id
* #return int
*/
public function update(array $data, int $id): int
{
return $this->article->where('id', $id)->update($data);
}
/**
* #param int $id
* #return int
*/
public function delete(int $id): int
{
return $this->article->destroy($id);
}
/**
* #param int $id
* #return Article
*/
public function find($id): ?Article
{
return $this->article->find($id);
}
}
It works perfectly in my local environment, but, is strange, because in the remote server, with exactly the same code, it throws an error:
Declaration of App\Admin\Repositories\ArticlesRepository::create(array $data): App\Admin\Models\Article must be compatible with App\Admin\Repositories\BaseRepository::create(array $data): Illuminate\Database\Eloquent\Model
Any ideas?
All function declarations should be exactly the same, including the return type declarations:
ArticlesRepositoryInterface:
public function create(array $data): Article;
BaseRepository:
public function create(array $data): Model
ArticlesRepository:
public function create(array $data): Article;
App\Admin\Models\Article and Illuminate\Database\Eloquent\Model cannot be used both.
Perhaps this doesn't throw an exception locally because of a different PHP version?
Note: you might want to consider to extend all the repositories from a single BaseRepository.

How to fix this error? ErrorException Use of undefined constant staff - assumed 'staff' (this will throw an Error in a future version of PHP) [duplicate]

This question already has answers here:
What does the PHP error message "Notice: Use of undefined constant" mean?
(2 answers)
Closed 2 years ago.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\staff;
class staffController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view ('staff.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'staff_name' => 'required',
'staff_dept' => 'required',
'staff_phoneNo' => 'required'
]);
$staff = new Staff([
'staff_name' => $request->get('staff_name'),
'staff_dept' => $request->get('staff_dept'),
'staff_phoneNo' => $request->get('staff_phoneNo')
]);
$staff->save();
return redirect()->route(staff.create)->with('success','Data Added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
return redirect()->route('staff.create')->with('success','Data Added');
Minor change in your code.

Laravel NotFoundHttpException No query results for model

I have error NotFoundHttpException No query results for model [App\ThreadForum]
My web.php :
Route::get('/threads','ThreadForumController#index');
Route::post('/threads','ThreadForumController#store');
Route::get('/threads/create','ThreadForumController#create');
Route::get('/threads/{thread}','ThreadForumController#show')->name('threads.show');
My model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class ThreadForum extends Model
{
protected $fillable = [
'user_id','title','body'
];
public function path(){
return route('threads.show',$this->id);
}
public function replies(){
return $this->hasMany('App\Reply');
}
public function creator(){
return $this->belongsTo('App\User', 'user_id');
}
public function addReply($reply){
$this->replies()->create($reply);
}
}
My controller:
namespace App\Http\Controllers;
use App\ThreadForum;
use Illuminate\Http\Request;
class ThreadForumController extends Controller
{
public function __construct()
{
$this->middleware('auth')->only('store');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$threads = ThreadForum::all();
return view('threads.index', compact('threads'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('threads.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd('store');
/*$thread = ThreadForum::create([
'user_id'=>auth()->id(),
'title'=>$request['title'],
'body'=>$request['body']
]);
redirect($thread->path());*/
}
/**
* Display the specified resource.
*
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function show(ThreadForum $thread)
{
return view('threads.show',compact('thread'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function edit(ThreadForum $threadForum)
{
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function update(Request $request, ThreadForum $threadForum)
{
}
/**
* Remove the specified resource from storage.
*
* #param \App\ThreadForum $threadForum
* #return \Illuminate\Http\Response
*/
public function destroy(ThreadForum $threadForum)
{
//
}
}
All functions work well, but when I run post('/threads') for store new record I get error.
I tried use dd('store') for debugging, but I dont see this text, only error.
How can I fix it?
Thanks.
I cannot comment, but to dump you have to use dd($request) not dd($store)
EDIT: I think that problem is in show method. You use compact, but there's no any variable above it. So you cannot pass anything to view.

Request Validation with Laravel 5.3

I am trying to validate my api call using laravel built-in request method.
I have used --resource to get make it REST.
OneTimePasswordController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Models\OneTimePassword as Model;
use App\Http\Requests\OneTimePasswordReq;
class OneTimePasswordController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(UserController $user)
{
//
$otps = Model::get();
return response()->json($otps);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(OneTimePasswordReq $request)
{
$insert = Model::create($request->all());
return response()->json($insert);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
$delete = Model::destroy($id);
return response()->json($delete);
}
}
OneTimePasswordReq
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
class OneTimePasswordReq extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'mobile' => 'required',
'code' => 'required',
];
}
protected function formatErrors(Validator $validator)
{
return $validator->errors()->all();
}
/**
* Set custom messages for validator errors.
*
* #return array
*/
public function messages()
{
return [
'mobile.required'=>"Mobile field is required"
];
}
}
If i pass my params, with values, it gets inserted.
Now if i pass a post request with mobile field deleted, i expect a validation errors.
But the call is made to index method which fetches all data from the url.
My understanding is the request is rejected because the params is missing and the question is why its changing to index method and how do i get the errors?
Note : I am aware about $validator->fails() concept, which i don't want to put into my controller as laravel offers this.
As i tested it with postman, it was redirecting to the same route and picking as get.
If called with javascript code, the errors are displayed.
To check with postman, you need add in headers
X-Requested-With: XMLHttpRequest
Thanks to all supporters.

Laravel 5 Resource Controller

I created a resource controller in Laravel 5, below are the details. However I am having this error when accessing the routes? I tried all routes and all of them produce the same erro. NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 802
at Router->findRoute(object(Request)) in Router.php line 670
Route.php:
Route::controller('my','myController');
Controller file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Just change this:
Route::controller('my','myController');
To:
Route::resource('my','myController');
More info about the Resource controllers can be found in the Laravel Docs

Categories