how to edit default artisan command laravel - php

when I run
php artisan make:request "TestRequest"
it will create file like below :
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class TestRequest extends FormRequest // i want to change from extends 'Form Request' to extends 'MyCustomFormRequest'
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
];
}
}
so as you can see above is the default one, I wanted to change extends class from 'FormRequest' (this is default) to MyCustomFormRequest (this is my custom)
so how do I achieve when I run
php artisan make:request "TestRequest"
, it will automatically extends 'MyCustomFormRequest' instead of 'FormRequest' ?

First ,you need to create a new command
php artisan make:command CustomRequestMakeCommand
Copy all code from Illuminate\Foundation\Console\RequestMakeCommand to App\Console\Commands\CustomRequestMakeCommand ( Remember to change the class ,namespace,and name command also)
Secondly ,create a new sub at console folder name like "stubs/customrequest.stub" ,copy all code from request.stub (vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/request.stub) to the new one ,change the FromRequest to YourCustomFormRequest
class DummyClass extends CustomFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
];
}
}
Then you can use your custom command ,you can read more about this at
https://laravel.com/docs/5.6/artisan

Related

Laravel component class ignore due to component create in different folder

<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Admin.-newclubform extends Component
{
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{echo "demo";
return view('components.admin.-newclubform');
}
}
I use php artisan make:component Admin.Newclubform
command to create component in Admin Folder.
views section working but class is ignored.
php artisan make:component Admin.Newclubform creates all class and view . class is generated by artisan command
As mentioned by #shaedrich Admin.Newclubform is not a valid class name.
So creating subfolder run command like below
php artisan make:component Admin/NewClubForm
This will create file inside
App\View\Components\Admin\NewClubForm
So your component look like this
<?php
namespace App\View\Components\Admin;
use Illuminate\View\Component;
class NewClubForm extends Component
{
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.admin.new-club-form');
}
}
then you can access like this
<x-admin.newclubform></x-admin.newclubform>

Custom Request for Form Validations

I'm facing a problem with custom request. I have created a request with php artisan make:request StoreNewClient.
I have configured the validations logic inside the new request file, like:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreNewClient extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [ ...
];
}
/**
* Get the error messages for the defined validation rules.
*
* #return array
*/
public function messages()
{
return [ ...
];
}
}
In the controller, I imported the file like use App\Http\Requests\StoreNewClient; and after, In the function store() I writed:
public function store(StoreNewClient $request)
{
// Will return only validated data
$validated = $request->validated();
...
}
That what I understood from the documentation, but this give me an error: Class App\Http\Requests\StoreNewClient does not exist but exists (!!).
I already tried to clear caches and dumped the composer but didn't solved the problem. Any help?
Fount the error. In the messages(), 1 line didn't had the comma at the end and neither the app neither the composer dump-autoload, gave any error.

Laravel 5.8 Custom validation error 'class does not exist'

I'm am new to laravel and i am trying to get my custom validation rules to work on my controller.
It's showing that the class does not exist.
ReflectionException thrown with message "Class App\Http\Controllers\StoreBooksRequest does not exist"
I made the request file using the artisan command.
lando artisan make:request StoreBooksRequest
this is my request file :
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreBooksRequest 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 [
//
'title' => 'required|unique:books|max:150',
'description' => 'required',
'isbn' => 'required|max:20'
];
}
}
and this is the controller where i am trying to get the custom request rules to work :
namespace App\Http\Controllers;
use App\Book;
use Illuminate\Http\Request;
class BooksController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$books = Book::all();
return view('books.index', compact('books'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('books.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(StoreBooksRequest $request)
{
$book = new Book();
$book->title = $request->title;
$book->description = $request->description;
$book->isbn = $request->isbn;
$book->save();
}
I think the problem is with the error saying that the request file is in the Controllers folder and not in the standard Requests folder.
You have not included the namespace of your custom request's class. Add use App\Http\Requests\StoreBooksRequest; after use Illuminate\Http\Request;
You seem to be using wrong namespace for your
Class App\Http\Controllers\StoreBooksRequest
Your namespace is set to namespace App\Http\Requests; while you are calling it from controller, If you move your Class to App\Http\Requests.
Also, don't forget to import the class in your controller
use StoreBooksRequest
When you execute the php artisan make:request Myrequestname, Laravel create the file inside the App\Http\Request subdirectory, so you need to be careful to use the right namespace, another thing you always had to be carefull is about the name you use, is not the same Mycontroller than mycontroller and is worst if your server is a Linux server, because the file system make differentiation beewteen Caps.

Trying to send email using command in laravel 5.4

I'm trying to send email using task scheduler in laravel 5.4 and below is code
I made a mail controller
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
# MAiler
use App\Mail\Mailtrap;
use App\Mail\EmailNotification;
class MailController extends Controller
{
/**
* Send email
* #return
*/
public function index(){
$user = Auth::user();
Mail::to($user)->send(new EmailNotification());
}
}
Next, I created a command and use the controller in there
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Http\Controllers\MailController;
class SendNotifications extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'SendNotifications:notification';
/**
* The console command description.
*
* #var string
*/
protected $description = 'This will send email';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$mail = new MailController();
$mail->index();
}
}
and in kernel
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'App\Console\Commands\SendNotifications'
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('SendNotifications:notification')
->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
when I try to send email in controller using route in web.php, it successfully send an email to mailtrap.io but, when i try to send it in the background using this command
php artisan SendNotification:notification
I got this error
Trying to get property of non-object
I don't know why it was supposed to be successful because it just called the email in the controller or I implemented this wrong
can you guide me with this?
Because you work on different sessions. You probably logged in on browser and created session for this user however this session is not same in your command line.
So your problem occurs in this line $user = Auth::user();
If you dd($user) on your console command you will notice $user is empty
and thats why you are getting try to get property of non object error

Laravel Throws ReflectionException Since Update to Version 5.1.7

I'd been working on my portfolio development with a blog alongside on Laravel 5.1.2 lately. It all was working fine till the moment I needed the illuminate/html package. Also, prior to running the composer require illuminate/html command, I ran the composer update command to let all the libraries update to their newer versions.
But that's where the problem crept in. Since, the upgrade to Laravel 5.1.7 and the installation of the illuminate/html package, the project has gone haywire and has been throwing barking mad
ReflectionException in Container.php line 736:
Class view does not exist
And I haven't the slightest clue where this is arising from.
PS: I have also updated the app.php file to include the respective providers and aliases corresponding to illuminate/html.
Update
Here's the code from my controller files if it might be of any help.
PagesController.php (I generated it as a plain stub via the --plain artisan option)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PagesController extends Controller
{
public function index()
{
return view('pages.home');
}
}
ArticleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ArticlesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$articles = Article::all();
return view('blog.index', compact('articles'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return view('blog.create');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$article = Article::findOrFail($id);
return view('blog.show', compact('article'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}

Categories