Target class [App\Repositories\User\UserRepository] does not exist - laravel

I'm using laravel and try to integrate the repository in laravel.
follow this link https://www.itsolutionstuff.com/post/laravel-5-repository-pattern-tutorial-from-scratchexample.html.
but getting the below issue. don't know what's wrong with code. try to find a solution but not getting any particular solution.
UserInterface
namespace App\Repositories\User;
interface UserInterface {
public function getAll();
public function find($id);
public function delete($id);
}
UserRepoServiceProvide
use Illuminate\Support\ServiceProvider;
class UserRepoServiceProvide extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('App\Repositories\User\UserInterface', 'App\Repositories\User\UserRepository');
}
UserRepository
use App\Repositories\User\UserInterface as UserInterface;
use App\User;
class UserRepository implements UserInterface
{
public $user;
function __construct(User $user) {
$this->user = $user;
}
public function getAll()
{
return $this->user->getAll();
}
public function find($id)
{
return $this->user->findUser($id);
}
public function delete($id)
{
return $this->user->deleteUser($id);
}
}
User Model
public function getAll()
{
return static::all();
}
public function findUser($id)
{
return static::find($id);
}
public function deleteUser($id)
{
return static::find($id)->delete();
}
UserController
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Repositories\User\UserInterface as UserInterface;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function __construct(UserInterface $user)
{
$this->user = $user;
}
public function index()
{
$users = $this->user->getAll();
return view('users.index',['users']);
}
}
composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"App\\Repositories\\": "app/Repositories/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
but still getting this issue
Please Help.
folder path

In your UserController :
remove
use App\Repositories\User\UserInterface as UserInterface;
and add
use App\Repositories\User\UserRepository as UserInterface;

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

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.

Add function to laravel Notification

I have next Notification class:
class WelcomeNotification extends Notification
{
use Queueable;
public function __construct()
{
//
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
//
];
}
}
And I want add some function to this. For example:
public function myFunction()
{
return 'something';
}
But $user->notifications->first()->myFunction return nothing
When you call the notifications() relation it turns out is a polymorphic relation using the DatabaseNotification model. The proper way is to inherit DatabaseNotification and write the custom function their.
For example, create app/DatabaseNotification/WelcomeNotification.php and inherit DatabaseNotification model.
namespace App\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotification;
class WelcomeNotification extends DatabaseNotification
{
public function foo() {
return 'bar';
}
}
And override the notifications() function that uses the Notifiable trait:
use App\DatabaseNotification\WelcomeNotification;
class User extends Authenticatable
{
use Notifiable;
...
public function notifications()
{
return $this->morphMany(WelcomeNotification::class, 'notifiable')
->orderBy('created_at', 'desc');
}
...
}
And now you can call the custom function as follows:
$user->notifications->first()->foo();

How can I solve Target [App\Repositories\NewsRepository] is not instantiable while building [App\Http\Controllers\Member\NewsController].?

My controller like this :
...
use App\Repositories\NewsRepository;
class NewsController extends Controller
{
protected $repository;
public function __construct(NewsRepository $repository)
{
$this->repository = $repository;
}
public function store(NewsCreateRequest $request)
{
...
$news = $this->repository->create($input);
...
}
}
My interface like this :
namespace App\Repositories;
use Prettus\Repository\Contracts\RepositoryInterface;
interface NewsRepository extends RepositoryInterface
{
//
}
My class like this :
...
use App\Repositories\NewsRepository;
class NewsRepositoryEloquent extends BaseRepository implements NewsRepository
{
public function model()
{
return News::class;
}
public function boot()
{
$this->pushCriteria(app(RequestCriteria::class));
}
}
My repository service provider like this :
...
class RepositoryServiceProvider extends ServiceProvider
{
...
public function register()
{
...
$this->app->bind(\App\Repositories\NewsRepository::class, \App\Repositories\NewsRepositoryEloquent::class);
}
}
If the method store on the controller executed, there exist error :
(1/1) BindingResolutionException Target
[App\Repositories\NewsRepository] is not instantiable while building
[App\Http\Controllers\Member\NewsRepository].
How can I solve the error?
You have to add constructor to your repository and call parent constructor for setup model like this:
public function __construct(Permission $model)
{
parent::__construct($model);
}

Magento 2 Error - Model collection resource name is not defined

I'm going through Magento 2 tutorials and I'm having trouble getting a collection from my custom model's factory after calling the create() method. It throws an error saying the "Model collection resource name is not defined". I've already cleared /var/generation and recompiled the di.
Company/Module/Model/Vendor.php
namespace Company\Module\Model;
class Vendor extends \Magento\Framework\Model\AbstractModel {
protected function _constructor() {
$this->_init('Company\Module\Model\Resource\Vendor');
}
}
Company/Module/Model/Resource/Vendor.php
namespace Company\Module\Model\Resource;
class Vendor extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()
{
$this->_init(
'company_vendor',
'vendor_id'
);
}
}
Company/Module/Model/Resource/Vendor/Collection.php
namespace Company\Module\Model\Resource\Vendor;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init(
'Company\Module\Model\Vendor',
'Company\Module\Model\Resource\Vendor'
);
}
}
Company/Module/Block/VendorList.php
namespace Company\Module\Block;
class VendorList extends \Magento\Framework\View\Element\Template {
protected $vendorFactory;
public function __construct(\Magento\Framework\View\Element\Template\Context $context,
\Company\Module\Model\VendorFactory $vendorFactory,
array $data = [])
{
parent::__construct($context, $data);
$this->vendorFactory = $vendorFactory;
}
public function getVendors() {
return $this->vendorFactory->create()->getCollection()->getItems(); //fails on getCollection()
}
This is the error I get:
1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): Model collection resource name is not defined.
You need to do following change.
Company/Module/Model/Vendor.php
namespace Company\Module\Model;
class Vendor extends \Magento\Framework\Model\AbstractModel {
protected function _constructor() {
$this->_init('Company\Module\Model\ResourceModel\Vendor');
}
}
Company/Module/Model/ResourceModel/Vendor.php
namespace Company\Module\Model\ResourceModel;
class Vendor extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()
{
$this->_init('company_vendor','vendor_id');
}
}
Company/Module/Model/ResourceModel/Vendor/Collection.php
namespace Company\Module\Model\ResourceModel\Vendor;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
public function _construct()
{
$this->_init('Company\Module\Model\Vendor','Company\Module\Model\ResourceModel\Vendor'
);
}
}
The issue was I had _constructor() instead of _construct()
namespace Company\Module\Model;
class Vendor extends \Magento\Framework\Model\AbstractModel {
protected function _construct() {
$this->_init('Company\Module\Model\Resource\Vendor');
}
}

Resources