How to use controller methods in laravel 8 with collective html? - php

This is code from one of my views, create.blade.php
Error: Action PetsController#store not defined.
I think this has something to do with Laravel 8 different way to call controller methods.
The controller is PetsController and it was created to be a CRUD (php artisan make:controller PetsController --resource)
using php artisan route:list it says that i should use "App\Http\Controllers\PetsController#store" in 'action', but isn't there a better way to do this?
#extends('layouts.app')
#section('content')
{!! Form::open([
'method'=>'post', 'action'=>'PetsController#store'
]) !!}
#endsection

Related

Laravel 8.15.0/Jetstream - How to register new blades x-jet-newblade?

I am just doing my very first steps with Laravel 8 and found a problem that I can not solve.
/var/www/html/laravel/resources/views/dashboard.blade.php:
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<x-jet-welcome />
</div>
If i create a new blade in the same directory (f.e. the form.blade.php) with the same code as above but with <x-jet-subform/> instead of <x-jet-welcome> it should normally redirect to the subform.blade.php which is located under var/www/html/laravel/resources/views/vendor/jetstream/components/subform.blade.php
But if I try to get to that page (after setting a Route at web.php) it says
InvalidArgumentException
Unable to locate a class or view for component [jet-subform].
So I think it's necessary to "register" new blades but I found no way to do that...
The view is already published with
php artisan vendor:publish --tag=jetstream-views
You can register your jetstream blade components in App\Providers\JetstreamServiceProvider.php located in app\Providers folder.
Add the following helper function to the file:
protected function registerComponent(string $component) {
\Illuminate\Support\Facades\Blade::component('jetstream::components.'.$component, 'jet-'.$component);
}
Then use the following snippet in register function to register your jetstream blade components:
public function register() {
$this->registerComponent('subform');
}
Now you can use your custom jetstream component:
<x-jet-subform>
I was dealing with the same problem here and found your question unanswered.
The solution I found was to create my own new Blade component.
You can do that using:
$ php artisan make:component MyComponent
This will create two new files /resources/views/components/my-component.blade.php and /app/View/Components/MyComponent.php.
Now you just need to build your component on that blade file and reference it using the x-tag like this:
<x-my-component></x-my-component>
This is how the blade component code should look like
<button {{ $attributes->merge(['type' => 'button', 'class' => 'some-classes']) }}> {{ $slot }} </button>
Hope it helps. Greetings from Brazil :)
I am not sure whether it's the correct or intended way to add new custom x-jet components here as this method may not survive an update, but you can register new components in this file:
vendor/laravel/jetstream/src/JetstreamServiceProvider.php.
Add, $this->registerComponent('subform'); to the configureComponents method, and then call it with an <x-jet-subform> tag

Error Message "GET method is not supported for this route. Supported methods: POST"

When I was click submit button I got an error message .
The GET method is not supported for this route. Supported methods:
POST
I have try this method, but still error.
php artisan route:clear
php artisan view:clear
This my code
<form action="{{ route('pemesanan.process') }}" method="post">
{{ csrf_field() }}
...
...
</form>
For route
Route::post('pemesanan/process', ['as' => 'pemesanan.process', 'uses' => 'PemesananController#process'])->middleware('auth');
your route name is "pemesanan.calculate" but you are using "pemesanan.process" in your form
change your form to solve your problem

Laravel blade "{{ command }}" not working correctly

I have this weird problem with my Laravel 5.5 version... I created the auth views using
php artisan make:auth
This command created the views controllers and everything I need to lets get stared to work. But I'm having this visualization problem
As you can see on the register view I have this problem.
The real thing is that "{{ any_command }}" is printing the code that its supose to generate instead of interprating like part of the code. But if I use {!! any_command !!} instead it seems to work propertly. What can happend to my laravel is screwed up. It has nothing to be with the artisan auth method, because I tried to create a new form (using laravel collective form helper) and get the same result.
{{ }} will escape all data before printing. So, if you write any HTML tag inside, it will be escaped and printed as is. Just like your example.
{!! !!} will print unescaped data. This will print the tags correctly, but you have to take care where you use it, because someone could inject unwanted data there.
So, in your case, you should use {!! !!}.
Please, refer to this question: What is the difference between {{ }} and {!! !!} in laravel blade files?

Laravel 5 GET inside controller?

I use Laravel5 and i cant understand why postProcess or getProcess not works?
Example::
html page:
{{ Form::open(array('url' => 'portfolio/process')) }}
{{ Form::submit() }}
{{ Form::close() }}
route:
Route::resource('portfolio','PortfolioController');
controller:
public function postProcess (){
return 'Text!';
}
Every time i get error:
MethodNotAllowedHttpException in RouteCollection.php line 218:
It doesn't work because Route::resource doesn't build these routes and you need to explicitly define them:
Route::post('portfolio/process', 'PortfolioController#postProcess');
I think you need to check your route list:
Run this command php artisan route:list in terminal and check your route.
Hope this work for you!

Routing in laravel 5 with resource

I've a route:
Route::resource('partner/register', 'PartnerController\Register');
And function index, works. But when I try route to store with:
{!! Form::open(['route'=>'partner/register.store']) !!}
Error:
Route [partner/register.store] not defined.
Why? help please.
PD: I used artisan make:controller PartnerController/Register to create the controller. Because need of create controllers in differents directories
This part is fine I guess as you said you needed it in different directories
Route::resource('partner/register', 'PartnerController\Register');
But to get the store route use
{!! Form::open(['url' => route('partner.register.store')]) !!}
Btw you can see all the current routes with php artisan route:list

Categories