Class App\Http\Requests\TestRequest does not exist? But I am using this class in Controller - laravel

I am using same App\Http\Requests\TestRequest in controller but its display message this Class not exit what can be problem? Plz help me?
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\TestRequest;
use App\Student;
class InsertController extends Controller
{
public function insert(TestRequest $request){
$obj = new Student();
$obj->Name =$request->name;
$obj->City = $request->city;
$obj->save();
return view('submit');
}
}
TestRequest Code is Here!
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class TestRequest 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 [
'Name'=>'required'
'City'=>'required'
];
}
}
Above Both codes are of Request folder And controller folder, I have done everything, i have done this class in Controller but its showing that not exist why? please Help me.

Related

How to Get Model Dynamic in Controller Using Laravel?

I'm trying to get model name on session. Using session I'm try too get model dynamic. But it is not work
My Code
<?php
namespace App\Exports;
use App\Http\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
class Export implements WithHeadings
{
/**
* #return \Illuminate\Support\Collection
*/
public $table="";
public function headings(): array
{
$model="App\Http\Models\\".Session::get('tableName');
$this->table = new $model;
}
}

How can I mock static method of class using mockery?

I have this class:
<?php
namespace App\Http\Controllers\Alerts;
use App\Http\Controllers\Controller;
use App\Models\Alert;
use App\Models\FiredAlert;
use App\Models\UnitReport;
use App\Notifications\AlertSetup;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
class AlertController extends Controller
{
/**
* #param Alert $alert
* #param null $schema
*/
public static function checkForInactivity(Alert $alert, $schema = null): void
{
...some code...
}
/**
* Get int day of the week
* #return int
*/
public static function getDayOfTheWeek(): int
{
return Carbon::now()->dayOfWeek;
}
/**
* Get all alerts available fot the current day and process it
*/
public static function checkForInactivityAlerts(): void
{
$alerts = Alert::where('day', self::getDayOfTheWeek())->get());
foreach ($alerts as $alert) {
self::checkForInactivity($alert);
}
}
}
I would like to mock getDayOfTheWeek and checkForInactivity and call checkForInactivityAlerts, something like:
$mockAlertController = Mockery::mock("alias:App\Http\Controllers\Alerts\AlertController");
$mockAlertController->shouldReceive('getDayOfTheWeek')->andReturn(2);
$mockAlertController->shouldReceive('checkForInactivity')->once();
AlertController::checkForInactivityAlerts()
But I got the error:
Mockery\Exception\BadMethodCallException : Static method
App\Http\Controllers\Alerts\AlertController::checkForInactivityAlerts()
does not exist on this mock object
How can I solve this?

Can an Eloquent model has multiple Observer?

Hi I want write a trait to add an observer to model but I thought write boot method is not the right way and finnaly i find that i can boot trait like boot[TraitName] but i wonder if i add an observer with code like this:
trait CreateObserver
{
public static function bootCreateObserver()
{
static::creating(function (Model $model) {
// ...
});
}
}
can I add another observer for my model like below or it will overriding my trait observer?
class MyModel extends Model
{
use CreateObserver;
public static function boot()
{
static::creating(function ($model) {
// ...
});
}
...
}
That's not the right way. I think this might help you:
https://laravel.com/docs/5.6/eloquent#observers
You bind observers to your models using a service boot:
<?php
namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
User::observe(UserObserver::class);
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
//
}
}
Inside the observer you can add all desired functionality:
<?php
namespace App\Observers;
use App\User;
class UserObserver
{
/**
* Listen to the User created event.
*
* #param \App\User $user
* #return void
*/
public function created(User $user)
{
//
}
/**
* Listen to the User deleting event.
*
* #param \App\User $user
* #return void
*/
public function deleting(User $user)
{
//
}
}
And to elaborate. Yes it can have multiple observers. Although I never seen a useful situation for that:
public function boot()
{
User::observe(UserObserver::class);
User::observe(AuthenticableModelsObserver::class);
}
This way both the UserObserver() and AuthenticableModelsObserver() are binded to the User() model on boot.

Laravel 5.2 Class 'App\authors' not found

Error is:
FatalErrorException in AuthorsController.php line 28: Class
'App\authors' not found
Line 28:
<?php
namespace App\Http\Controllers;
use App\authors;
use App\Http\Requests;
use Illuminate\Http\Request;
class AuthorsController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function show()
{
$authors = authors::all(); LINE 28
return view('authors', ['authors'=> $authors]);
}
}
Authors class:
<?php
namespace App;
use App;
use Illuminate\Database\Eloquent\Model;
class authors extends Model
{
protected $table = 'authors';
}
I don t get it where the mistake is... I am new to Laravel.
Try App\Author;
Check your spelling errors. Hope you get fixed

ReflectionException in CommanderTrait.php line 59: Class App\FollowUserCommand does not exist

I Was following Laracasts video for creating follow option but when I'm clicking on Follow button on members page it is showing the above error. This is my followcontroller
<?php
namespace App\Http\Controllers;
use App\User;
use Laracasts\Commander\CommanderTrait;
use App\FollowUserCommand;
use Sentinel;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class FollowsController extends Controller
{
use CommanderTrait;
/**
* Follow a User
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store()
{
$input = array_add(Input::all(), 'user_id', Sentinel::getuser()->id);
$this->execute(FollowUserCommand::class, $input);
return Redirect::back();
}
/**
* Unfollow a User
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
This is my FollowUserCommand
<?php namespace App\User;
class FollowUserCommand {
public $user_id;
public $userIdToFollow;
function __construct($user_id, $userIdToFollow)
{
$this->user_id = $user_id;
$this->userIdToFollow = $userIdToFollow;
}
}
FollowUserCommandHandler
<?php namespace App;
use Laracasts\Commander\CommandHandler;
class FollowUserCommandHandler implements CommandHandler {
protected $userRepo;
function __construct(UserRepository $userRepo)
{
$this->userRepo = $userRepo;
}
public function handle($command)
{
$user = $this->userRepo->findById($command->user_id);
$this->userRepo->follow($command->userIdToFollow, $user);
return $user;
}
}
UserRepository
class UserRepository {
public function save(User $user)
{
return $user->save();
}
public function getPaginated($howMany = 4)
{
return User::orderBy('first_name', 'asc')->paginate($howMany);
}
public function findByUsername($username)
{
return User::with(['feeds' => function($query)
{
$query->latest();
}
])->whereUsername($username)->first();
}
public function findById($id)
{
return User::findOrFail($id);
}
public function follow($userIdToFollow, User $user)
{
return $user->follows()->attach($userIdToFollow);
}
}
User.php
<?php namespace App;
use Cartalyst\Sentinel\Users\EloquentUser;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends EloquentUser {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes to be fillable from the model.
*
* A dirty hack to allow fields to be fillable by calling empty fillable array
*
* #var array
*/
protected $fillable = [];
protected $guarded = ['id'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* To allow soft deletes
*/
use SoftDeletes;
protected $dates = ['deleted_at'];
// This function allows us to get a list of users following us
public function follows()
{
return $this->belongsToMany(self::class, 'follows', 'follower_id', 'followed_id')->withTimestamps();
}
// Get all users we are following
public function following()
{
return $this->belongsToMany('User', 'followers', 'user_id', 'follow_id')->withTimestamps();
}
}
Can anyone tell me why it is showing error even after "use App\FollowUserCommand;" has been declared in namespace.
Your namespace when declaring the FollowUserCommand class is wrong, it should be:
<?php namespace App;
class FollowUserCommand {...
And right now you have <?php namespace App\User;.

Resources