MethodNotAllowedHttpException in RouteCollection.php line 219 in Laravel - laravel

I have used illuminate/html in laravel to create a form and insert data to it.
this is my routes.php:
Route::post('/userlog', 'LogController#userlog');
this is my view:
#extends('layouts.master')
#section('content')
{!! Form::open(['url' => '/userlog']) !!}
<div class="form-group">
{!! Form::label('phone', 'User phone:') !!}
{!! Form::text('phone', null, ['class' => 'form-control input-sm']) !!}
</div>
{!! Form::close() !!}
#endsection
and this is the controller:
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Requests;
class LogController extends Controller
{
public function userlog(Request $request)
{
$input = $request::all();
print_r($input);die();
}
}
I face this error:
MethodNotAllowedHttpException in RouteCollection.php line 219:
What is the problem?

Related

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

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() !!}

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');
}

update user avatar with laravel5

From the look of it seems to me very logic but I am missing something, it doesn't work, nothing changes!
Here is my profile controller file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Image;
class ProfileController extends Controller
{
public function profile()
{
$user = Auth::user();
return view('profile')->with('user', $user);
}
public function edit()
{
$user = Auth::user();
return view('edit')->with('user', $user);
}
public function update(Request $request)
{
if($request->hasFile('avatar'))
{
$avatar = $request->file('avatar');
$filename = time().'.'.$avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/users_avatars/'.$filename));
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return redirect('profile')->with('user', Auth::user());
}
}
and here is my edit.blade.php
#extends('layouts.app')
#section('content')
<div class="col-md-6">
{!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController#update', 'file'=>'true']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email') !!}
{!! Form::email('email', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('number', 'Phone') !!}
{!! Form::text('number', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group col-md-5">
{!! Form::label('avatar', 'Avatar') !!}
{!! Form::file('avatar', ['class'=>'form-control']) !!}
</div><br><br><br><br>
<div class="form-group">
{!! Form::submit('Update', null, ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
but when I edit the user it doesn't change..plz help
It seems like you have made a mistake while using Form from Laravel HTML Collective. You should use "files" => true instead of "file" => true
{!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController#update', 'file'=>'true']) !!}

I can't store category_id on table

I cannot get category_id when I created a topic. Here are my codes and mysql. I think I have a problem in store method on forum controller.
mysql pro screenshot
Topic create page
#extends('app')
#section('content')
<h1>トピック</h1>
<hr>
{!! Form::open(['url' => 'forums']) !!}
<div class="form-group">
{!! Form::label('title', 'タイトル:') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body', '本文:') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('category', 'カテゴリー:') !!}
{!! Form::select('category', $categories,null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('送信',['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#stop
forums controller
<?php
namespace App\Http\Controllers;
use App\Category;
use App\User;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Topic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ForumsController extends Controller
{
public function index()
{
$categories = Category::all();
$topics = Topic::latest()->get();
return view('forums.index',compact('categories','topics'));
}
public function create()
{
$categories = Category::lists('title', 'id');
return view('forums.create', compact('categories'));
}
public function store(Request $request)
{
Auth::user()->topics()->save(new Topic($request->all()));
// flash()->success('投稿しました','success');
// return redirect('forums');
}
}
Category model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = [
'title'
];
public function topics()
{
return $this->hasMany('App\Topic');
}
}
Topic model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class topic extends Model
{
protected $fillable = [
'title',
'body'
];
public function category()
{
return $this->belongsTo('App\category');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
Add category_id to the topic's Model fillable array like this
protected $fillable = [
'title',
'body',
'category_id'
];

Resources