Form model binding not working in laravel 5.2 using laravelcollective - laravel

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

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

Yield a form on a different page using laravel

I'm making a twitter look-a-like app.
I have a form to post a message on 1 blade (createMessage.blade.php) which is as followed:
#section('message')
{!! Form::open(['url' => 'message/postmessage']) !!}
<div class="form-group col-lg-6 col-lg-offset-3">
{!! Form::label('body') !!}
{!! Form::textarea('body', null,['class' => 'form-control']) !!}
</div>
<div class="form-group col-lg-6 col-lg-offset-3">
{!! Form::submit('Post message', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection`
Now I want to get my form on different pages (such as my timeline) but if I try #yield('message') it doesn't work.
timeline:
#extends('layouts.app')
#section('content')
#yield('message')
//some code for my timeline
#endsection
I can't find out why it doens't work.
Thanks in advance!
You have to use
#include('createMessage.blade.php') in the timeline file and remove the #section declaration in createMessage.blade.php

How to add bootstrap for below laravel 5 form

Below is the output of form.
I want to keep this both 2 fields in a one row. How can I do that using bootstrap? This is my view code.
{!! Form::label('titles', 'Title') !!}
{!! Form::text('title',null,['class' => 'form-control']) !!}
You can try like this:
<div class="form-group col-md-1">
{!! Form::label('titles', 'Title') !!}
</div>
<div class="form-group col-md-11">
{!! Form::text('title',null,['class' => 'form-control']) !!}
</div>

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