laravel collective checkbox form - laravel

I'm trying to implement checkbox and laravel collective in my form but I get only single value in form, any ideas how to fix it
{!! Form::open(array('action'=>'UserController#updateInfo','method'=>'post')) !!}
Workdays:
<br>
{!! Form::label('monday', 'Monday') !!}
{!! Form::checkbox('workday', 'monday') !!}
<br>
{!! Form::label('tuesday', 'Tuesday') !!}
{!! Form::checkbox('workday', 'tuesday') !!}
<br>
{!! Form::label('wednesday', 'Wednesday') !!}
{!! Form::checkbox('workday', 'wednesday') !!}
<br>
{!! Form::label('thursday', 'Thursday') !!}
{!! Form::checkbox('workday', 'thursday') !!}
<br>
{!! Form::label('friday', 'Friday') !!}
{!! Form::checkbox('workday', 'friday') !!}
<br>
{!! Form::label('saturday', 'Saturday') !!}
{!! Form::checkbox('workday', 'saturday') !!}
<br>
{!! Form::label('sunday', 'Sunday') !!}
{!! Form::checkbox('workday', 'sunday') !!}
<br>
{!! Form::submit('Save', $attributes = ['class'=>'button']) !!}
{!! Form::close() !!}
when I print my request i only get single output (eg. selected monday friday I get only friday when request is processed)
also labels not working - ideas on that too?

You're using the same name (workday) for all your checkboxes. That's why it's only showing the last checkbox with that name
Just change all names to workday[] instead.
{!! Form::checkbox('workday[]', 'monday') !!}
This will return all selected checkbox in an array.

Try this:
You used same name for every checkbox when you checked multiple checkbox you only get the last value.
{!! Form::open(array('action'=>'UserController#updateInfo','method'=>'post')) !!}
Workdays:
<br>
{!! Form::label('monday', 'Monday') !!}
{!! Form::checkbox('workday[]', 'monday') !!}
<br>
{!! Form::label('tuesday', 'Tuesday') !!}
{!! Form::checkbox('workday[]', 'tuesday') !!}
<br>
{!! Form::label('wednesday', 'Wednesday') !!}
{!! Form::checkbox('workday[]', 'wednesday') !!}
<br>
{!! Form::label('thursday', 'Thursday') !!}
{!! Form::checkbox('workday[]', 'thursday') !!}
<br>
{!! Form::label('friday', 'Friday') !!}
{!! Form::checkbox('workday[]', 'friday') !!}
<br>
{!! Form::label('saturday', 'Saturday') !!}
{!! Form::checkbox('workday[]', 'saturday') !!}
<br>
{!! Form::label('sunday', 'Sunday') !!}
{!! Form::checkbox('workday[]', 'sunday') !!}
<br>
{!! Form::submit('Save', $attributes = ['class'=>'button']) !!}
{!! Form::close() !!}

You're using the same name (workday) for all your checkboxes. That's why it's only showing the last checkbox with that name
{{ Form::checkbox('workday[]', 'monday') }}

Related

How do I put fill out this field in Laravel?

How do I put in my form.blade the "Please fill out this field" in every attribute?
<div class="form-group">
{!! Form::label('title','Title:'); !!}
{!! Form::text('title', null,['class'=>'form-control']); !!}
</div>
<div class="form-group">
{!! Form::label('description','Description:'); !!}
{!! Form::textarea('description', null,['class'=>'form-control']); !!}
</div>
<div class="form-group">
{!! Form::label('stock','Stock:'); !!}
{!! Form::text('stock', null,['class'=>'form-control']); !!}
</div>
<div class="form-group">
{!! Form::label('category_id','Category ID:'); !!}
{!! Form::select('category_id', $categories, null,['class'=>'form-control', 'placeholder'=>'Choose a Category']); !!}
</div>
{!! Form::submit('Save',['class'=>'btn btn-success']); !!}
Try this
<div class="form-group">
{!! Form::label('title','Title:'); !!}
{!! Form::text('title', null,['class'=>'form-control','required']); !!}
</div>
<div class="form-group">
{!! Form::label('description','Description:'); !!}
{!! Form::textarea('description', null,['class'=>'form-control','required]); !!}
</div>
<div class="form-group">
{!! Form::label('stock','Stock:'); !!}
{!! Form::text('stock', null,['class'=>'form-control','required']); !!}
</div>
<div class="form-group">
{!! Form::label('category_id','Category ID:'); !!}
{!! Form::select('category_id', $categories, null,['class'=>'form-control', 'placeholder'=>'Choose a Category','required']); !!}
</div>
{!! Form::submit('Save',['class'=>'btn btn-success']); !!}

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

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>

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