laravel form is not working even after every particular step - laravel

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

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

laravelcollective/html not finding any of my controllers

I am getting this error from a login page
Action App\Http\Controllers\Admin\LoginController#authenticate not defined. (View: /Applications/XAMPP/xamppfiles/htdocs/lectureme/resources/views/admin/login.blade.php)
I know for certain that this controller exists though, so why is it unable to find it? I have also created a new controller in the controller root and named it TestController, and tried routing to that instead, but that was also apparently not found.
Any suggestions for how to get access to the controller? Form code:
{!! Form::open(['action' => 'LoginController#authenticate']) !!}
<div class="form-group">
<div class="form-group">
{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email Address:') !!}
{!! Form::text('email', null, ['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::submit('Login', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
I have also tried composer dump-autoload and php artisan cache:clear
Make sure you namespaced your controller properly. Assuming you have placed your controller in the App\Http\Controllers\Admin directory it would be:
namespace App\Http\Controllers\Admin

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

Error when upload file - laravel 5

Controller:
public function store(StoreSongRequest $request)
{
dd($request->get('song'));die;
}
create.blade.php
{!! Form::open(array('route' => 'songs.store', 'class' => 'form', 'novalidate' => 'novalidate', 'files' => true)) !!}
<div class="form-group">
{!! Form::label('Name') !!}
{!! Form::text('name') !!}
</div>
<div class="form-group">
{!! Form::label('Upload') !!}
{!! Form::file('song') !!}
</div>
<div class="form-group">
{!! Form::submit('Upload', array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
When i submit i appear error:
TokenMismatchException in VerifyCsrfToken.php line 46:
I was comment out 'app\Http\Middleware\VerifyCsrfToken' in kernel.php file but still the same.
The issue is that post_max_size was low $_POST. Increasing post_max_size in php.ini solved the issue.

Resources