Laravel 8 with laravelcollective/html 6.2, Action Controller#function not defined - laravel

I am new to Laravel and Stackoverflow
When Laravel7 is released, I started to learn Laravel.
The new version 8.0 was introduced not long ago and I started to try it.
I cannot define that the problem is caused by a newer version of Laravelor any misconfiguration.
When I try the following (edit.blade.php)
{!! Form::open(['action' => ['ProductController#update', $product->id], 'method' => 'POST']) !!}
or
{!! Form::open(['action' => [[ProductController::class, 'update'], $product->id], 'method' => 'POST']) !!}
an error occurred
Action ProductController#update not defined
then I tried to replace the controller name with a path like
{!! Form::open(['action' => ['App\Http\Controllers\ProductController#update', $product->id], 'method' => 'POST']) !!}
or
{!! Form::open(['action' => [[App\Http\Controllers\ProductController::class, 'update'], $product->id], 'method' => 'POST']) !!}
It works!
so I think this is about namespace
but I have a namespace heading
namespace App\Http\Controllers; in my App\Http\ProductController.php
although I can solve the problem by typing the full path of the controller in the collective form,
I am worried that my code has configuration error or syntax errors, etc.

This is a change to Laravel 8. There is no namespace prefix applied to your routes by default and there is no namespace prefix used when generating URLs to "actions".
To have the namespace prefixed to the Controller you are trying to reference when generating URLs to actions you would need to define the $namespace property on your App\Providers\RouteServiceProvider:
protected $namespace = 'App\Http\Controllers';
Now you can reference your action with that namespace prefix:
Form::open(['action' => ['ProductController#update', $product->id], 'method' => 'POST'])

In Laravel 8: use this code I think your problem will be solved
web.php
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
Route::get('create', 'Product\ProductController#Create')->name('product.create')->middleware('can:Add Product');
Route::post('create', 'Product\ProductController#Store')->name('product.store')->middleware('can:Add Product');
});
resources/views/product:
<form method="post" action="{{ route('product.store') }}" enctype="multipart/form-data">
#csrf
{{-- START - PRODUCT INFORMATION --}}
{{-- END- PRODUCT INFORMATION --}}
</form>
app/Http/Controllers/Products:
<?php
namespace App\Http\Controllers\Products;
use App\Http\Requests\Product\ProductRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ProductController extends Controller {
//---Constructor Function
public function __construct() {
$this->middleware('auth:sanctum');
}//---End of Function Constructor
//---Create Product
public function create() {
return view('product.product_add');
}//---End of Function create
//---Store Product in Database
public function store(ProductRequest $request) {
//---Add Product Details in DB
}
?>

Laravel 8 : Collective form - This is using to create a Laravel view page
{!! Form::open(['route' => '', 'id'=>'form','class' => 'needs-validation',]) !!}
<div class="form-group row">
{!! Form::label('employee_id', 'Employee ID:', ['class'=>'col-md-1 col-form-label custom_required']) !!}
<!-- Employee ID -->
<div class="form-group row">
{!! Form::label('employee_name', 'Employee Name:', ['class'=>'col-md-1 col-form-label custom_required']) !!}
<div class="col-lg-8">
{!! Form::text('employee_name', #$employee_id, ['class' => 'form-control', 'required', 'placeholder' => 'Employee Name', 'pattern'=> '^[a-z A-Z0-9_.-]*$']) !!}
</div>
<!-- Employee number -->
<div class="form-group row">
{!! Form::label('phone_number', 'Number:', ['class'=>'col-md-1 col-form-label custom_required']) !!}
<div class="col-lg-8">
{!! Form::text('phone_number', #$phone_number, ['class' => 'form-control', 'required','placeholder' => 'number']) !!}
</div>
{!! Form::close() !!}

Related

How to use $_GET variable from AJAX - LARAVEL

I need to use the $_GET variable into the blade.
The AJAX code work good, i received an alert with the correct value but i can't use it into the blade.
Route:
/*
|--------------------------------------------------------------------------
| Routes per gestire la pagina "Home"
|--------------------------------------------------------------------------
*/
Route::get('/home', 'HomeController#index');
Route::get('/home', array('as' => 'success', 'uses' => 'StatisticheController#totaleRichiesteAnnue'));
home.blade.php
#if(isset($_GET['tipologia_evento_id']))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
<script>
var tipologia_evento_id = event.tipologia_evento_id;
$.ajax({
type: "GET",
url: '/home',
data: { tipologia_evento_id: tipologia_evento_id},
success: function(msg)
{
alert(tipologia_evento_id);
$('#modal-event').modal('show');
}
});
</script>
In laravel 5 request() helper is available globaly
#if(request()->tipologia_evento_id)
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
request()->tipologia_evento_id is not set it will return null
Hope this helps
You can use the Request facade:
Request::get('tipologia_evento_id');
More details on: https://laravel.com/docs/5.6/requests
You should try below code:
#if(app('request')->input('tipologia_evento_id'))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
OR
#if(Request::get('tipologia_evento_id'))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
As you already have route and controller, why not pass it from controller and use it in view?
class HomeController extends Controller
{
public function index(Request $request)
{
$getData = $request->all();
return view('home', compact('getData'));
}
}
in your view you can easily access as $getData
So your home.blad.php will look something like this
#if($getData->tipologia_evento_id)
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
EDIT:
If you want to get json response using ajax
class HomeController extends Controller
{
public function index(Request $request)
{
$getData = $request->all();
return response()->json($getData);
}
}

laravel form is not working even after every particular step

In composer.json
require {
"laravelcollective/html": "^5.5"
}
{!! Form::open(['route' => 'blogs.store']) !!}
<div class="col-md-6">
<div class="form-group">
{!! Form::label('title', 'Blog Title') !!}
{!! Form::text('title', null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body', 'Blog Body') !!}
{!! Form::textarea('body', null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add Blog', ['class'=>'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
In Controller
public function store(BlogRequest $request)
{
$input = Request::all();
Blog::create($input);
return redirect(blogs);
}
provider in app
Collective\Html\HtmlServiceProvider::class,
aliases in app
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
form not working apart after submitting it show value in address bar this is what it shows
http://localhost/lynda/blogs/create?_token=dIQXrWadbNNJhCBMUYjUAAOM1MPXDmhD782rlJ0F&title=aaaaa&body=aaaa
Please add a method to your form. If you don't have method, it will use get method.
{!! Form::open(['method' => 'post', 'route' => 'blogs.store']) !!}
You should try this:
1) First run composer update for update your html package after run composer dump-autoload in terminal/cmd
2) Please clear the cache and run this command in terminal/cmd
php artisan config:cache
php artisan cache:clear
3)
public function store(BlogRequest $request)
{
$input = $request->all();
Blog::create($input);
return redirect('blogs');
}

Form model binding laravel 5.1 for multiple models

I want Form model binding for multiple objects in laracollective's Form package?
Something as following?
Form::model([$user,$vendors], array('route' => array('user.update', $user->id)))
Where can I request this feature?
I assume you're using Laravel-Collective, Unfortunately you cant do something like that. instead you can try something like this :
UPDATE
you can query all your model in your controller and combine them like this :
$user = User::where('id',$user_id)->get();
$vendor = Vendor::where('user_id',$user_id)->get();
//merge two model
$user = $user->merge($vendor);
// return $user;
return view('admin.users.edit', compact('user'))
->withTitle('Edit user');
and in your form call them like this :
{!! Form::model($user[1], ['route' => ['admin.users.update', $user],'method'=>'PUT']) !!}
#include('admin.users._formEdit')
<div>
{!! Form::submit('Save user', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
_formEdit.blade.php
<div class="form-group">
{!! Form::label('first_name', 'First Name : ') !!}
{!! Form::text('user[first_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name', 'Last Name : ') !!}
{!! Form::text('user[last_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('email', 'Email : ') !!}
{!! Form::email('user[email]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('vendor_name', 'vendor_name') !!}
{!! Form::text('vendor_name', null,['class' => 'form-control']) !!}
</div>
OR ANOTHER SOLUTION
create relation between model of your User and Vendor (one-to-one or one-to-many) example
User :
public function vendor(){
return $this->hasOne('App\Vendor','user_id');
}
Vendor:
public function user(){
return $this->belongsTo('App\User','user_id);
}
Build your response query like this :
$user = Vendor::with('user')->find($user_id);
and then in your view template :
{!! Form::model($user, ...) !!}
Vendor: {!! Form::text('vendor_name') !!}
User: {{ Form::text('user[username]') }}
{!! Form::close() !!}

Form model binding not working in laravel 5.2 using laravelcollective

Hello I'm new to laravel 5.2 and going through some lessons.
For some reason form model binding is not working for me.
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
I received data in $post because I'm using a workaround like this:
{!! Form::text('title', "$post->title" ,['class'=> 'form-control']) !!}
And that is showing my data.
Controller:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller{
public function update(Request $request, $id){
$post =Post::findOrfail($id);
$post->update($request->all());
return redirect('/posts');
}
}
create.blade.php view:
#section('content')
<h1>Create Post</h1>
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit('Create Post', ['class'=> 'btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection
You shouldn't be quoting the value for null in your input:
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
should be
{!! Form::text('title', null, ['class'=> 'form-control']) !!}
Ok, try adding the body of the form into a partial called posts/partials/form.blade.phpand include it between the form open / model and form close tags.
Example:
posts/partials/form.blade.php
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit($formButtonText, ['class'=> 'btn-primary form-control']) !!}
</div>
posts/create.blade.php
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Create Post'
])
{!! Form::close() !!}
posts/edit.blade.php
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Update Post'
])
{!! Form::close() !!}

Method is not allowed

I am trying to create simple form with post. But when i submit my form i get this error - MethodNotAllowedHttpException in RouteCollection.php line 219:
My route file:
Route::get('articles', 'ArticlesController#index');
Route::get('articles/create', 'ArticlesController#create');
Route::get('articles/{id}', 'ArticlesController#show');
Route::post('articles', 'ArticlesController#store');
Form:
{!! Form::open(['url' => 'articles', 'method' => 'post']) !!}
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body', 'Body:') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add article', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
Controller class:
public function store(Request $request) {
$input = $request->all();
return $input;
}
Thanks for your attention. I dont get where is the problem.
Found answer. Just type php artisan route:clear in terminal.
You have the same url both in Route::get('articles', 'ArticlesController#index'); and Route::post('articles', 'ArticlesController#store');
Use action() instead of url can solve this problem. EX:
{!! Form::open(['action' => 'ArticlesController#store', 'method' => 'post']) !!}
Change the route: Route::get('articles', 'ArticlesController#index'); to Route::post('articles', 'ArticlesController#index');

Resources