<?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>
Related
I'm using a package with cities and countries in my Laravel project. I set up a repository pattern to use this cities data. My plan is to send the cities data to the register view or any other view I want. In the example here I want to send to the project.create page. I tested the repository and when I look through the controller, I can pass the data to the view and print it with dd(). There is no problem with this. Now I want to send this data to a view i want via viewcomposer.
First of all, I wrote a ComposerServiceProvider.php file as follows
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
View::composer(
'project.create',
'App\View\Composers\LocationComposer'
);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Then I wrote the composer file, which is as follows:
<?php
namespace App\View\Composers;
use App\Repository\Eloquent\LocationRepository;
use Illuminate\View\View;
class LocationComposer
{
/**
* #var LocationRepository
*/
public $locationlist;
/**
* LocationComposer constructor.
* #param LocationRepository $locations
* #return void
*/
public function __construct(LocationRepository $locations)
{
$this->locationlist = $locations->getAllCities();
}
/**
* Bind data to the view.
*
* #param \Illuminate\View\View $view
* #return void
*/
public function compose(View $view)
{
$view->with('cities', $this->locationlist);
}
}
But the data does not pass to the view I want.
this is the error i got
Things I've tried and done:
-I registered the composerserviceprovider via app config.
-I ran php artisan config:clear
-I tried to send to other views Welcome, register etc.
I suspect I am not invoking the repository correctly into composer.
Thanks for your help...
Probabaly you didn't add App\Providers\ComposerServiceProvider::class into your config/app.php file. All additional service providers need to be added into providers array of config/app.php file.
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
Symfony 3.4.
I have annotations for my controller:
/**
*
* #Route("/{prefix}", requirements={"prefix":"daily_task|event"})
*/
class TaskController extends Controller
and want to access current {prefix} value directly from controller's methods (which aren't routed actions). How to get it's value?
Finally: $this->get('request_stack')->getCurrentRequest()->get('prefix')
Symfony will pass you the variables automatically if you use them as function parameters, like this:
/**
*
* #Route("/{prefix}", requirements={"prefix":"daily_task|event"})
*/
class TaskController extends Controller {
/**
* #Route("/{_locale}/some/path", name="_some_route_name")
*/
public function actualAction($prefix, $_locale) { /* ... */ }
}
Alternatively you can use the whole request like this:
/**
*
* #Route("/{prefix}", requirements={"prefix":"daily_task|event"})
*/
class TaskController extends Controller {
/**
* #Route("/{_locale}/some/path", name="_some_route_name")
*/
public function actualAction(Request $request) {
$prefix = $request->get('prefix');
}
}
I am using Laravel 5.2. I got the following error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class '\App\Providers\Weixin\WeixinApiProvider' not found
The program runs fine if I move the class file from app/Providers/MyProviders to app/Providers and change the namespace from App\Providers\MyProviders to App\Providers.
But why can't i create the file under app/Providers/MyProviders/ and set the namespace accordingly? Any help?
this is my class file like:
<?php
namespace App\Providers\Weixin;
use Illuminate\Support\ServiceProvider;
class WeixinApiProvider extends ServiceProvider{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->singleton('WeixinApi',function(){
return new \App\Services\WeixinApi();
});
}
}
For the background, I'm a laravel 5 newbie, but I did use laravel 4 in my past projects.
In laravel 4 I just call the model straight in the artisan command but now, how do I call a model inside an artisan command. here's my current code:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\sampleModel as sampleModel;
class testCommand extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'test';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
sampleModel::sampleMethod();
}
}
Your issue isn't related to the command, you can use Model in commands.
Issue is in your Model, add this to the beginning of the Model:
use DB;
Note 1, Laravel models should be named with first capital: SampleModel, not sampleModel;
Note 2, as sampleModel is excess as model already named as sampleModel.