Model class is still not found on Laravel 4 - model-view-controller

I am creating an app in Laravel 4.2 and when I create a model, I get an exception that it's not found:
models/Annulations.php :
class Annulation extends Eloquent {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'annulation_remboursement';
controllers/AnnulationsController.php :
class AnnulationsController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$annulations = Annulation::all();
return View::make('Annulations.index')->withAnnulations($annulations);
}
On stackoverflow, i found the solution about a command :
$ php artisan dump-autoload
If i try this command then I have still this error :
Class 'Annulation' not found
Could you help me?

If you create your model Annulations the your class should be class Annulations.
For example:
folder path: models/Annulations
class Annulations extends Eloquent{
//logic here
}
In your controller:
public function someController(){
$annulations = Annulations::all(); //call your model
print_r($annulations); //print to check if data exists
return View::make('Annulations.index')->withAnnulations($annulations);
}

Related

How to use laravel repository pattern searchable array?

I am using laravel-repository pattern ,i have one api which is responsible for getting all users it's working fine ,if we are using that package by default search should support for that i set $fieldSearchable array in the repository class.
i hit an api like this localhost.com/api/lists?search=foo,it's not working can you please help me where did i mistake
UserController.php
public function __construct(UserRepositoryInterface $repository)
{
$this->repository = $repository;
}
public function getUsers(){
$data = $this->repository->show();
return response()->json(fractal($data, new UserTransformer()));
}
UserRepositoryInterface.php
interface UserRepositoryInterface extends RepositoryInterface
{
public function show();
}
UserRepository.php
<?php
namespace App\Repositories;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\User as AppUser;
use App\UserSection;
use App\Validators\UserValidator;
use Illuminate\Support\Facades\DB;
/**
* Class UserRepositoryEloquent.
*
* #package namespace App\Repositories;
*/
class UserRepository extends BaseRepository implements UserRepositoryInterface
{
protected $fieldSearchable = ['phone_number'];
/**
* Specify Model class name
*
* #return string
*/
public function model()
{
return AppUser::class;
}
/**
* Boot up the repository, pushing criteria
*/
public function boot()
{
$this->pushCriteria(app(RequestCriteria::class));
}
public function show(){
return $this->model()::get();
}
}
It maybe resolved by utilising pre-difined methods No need to write show() function logic because by default l5-Repository pattern contains some methods to get all the data all()or paginate().in your controller write like this in getUsers()
$data = $this->repository->all();
or
$data = $this->repository->paginate('25');
all() is for fetch all the data from DB and paginate($limit) is fetch the data per page based on the limit.
if you are using any one of the above mentioned method then automatically search functionality will work

Access controller variable directly inside component class - Laravel

Normally we pass the parameter from controller into x component like <x-book-list books="$books" /> and access it in BookList Class as below
namespace App\View\Components;
use Illuminate\View\Component;
class BookList extends Component
{
public $books;
/**
* BookList Component
*
* #param array $book
*
* #return void
*/
public function __construct(array $books)
{
$this->books = $books;
}
I use this kind of component in my app repeatedly but here, I want to make it a bit cleaner without adding books attribute every-time I call it because they are definitely the same everywhere.
So, can I access the books variable inside Book component class without passing through <x-book-list />?
Probably something as below.
namespace App\View\Components;
use Illuminate\View\Component;
class BookList extends Component
{
public $books;
/**
* BookList Component
*
* #param array $book
*
* #return void
*/
public function __construct()
{
// How to access $book variable directly from controller without passing from <x-booklist /> ????
}
just addd the property as tag like:
<x-my-component
:variable="$variable"
/>
MyComponent.php
...
class MyComponent extends Component
{
public $variable;
public function __construct($variable)
{
$this->variable= $variable;
}
}
and thats all

Laravel 5.3 Call to a member function groupBy() on null

I'm getting the following error:
FatalErrorException in EloquentVehicle.php line 30: Call to a member
function groupBy() on null
I have the following code:
<?php
namespace App\Project\Frontend\Repo\Vehicle;
use Illuminate\Database\Eloquent\Model;
class EloquentVehicle implements VehicleInterface
{
protected $vehicle;
/**
* EloquentVehicle constructor.
*
* #param Model $vehicle
*/
public function __construct(
Model $vehicle
)
{
$this->$vehicle = $vehicle;
}
/**
* Fetch unique makes
*
* #return mixed
*/
public function fetchMakes()
{
return $this->vehicle->groupBy(array('make'))
->orderBy('make', 'asc')
->get();
}
}
I've checked Illuminate\Database\Eloquent\Model for the method which is obviously not there, but I don't know what I should be adding to my class so that I can use the groupBy method. The laravel docs say the method exists.
UPDATE: Apparently I can't typehint an abstract class. I don't know how else I should be going about using Eloquent to retrieve records. If it helps, below is the code I have for registering the classes to the service container
<?php
namespace App\Providers;
use App\Vehicle;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
use Illuminate\Support\ServiceProvider;
class RepoServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('App\Project\Frontend\Repo\Vehicle\VehicleInterface', function($app)
{
return new EloquentVehicle(
new Vehicle
);
});
}
}
I just found my mistake and quite literally lay my face in my palms.
This
$this->$vehicle = $vehicle;
should be this
$this->vehicle = $vehicle;

Laravel - Larabook "Method [execute] does not exist."

I'm following the Laracasts's Larabook tutorial. I'm on the 'Following Users' part. I added a follow button to all user's profiles, but I'm stuck with "BadMethodCallException Method [execute] does not exist." error. Here is my FollowsController;
<?php
use Larabook\Users\FollowUserCommand;
class FollowsController extends \BaseController {
/**
* Follow a user
*
* #return Response
*/
public function store()
{
//id of the user to follow
//id of the authenticated user
$input = array_add(Input::all(), 'userId', Auth::id());
$this->execute(FollowUserCommand::class, $input);
Flash::success('You are now following this user.');
return Redirect::back();
}
/**
* Unfollow a user
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
According to the instructions of the laracasts/Commander package you need to inject the Commander Trait in your controller to be able to access methods like execute.
You can do this either in your BaseController so you can use it in every controller that extends BaseController:
class BaseController extends Controller {
use CommanderTrait;
}
Or just in the controller itself:
class FollowsController extends \BaseController {
use CommanderTrait;
// your methods
}

Laravel 4 facade class not calling functions on facade root class

I've set up a package in laravel 4 via the artisan workbench command. I created a facade class and followed this tutorial to come up with the following service provider, facade and root classes:
src/Spoolphiz/Infusionsoft/InfusionsoftServiceProvider.php:
namespace Spoolphiz\Infusionsoft;
use Illuminate\Support\ServiceProvider;
class InfusionsoftServiceProvider extends ServiceProvider {
protected $defer = false;
/**
* Bootstrap the application events.
*
* #return void
*/
public function boot()
{
$this->package('spoolphiz/infusionsoft');
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
// Register 'infusionsoft' instance container to our Infusionsoft object
$this->app['infusionsoft'] = $this->app->share(function($app)
{
return new Spoolphiz\Infusionsoft\Infusionsoft;
});
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return array();
}
}
src/Spoolphiz/Infusionsoft/Facades/Facade.php:
namespace Spoolphiz\Infusionsoft\Facades;
use Illuminate\Support\Facades\Facade;
class Infusionsoft extends Facade {
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor() { return 'infusionsoft'; }
}
Finally I've set up the underlying class thats to be connected to the facade at src/Spoolphiz/Infusionsoft/Infusionsoft.php:
namespace Spoolphiz\Infusionsoft;
//use Spoolphiz\Infusionsoft\iSDK;
/*
This is hackish and a un-laravel way to handle the requirement of \iSDK but unfortunately the xmlrpc3.0 lib doesn't want to correctly encode values when run with a namespace. Will try to resolve this later.
*/
require_once(__DIR__.'/isdk.php');
class Infusionsoft extends \iSDK {
protected $_app;
/**
* Init the sdk
*
*/
public function __construct( $connectionName )
{
$this->_app = parent::cfgCon($connectionName);
}
public function test()
{
dd('works');
}
}
I set up the service provider and the facade alias of Infusionsoft in app/config/config.php.
When I try running methods belonging to the extended iSDK class against an instance of Spoolphiz\Infusionsoft\Facade\Infusionsoft I get undefined method errors, such as the following:
Call to undefined method Spoolphiz\Infusionsoft\Facades\Infusionsoft::loadCon()
Why is this? The whole point of facades is to be able to call methods against its root class...
Looks like I was being stupid. I was developing this package in the laravel workbench. Once it was done I submitted it to packagist and set up a requirement for it in the same laravel app. Having the package installed in vendors directory and in the workbench caused some sort of conflict.
Lesson learned: make sure you dont have the same package in your workbench and in your application's vendors directory.

Resources