I'm trying to use a Repository, but I'm getting this error:
Class App\Repositories\CategoryRepository does not exist
This is my CategoryRepository.php
<?php
namespace App\Repositories;
class SubCate
{
/**
* Get all of the tasks for a given user.
*
* #param User $user
* #return Collection
*/
public function getCategories(){
$categories=\App\category::where('parent_id',0)->get();//united
$categories=$this->addRelation($categories);
return $categories;
}
}
?>
And this is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Product;
use App\Category;
use App\Repositories\CategoryRepository;
class ProductController extends Controller
{
//
public function __construct(CategoryRepository $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
public function index(Request $request)
{
$subcate = new SubCate;
try {
$allSubCategories=$subcate->getCategories();
} catch (Exception $e) {
//no parent category found
}
return view('welcome', [
'allSubCategories' => $allSubCategories,
]);
}
}
What is wrong?
Your category repository class name is
class SubCate
but you are using
use App\Repositories\CategoryRepository; .
So, change your class name to CategoryRepository
Related
I have this code
Controller
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct(Notification $notification)
{
$this->notification = $notification;
}
public function index()
{
return $this->notification->sendMessage('test');
}
Interface
<?php
namespace App\Contracts\Exchange;
interface Notification
{
public function sendMessage($message);
}
File Kernel.php
namespace App\Providers;
use App\Contracts\Exchange\Notification;
use App\Exchange\Helpers\Notification\Telegram;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(Notification::class, function (){
return new Telegram(env('TELEGRAM_EXCHANGE_TOKEN'), env('TELEGRAM_EXCHANGE_CHAT_ID'));
});
}
If I try to use new Locker(); I get a TypeError error: Too few arguments to function App\Exchange\Helpers\Locker::__construct(), 0 passed in Psy Shell code on line 1 and exactly 1 expected
Your controller should extend Illuminate\Routing\Controller in order for dependency injection to work. Or just refactor your __construct method using app helper:
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct()
{
$this->notification = app(Notification::class);
}
public function index()
{
return $this->notification->sendMessage('test');
}
}
I have this command line that doesn't work but I tried namespace app\Models\Job; or use app\Models\Job; or namespace App\Models\Job; or use App\Models\Job;.
I have also tried directly adding App\Models\Job to the command line but it didn't seem to work.
namespace App\Http\Controllers;
use app\Models\Job;
use Illuminate\Http\Request;
class JobController extends Controller
{
public function __construct(){
$this->middleware(['employer','verified'],['except'=>array('index','show','apply','allJobs','searchJobs','category')]);
}
public function index(){
$jobs = Job::latest()->limit(5)->where('status',1)->get();
$categories = Category::with('jobs')->paginate(5);
$companies = Company::get()->random(6);
return view('welcome',compact('jobs','
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Job extends Model
{
use HasFactory;
protected $fillable = ['user_id','company_id','title','slug','description','roles','category_id','position','address','type','status','last_date','number_of_vacancy','experience','gender','salary'];
public function getRouteKeyName(){
return 'slug';
}
public function company(){
return $this->belongsTo('App\Company');
}
public function users(){
return $this->belongsToMany(User::class)->withTimeStamps();
}
public function checkApplication(){
return \DB::table('job_user')->where('user_id',auth()->user()->id)->where('job_id',$this->id)->exists();
}
public function favorites(){
return $this->belongsToMany(Job::class,'favourites','job_id','user_id')->withTimeStamps();
}
public function checkSaved(){
return \DB::table('favourites')->where('user_id',auth()->user()->id)->where('job_id',$this->id)->exists();
}
}
You should do this instead:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Job; // change app to App
class JobController extends Controller
{...
I am trying to use Laravel view composer. I have registered my class in config/app.php but I keep getting the following error:
"Class App\Http\ViewComposers\PostComposer does not exist
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use View;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
View::composer('plain','App\Http\ViewComposers\PostComposer');
}
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
}
my post composer class
<?php
namespace App\Http\ViewComposer;
use Illuminate\View\View;
use App\Post;
class PostComposer
{
public function comspose(View $view)
{
$posts = Post::all();
$view->with('postha', $posts );
}
}
and here is the screenshot of my browser:
![folder structure in my app][]
Your namespace is wrong.
You're importing from (plural):
App\Http\ViewComposers\PostComposer
but the namespace of your ViewComposer isn't plural:
App\Http\ViewComposer
try it : namespace App\Http\ViewComposer To namespace App\Http\ViewComposers
I'm quite new in Laravel 5, what I am trying to do is a simple repository with dependency injection. But I'm stuck with this error:
Argument 1 passed to
App\Http\Controllers\Api\UserController::__construct() must implement
interface App\Repositories\UserInterface, instance of
App\Repositories\UserRepository given
Here is my code:
UserController:
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Response;
use App;
use Auth;
use Crypt;
use Lang;
use Image;
use Storage;
use Config;
use Validator;
use App\User;
use App\Repositories\UserInterface;
class UserController extends Controller
{
protected $config;
protected $users;
public function __construct(UserInterface $users)
{
$this->middleware('api');
$this->middleware('auth', ['except' => 'getInfo']);
$this->users = $users;
$this->config = Config::get('images.avatar');
}
UserInterface:
namespace App\Repositories;
use App\Repositories\BaseInterface;
interface UserInterface extends BaseInterface
{
};
BaseInterface:
namespace App\Repositories;
interface BaseInterface
{
public function all();
public function paginate($count);
public function find($id);
}
BaseRepository
namespace App\Repositories;
use App\Repositories\BaseInterface;
class BaseRepository implements BaseInterface
{
protected $model;
public function __call($name, $args)
{
// $this->getNewInstance()->{$name($args)};
return call_user_func_array([
$this->getNewInstance(),
$method], $args);
}
public function all($relations = [])
{
$instance = $this->getNewInstance();
return $instance->with($relations)->all();
}
public function find($id, $relations = [])
{
$instance = $this->getNewInstance();
return $instance->with($relations)->find($id);
}
public function findOrFail($id, $relations = [])
{
$instance = $this->getNewInstance();
return $instance->with($relations)->findOrFail($id);
}
public function paginate($count)
{
}
protected function getNewInstance()
{
return new $this->model;
}
}
UserRepository
namespace App\Repositories;
use App\Repositories\BaseRepository;
Class UserRepository extends BaseRepository
{
protected $model = 'App\User';
}
RepositoryServiceProvider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register any error handlers.
*
* #return void
*/
public function boot()
{
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
//
App::bind('App\Repositories\UserInterface', 'App\Repositories\UserRepository');
}
}
Of course RepositoryServiceProvider is added under service providers in my config/app.php
Please help, I'm almost sure that I've tried everything whatever I found in Google.
Your UserRepository has to implement UserInterface:
namespace App\Repositories;
use App\Repositories\BaseRepository;
class UserRepository extends BaseRepository implements UserInterface
// ^^^^^^^^^^^^^^^^^^^^^^^^
{
protected $model = 'App\User';
}
I am looking to display a list of students that have the same course ID as the current user (tutor).
http://snag.gy/VOHJ3.jpg Here is my database design.
<?php
namespace Simple\ProfileBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
class SecurityController extends Controller
{
public function loginAction(Request $request)
{
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('SimpleProfileBundle:Security:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
public function dumpStringAction()
{
$findStudents = $this->getUser()->getCourses();
$results = $this->_em
->createQuery("SELECT * FROM user where")
->getResult();
return $results;
}
return $this->render('SimpleProfileBundle:Security:dumpString.html.twig', array(
'findstudents'=> $findStudents));
}
}
Anyone have any idea how i can do this ? I was thinking of a custom query however i am unsure how to do so?
Cheers
First of all if you want to use custom queries, you should do that by creating entities' repository.
Example:
Entity:
<?php
namespace YourName\YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* YourClass
*
* #ORM\Entity(repositoryClass="YourName\YourBundle\Entity\Repository\YourClassRepository")
* #ORM\Table(name="your_class")
*/
class YourClass
{
// your entity definition
}
Then you have to create entity repository class:
<?php
namespace YourName\YourBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
/**
* YourClassRepository
*/
class YourClassRepository extends EntityRepository
{
public function getStudentsByCourseID($courseId)
{
$qb = $this->_em->createQueryBuilder();
$qb
->select('student')
->from('YourNameYourBundle:YourClass', 'student')
->leftJoin('YourNameYourBundle:Course', 'course')
->where('course.id == :courseId');
$qb->setParameter('courseId', $courseId);
return $qb->getQuery()->getArrayResult();
}
Then you can call your custom query in your controller:
<?php
namespace Simple\ProfileBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
class SecurityController extends Controller
{
public function loginAction(Request $request)
{
// your code here...
}
public function yourAction($courseID)
{
$repo = $this->getDoctrine()->getRepository('YourNameYourBundle:YourClass');
$students = $repo->getStudentsByCourseID($courseID);
return [
'students' => $students
];
}
}
I think that's what you need.