Why i have POST method if everywhere is GET method? - laravel

I want make search by parameters. But it shows i have mixing GET and POST methods. (Error message: MethodNotAllowedHttpException
No message). Blade form by default have POST. i changed to GET. Route have GET method. Maybe you can see what i am doing wrong. This is my VIEW:
{!! Form::open([ 'action' => ['HomePageController#index', 'method' => 'get']]) !!}
<div class="container">
<div class="col-xs-2 form-inline">
{!! Form::label('city_id', trans('quickadmin.companies.fields.city').'', ['class' => 'control-label']) !!}
{!! Form::select('city_id', $cities, old('city_id'), ['class' => 'form-control select2') !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('categories', trans('quickadmin.companies.fields.categories').'', ['class' => 'control-label']) !!}
{!! Form::select('categories', $categories, old('categories'), ['class' => 'form-control select2']) !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('search', trans('quickadmin.companies.fields.name').'', ['class' => 'control-label']) !!}
{!! Form::text('search', old('search'), ['class' => 'form-control', 'placeholder' => 'Search']) !!}
</div>
<div class="form-inline">
<div class="col-xs-2">
<button type="submit"
class="btn btn-primary">
Search
</button>
</div>
</div>
</div>
{!! Form::close() !!}
My controller:
public function index( Request $request)
{
$cities = \App\City::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$categories = \App\Category::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$name = $request->input('city_id');
$companies = \App\Company::All()->where('city_id', '=', $name);
return view('table', compact('companies', $companies, 'cities', $cities, 'categories', $categories));
My route:
Route::get('/', 'HomePageController#index');
Thank you for your help.

There is a problem in the form open, try it like this :
{!! Form::open([ 'action' => 'HomePageController#index', 'method' => 'get']) !!}

Related

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

htmlentities error when passing from a modal

New to Laravel, please bare with.
Error:
htmlentities() expects parameter 1 to be string, object given (View: /var/www/html/willow/resources/views/emails/valuation.blade.php)
The modal from which it is being sent:
{!! Form::open(['action' => ['EnquiryController#valuationRequest']]) !!}
<div class="form-group">
{!! Form::text('name', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Name']) !!}
</div>
<div class="form-group">
{!! Form::text('email', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Email Address']) !!}
</div>
<div class="form-group">
{!! Form::text('telephone', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Telephone Number']) !!}
</div>
<div class="form-group">
{!! Form::text('house_number', null, ['class' => 'form-control has-feedback', 'placeholder' => 'House name / number']) !!}
</div>
<div class="form-group">
{!! Form::text('postcode', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Postcode']) !!}
</div>
<div class="form-group">
{!! Form::textarea('message', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Message', 'rows' => '5']) !!}
</div>
<div class="form-group">
<input type="submit" class="button black" value="Register">
</div>
{!! Form::close() !!}
and the function:
public function valuationRequest(ValuationRequest $request)
{
// dd($request->all());
Mail::send('emails.valuation',
['name' => $request['name'],
'email' => $request['email'],
'telephone' => $request['telephone'],
'house_number' => $request['house_number'],
'postcode' => $request['postcode'],
'message' => $request['message'],
],
function ($message) use ($request) {
$message->to('paolo#bigg.co.uk', 'Paolo Resteghini')->subject('Valuation Request - Willow Lettings');
});
Session::flash('flash_message', 'Your request has been sent.');
return redirect(URL::previous());
}
The contents of the DD are perfect. All of the requests are populated as expected, but when trying to go through the rest of the function it fails with the error above.
emails.valuation:
Hello, <br><br>
You have received a new valuation request via the Willow Lettings website. Here they are: <br><br>
<b>Name:</b> {{ $name }}<br>
<b>Email:</b> {{ $email }}<br>
<b>Phone:</b> {{ $telephone }}<br>
<b>House number:</b> {{ $house_number }}<br><br>
<b>Postcode:</b> {{ $postcode }}<br><br>
{{ $message }}
Most likely, this is a problem with your message variable. As you can see from the docs:
Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, you should avoid passing a message variable in your view payload.
In other words, you should change message into something else like msg.
'msg' => $request['message'],
Then, in your blade file, reflect that change:
{{ $msg }}

Update data with Laravel Collective forms

I have an edit form with Laravel Collective but when clicking the button, the data do not update. Below are my codes.
Form:
{!! Form::model($post, ['route' => ['/post/update/', $post->id]]) !!}
{{ method_field('PATCH') }}
<div class="form-group">
<div class="row">
<div class="col-lg-6">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="col-lg-6">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'rows' => 10]) !!}
</div>
<hr/>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
</div>
{!! Form::close() !!}
Controller:
public function edit($id)
{
return \View::make('admin/post/edit')->with([
'post' => \DB::table('posts')->find($id),
'categories' => \App\Category::lists('category', 'id')
]);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return \Redirect::to('/admin/posts');
}
Routes:
Route::get('/admin/post/edit/{id}', 'Admin\PostController#edit');
Route::patch('/post/update/', [
'as' => '/post/update/',
'uses' => 'Admin\PostController#update'
]);
It's a bit different from the Laracast, and it's confusing me. Framework is new to me and the lack of code to do something is confusing.
I solved it. Mass Assignment. explains what to do if using update or create
So, the update method is:
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->category_id = $request->category_id;
$post->content = $request->content;
$post->save();
return \Redirect::to('/admin/posts');
}

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

Including Sub-Views in Blade

I'm trying to include two sub-views ( 'login' and 'register' ) in the 'home' view which is like this:
#extends('master')
#section('content')
#include('auth.login')
<hr>
#include('auth.register')
#endsection
And the 'login' and 'register' views:
//register.blade.php
#extends('master')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email', 'Email Address') !!}
{!! Form::email('email', null,
['class' => 'form-control',
'placeholder' => 'Email Address',
'required' => true]) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password',
['class' => 'form-control',
'placeholder' => 'Password',
'required' => true]) !!}
</div>
<div class="checkbox">
<label>
{!! Form::checkbox('remember', null, []) !!}
Remeber Me
</label>
</div>
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
#endsection
//login.blade.php
#extends('master')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email', 'Email Address') !!}
{!! Form::email('email', null,
['class' => 'form-control',
'placeholder' => 'Email Address',
'required' => true]) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password',
['class' => 'form-control',
'placeholder' => 'Password',
'required' => true]) !!}
</div>
<div class="checkbox">
<label>
{!! Form::checkbox('remember', null, []) !!}
Remeber Me
</label>
</div>
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
#endsection
I've tried removing the master extension from the sub-views but it didn't work. Only one sub-view is being rendered. I can't figure out, why that's happening?
main.blade.php
#extends('master')
#section('content')
#include('auth.login')
<hr>
#include('auth.register')
#endsection
For the sub views you have to remove the #extends('master'). Then you have two options I can think of.
You can add the #parent directive.
register.blade.php
#section('content')
{!! Form::open() !!}
...
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
#parent
#endsection
login.blade.php
#section('content')
{!! Form::open() !!}
...
{!! Form::close() !!}
#parent
#endsection
or remove the sections.
register.blade.php
{!! Form::open() !!}
...
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
login.blade.php
{!! Form::open() !!}
...
{!! Form::close() !!}
See this similar question.
Updated per comments
If you need to access the pages separately you could extract the form code into partials and include them where needed.
main.blade.php
#extends('master')
#section('content')
#include('auth.partials.login')
<hr>
#include('auth.partials.register')
#endsection
Setup you stand alone pages.
auth/register.blade.php
#section('content')
#include('auth.partials.register')
#endsection
auth/login.blade.php
#section('content')
#include('auth.partials.login')
#endsection
Setup your partials
auth/partials/register.blade.php
{!! Form::open() !!}
...
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
auth/partials/login.blade.php
{!! Form::open() !!}
...
{!! Form::close() !!}

Resources