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

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
}

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

When i am registering user observer in Laravel while fetching single user also i'm getting list of all users

Getting issue with Laravel Observer
I created a user observer when i'm doing api call in order to fetch single user also i'm getting list of all user exist in table. what i'm doing wrong.
Here my ObserverProvider and UserObserver which i register in app.php
ObserversServiceProvider
namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class ObserversServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot() {
User::observe(UserObserver::class);
}
/**
* Register the application services.
*
* #return void
*/
public function register() {
}
}
UserObserver
namespace App\Observers;
use App\User;
class UserObserver
{
/**
* Listen to the User created event.
*
* #param User $user
* #return void
*/
public function created(User $user) {
// doing something
}
}
I'm getting list of all user when i'm doing.
$user = User::where('user_id',1)->first();

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;

Model class is still not found on Laravel 4

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

Resources