Laravel - Form only submitting data from one section in blade view - laravel

I have two sections included in one view file that I am working out of and they are the sidebar and content sections. I ran into a rather confusing issue, being that both sections contain data that I want submitted via the form.
For some reason, the view is only rendering the form across the content section and excluding all of the data from the sidebar section. Originally I thought opening the form at the top of the content section and closing it at the end of the sidebar section would work. Unfortunately that was not the case and I'm at a loss.
I was wondering if there was any way possible to extend the form so it includes the data across both sections? Any suggestions on how to fix this issue would be greatly appreciated!
#extends('main')
#section('title', 'Sign Up')
#section('content')
{!! Form::open(['route' => 'signup.store', 'method' => 'post']) !!}
<div class="panel-header"><span>{{ $visitor->username }}'s Profile</span></div>
<div class="secondary-panel" align="center"><strong>Player Information</strong></div>
<div class="xbs-border"></div>
<div class="row_1g">
<div class="body-content">
<div class="col-md-6">
<div class="form-group">
{{ Form::label('first', 'First Name') }}
{{ Form::text('first', $player->first, array('class' => 'form-control', 'placeholder' => 'First Name')) }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ Form::label('last', 'Last Name') }}
{{ Form::text('last', $player->last, array('class' => 'form-control', 'placeholder' => 'Last Name')) }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ Form::label('number', 'Number') }}
{{ Form::selectRange('number', 0, 99, $player->num, array('class' => 'form-control')) }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ Form::label('birthday', 'Birthday') }}
{{ Form::date('birthday', $player->birthday, array('class' => 'form-control')) }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ Form::label('city', 'City') }}
{{ Form::text('city', $player->city, array('class' => 'form-control', 'placeholder' => 'City')) }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ Form::label('state_province', 'State/Province') }}
{{ Form::select('state_province',['United States' => ['' => 'State/Province', 'AL'=>'Alabama','AK'=>'Alaska','AZ'=>'Arizona','AR'=>'Arkansas','CA'=>'California','CO'=>'Colorado','CT'=>'Connecticut','DE'=>'Delaware','DC'=>'District of Columbia','FL'=>'Florida','GA'=>'Georgia','HI'=>'Hawaii','ID'=>'Idaho','IL'=>'Illinois','IN'=>'Indiana','IA'=>'Iowa','KS'=>'Kansas','KY'=>'Kentucky','LA'=>'Louisiana','ME'=>'Maine','MD'=>'Maryland','MA'=>'Massachusetts','MI'=>'Michigan','MN'=>'Minnesota','MS'=>'Mississippi','MO'=>'Missouri','MT'=>'Montana','NE'=>'Nebraska','NV'=>'Nevada','NH'=>'New Hampshire','NJ'=>'New Jersey','NM'=>'New Mexico','NY'=>'New York','NC'=>'North Carolina','ND'=>'North Dakota','OH'=>'Ohio','OK'=>'Oklahoma','OR'=>'Oregon','PA'=>'Pennsylvania','RI'=>'Rhode Island','SC'=>'South Carolina','SD'=>'South Dakota','TN'=>'Tennessee','TX'=>'Texas','UT'=>'Utah','VT'=>'Vermont','VA'=>'Virginia','WA'=>'Washington','WV'=>'West Virginia','WI'=>'Wisconsin','WY'=>'Wyoming',], 'Canada' => ['AB' => 'Alberta', 'BC' => 'British Columbia', 'MB' => 'Manitoba', 'NB' => 'New Brunswick', 'NL' => 'Newfoundland', 'NS' => 'Nova Scotia', 'NT' => 'Northwest Territories', 'NU' => 'Nunavut', 'ON' => 'Ontario', 'PE' => 'Prince Edward Island', 'QC' => 'Quebec', 'SK' => 'Saskatchewan', 'YT' => 'Yukon']], $player->state_province, array('class' => 'form-control')) }}
</div>
</div>
</div>
<div class="row_1f" style="text-align:center;border-top:1px solid #ddd;">
<div class="body-content">
<strong>Player name falls under the site terms and conditions.</strong>
</div>
</div>
</div>
<div class="panel-header"><span>Additional Information</span></div>
<div class="secondary-panel" align="center"><strong>Player Availability</strong></div>
<div class="xbs-border"></div>
<div class="row_1g">
<div class="body-content">
<div class="col-md-6">
<div class="form-group">
{{ Form::label('time', 'Preferred Time (EST)') }}
{{ Form::select('time', ['9' => '9:00 PM', '10' => '10:00 PM', '11' => '11:00 PM'], $player->time, array('class' => 'form-control')) }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ Form::label('commitment', 'League Commitment') }}
{{ Form::select('commitment', ['0' => 'Part-Time', '1' => 'Full-Time',], $player->commitment, array('class' => 'form-control')) }}
</div>
</div>
<div class="col-md-12">
<div class="form-group">
{{ Form::label('available_extra', 'Availability Extra') }}
{{ Form::textarea('available_extra', $player->available_extra, array('class' => 'form-control', 'placeholder' => 'Any additional information that you would like to add.')) }}
</div>
</div>
</div>
</div>
<div class="row_1g">
<div class="body-content">
<div class="form-group" align="center">
{!! Form::hidden('player_id', $visitor->user_id) !!}
{{ Form::submit('Submit Registration', array('class' => 'btn xbs-button')) }}
</div>
</div>
</div>
<div class="row_1f" style="border-top:1px solid #ddd;">
<div class="body-content">
<ul style="margin-bottom:0;">
<li><strong>Part-Time Player</strong> - At least two games per week.</li>
<li><strong>Full-Time Player</strong> - Anything over two games games per week.</li>
</ul>
</div>
</div>
#endsection
#section('sidebar')
<div class="panel-header"><span>Position Preferences</span></div>
<div class="secondary-panel" align="center"><strong>Position Breakdown</strong></div>
<div class="xbs-border"></div>
<div class="col-md-12 no-padding">
<div class="row_1g">
<div class="body-content">
<div class="col-xs-12">
<div class="form-group">
{{ Form::label('pos_c', 'Center') }}
{{ Form::select('pos_c', ['0' => 'Never', '1' => 'Rarely', '2' => 'Occasional', '3' => 'Preferred'], null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('pos_lw', 'Left Wing') }}
{{ Form::select('pos_lw', ['0' => 'Never', '1' => 'Rarely', '2' => 'Occasional', '3' => 'Preferred'], null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('pos_rw', 'Right Wing') }}
{{ Form::select('pos_rw', ['0' => 'Never', '1' => 'Rarely', '2' => 'Occasional', '3' => 'Preferred'], null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('pos_ld', 'Left Defense') }}
{{ Form::select('pos_ld', ['0' => 'Never', '1' => 'Rarely', '2' => 'Occasional', '3' => 'Preferred'], null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('pos_rd', 'Right Defense') }}
{{ Form::select('pos_rd', ['0' => 'Never', '1' => 'Rarely', '2' => 'Occasional', '3' => 'Preferred'], null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('pos_g', 'Goalie') }}
{{ Form::select('pos_g', ['0' => 'Never', '1' => 'Rarely', '2' => 'Occasional', '3' => 'Preferred'], null, array('class' => 'form-control')) }}
</div>
</div>
</div>
</div>
<div class="row_1f" style="border-top:1px solid #ddd;">
<div class="body-content">
<ul style="margin-bottom:0;">
<li><strong>Never</strong> - 0 games per season.</li>
<li><strong>Rarely</strong> - up to 10 games per season.</li>
<li><strong>Occasional</strong> - 10 to 20 games per season.</li>
<li><strong>Preferred</strong> - Any amount of games.</li>
</ul>
</div>
</div>
</div>
{!! Form::close() !!}
#endsection
Main.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
#include('partials._header')
#yield('stylesheets')
</head>
<body>
#include('partials._navigation')
<div class="container">
<div class="col-md-12" style="padding:0;margin-top:20px;">
#include('partials._messages')
</div>
<div class="col-md-4" style="padding-left:0;">
#yield('sidebar')
</div>
<div class="col-md-8" style="padding-right:0;">
#yield('content')
</div>
</div>
#include('partials._footer')
#include('partials._javascript')
#yield('scripts')

So the problem is in your main.blade.php file.
This markup will cause a browser to try and fix your form, because the opening and closing form element do not share the same parent.
<div class="col-md-4" style="padding-left:0;">
#yield('sidebar')
</div>
<div class="col-md-8" style="padding-right:0;">
#yield('content')
</div>
This would most likely result in a form in the content section and no form in the sidebar section. For example:
<div class="col-md-4" style="padding-left:0;">
<form>
...
</form>
</div>
<div class="col-md-8" style="padding-right:0;">
...
</div>
You need to either wrap your columns with the form element, or move the column divs into your sections.
#section('content')
{!! Form::open(['route' => 'signup.store', 'method' => 'post']) !!}
<div class="col-md-8" style="padding-right:0;">
...
</div>
#endsection
#section('sidebar')
<div class="col-md-4" style="padding-left:0;">
...
</div>
{!! Form::close() !!}
#endsection

Related

Laravel view load twice to display comment

I don't know why the page has to load twice to display comments.
Here is my route: Route::post('/addComment', 'CommentsController#addComment');
Here is my controller:
public function addComment(Request $request)
{
$this->validate($request, [
'name' => 'required',
'body' => 'required',
]);
$lesson_id = $request->lesson_id;
$comment = new Comment;
$comment->name = $request->input('name');
$comment->body = $request->input('body');
$comment->parrent_id = '0';
$comment->lesson_id = $request->lesson_id;
$comment->save();
return back();
}
Here is my view:
<div class="leave_review">
<h3 class="blog_heading_border"> コメント </h3>
{!! Form::open(['action' => ['CommentsController#addComment'], 'method' => 'POST', 'id' => 'postForm' ]) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="hidden" id ="lesson_id" name="lesson_id" value="{{$lesson->id}}" />
</div>
<div class="row">
<div class="col-sm-6">
#error('name')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('name','名前')}}
{{Form::text('name', '', ['class' => 'form-group', 'id' => 'name' ]) }}
</div>
<div class="col-sm-12">
#error('body')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('body','メッセージ')}}
{{Form::textarea('body', '', ['class' => 'form-group', 'id' => 'body']) }}
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
</div>
{{Form::submit('Submit', ['class' => 'send mt_btn_yellow pull-right', 'id' => 'submit'])}}
{!! Form::close() !!}
{{-- End add comment --}}
{{--Display comment--}}
<ol class="review-lists">
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
you don't have a #endforeach

Laravel required if validation issue

Laravel required if validation issue
Blade:
{{ Form::open(['route' => ['updateEmailSettings']]) }}
<div class="form-group row">
{{ Form::label('driver','Mail Driver',['class' => 'col-md-3 col-form-label required']) }}
<div class="col-md-9">
{{ Form::select('driver',$drivers, null,['class' => 'form-control', 'placeholder' => 'Select']) }}
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label font-weight-bold">Mandrill</label>
</div>
<div class="form-group row">
{{ Form::label('mailgun_secret','Secret',['class' => 'col-md-3 col-form-label']) }}
<div class="col-md-9">
{{ Form::text('mailgun["secret"]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
</div>
</div>
<div class="form-group row">
<div class="col-md-9 ml-md-auto">
{{ Form::button('<i class="far fa-save"></i> Save',['class'=>'btn btn-primary mr-3','type'=>'submit']) }}
<a class="btn btn-danger" href="{{ route('emailSettings') }}"><i class="far fa-times-circle"></i> Cancel</a>
</div>
</div>
{{ Form::close() }}
Form Request:
return [
'driver' => 'required',
'mailgun.*.domain' => 'required_if:driver,mailgun'
];
Validation always fails. Please suggest me if i miss anything.
Resolved myself
Blade: Removed double quotes inside the bracket.
{{ Form::text('mailgun[secret]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
Form Request: Removed asterisk
'mailgun.domain' => 'required_if:driver,mailgun'

Link Button Not Working in Laravel

I want to edit a record in a CRUD application in laravel where I have a button which has been linked to go to the index view but when I click it, it redirects me to the UPDATE method of the controller.
This is my form:
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }}
{{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }}
{{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
{!! Form::close() !!}
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
</div>
</div>
</div>
I have the following button for going back to the index page but this is taking me to the UPDATE method of the controller:
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
This is my index method in the controller:
public function index()
{
$players = Player::paginate(5);
return view('players.index', compact('players'));
}
This is the UPDATE method in the controller:
public function update(Request $request, $id)
{
return "Hi";
}
This is my route file contents:
Route::resource('news', 'NewsController');
Route::resource('competition', 'CompetitionsController');
Route::resource('players', 'PlayersConroller');
Everything looks fine to me but I don't know what goes wrong here.
Any help is appreciated in advance.
I am not sure if it will solve your issue, try to put your button code outside the form-group div.
You can change your code as
Cancel
You can check your html you have put button inside form tag which of type submit that's why it is submitting the form again.
Replace your form code with:
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }} {{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }} {{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
</div>
{!! Form::close() !!}
</div>
<a href="{{ route('players.index') }}">
<button class="btn btn-danger">Cancel</button>
</a>
</div>

Route [admin/update] not defined in Laravel

I am having trouble updating my data in Laravel and this is giving me headache. Every time i clicked on the update button error occurs.
Below is the error:
"Route [admin/update] not defined. (View: C:\xampp\htdocs\Tailor\core\resources\views\expensesCat\edit.blade.php)"
here is the edit.blade
<div class="portlet light bordered">
<h3 class="page-title">Expenses Categories</h3>
{!! Form::model($expenses_category, ['method' => 'POST', 'route' => ['admin/update', $expenses_category->id]]) !!}
<div class="panel panel-default">
<div class="panel-heading">
Edit
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 form-group">
<input name="order_create_by" type="hidden" value="{{ Auth::user()->id }}">
{!! Form::label('name', 'Name*', ['class' => 'control-label']) !!}
{!! Form::text('name', old('name'), ['class' => 'form-control', 'placeholder' => '']) !!}
<p class="help-block"></p>
#if($errors->has('name'))
<p class="help-block">
{{ $errors->first('name') }}
</p>
#endif
</div>
</div>
</div>
</div>
The web.php
Route::post('/update/{id}', 'ExpensesCategoriesController#updateCat');
The ExpensesCategoriesController
public function updateCat(UpdateExpensesCategoriesRequest $request, $id)
{
$expenses_category = ExpensesCategory::findOrFail($id);
$expenses_category->update($request->all());
return redirect('admin/expenses_categories');
}
Use url in your form -
{!! Form::model($expenses_category, ['method' => 'POST', 'url' =>'admin/update/'.$expenses_category->id]) !!}
You're trying to use the route by its name, but you didn't name it. So, change it to:
'route' => ['admin.update', ....
And name the route:
Route::post('/update/{id}', 'ExpensesCategoriesController#updateCat')->name('admin.update');
Or:
Route::post('/update/{id}', ['as' => 'admin.update', 'uses' => 'ExpensesCategoriesController#updateCat']);

foreach loop in a form to save values

I try to save two values from text field but actually with this view when i click on save i get only the first ID of the foreach.
the two text field are : resultat_domicile and resultat_visiteur i would like to get the values foreach objects
someone know how to achieve this ? thanks a lot in advance
here my view :
#foreach($rencontres as $rencontre)
{!! Form::open(array('route' => array('add.resultat', $rencontre->id)))!!}
<div class="box-body">
<div class="row">
<div class="col-md-5">
<h2 class="pull-right"><div class="fa fa-shield pull-right"></div>{{$rencontre->equipe_domicile->lb_equipe}}</h2>
</div>
<div class="col-md-2">
<div class="row">
<div class="col-xs-5">
{!! Form::text('resultat_domicile', null , ['class' => 'form-control', 'placeholder' => 'Ex: 20 points']) !!}
</div>
<div class="col-xs-2 text-center">
<span class="h2">-</span>
</div>
<div class="col-xs-5">
{!! Form::text('resultat_visiteur', null , ['class' => 'form-control', 'placeholder' => 'Ex: 20 points']) !!}
</div>
</div>
</div>
<div class="col-md-5">
<h2 class="pull-left"><div class="fa fa-shield pull-left"></div>{{$rencontre->equipe_visiteur->lb_equipe}}</h2>
</div>
</div>
<div class="box-footer">
<div class="col-xs-12 text-center">
{!! Form::submit('Save', ['class' => 'btn btn-info btn-lg center-block']) !!}
{!! Form::close() !!}
</div>
</div>
#endforeach
here my controller :
public function addResultat(Request $request , $id){
$rencontre = Rencontre::findOrFail($id);
$rencontre->resultat_domicile = $request->input('resultat_domicile');
$rencontre->resultat_visiteur = $request->input('resultat_visiteur');
dd($rencontre);
}
Are you sure you're not only retrieving the LAST of the 'recontres'? When you create those form elements with the names 'resultat_domicile' and 'resultat_visiteur', you are overwriting which field gets sent to the request.
If you put a [] after the name, then they will be an array of values when they get sent to the request.
For example, this would be:
{!! Form::text('resultat_domiciles[]', ...

Resources