Laravel date input field default time value - php

I am trying to display a default time value within an input field. For some reason the current time is showing up in my source code but is not displayed in my view.
My Form:
<div class="form-group">
{!! Form::label('published_at', 'Publish On') !!}
{!! Form::input('date','published_at', Carbon\Carbon::now()->format('H:i'), ['class' => 'form-control']) !!}
</div>
Sourcecode:
<div class="form-group"> <label for="published_at">Publish On</label>
<input class="form-control" name="published_at" type="date" value="15:58" id="published_at">
</div>
What's wrong here? I am trying to get the current time and date.

Related

Customize Laravel collective form with font awesome

I am looking for a way to add customization to laravel form,instead of using type form
How can I get the following,
<div class="col-sm-4">
<div class="input-group">
<span class="input-group-append">
<span class="input-group-text"><i
class="livicon" data-name="number"
data-size="18" data-c="#000"
data-hc="#000" data-loop="true"></i></span>
</span>
<input type="number" class="form-control"
placeholder="Warehouse No" id="wh_no" name="wh_no" >
</div>
out from laravel forms,this don't load livicion or font-awesome,,ve
{!! Form::number('wh_no', null, ['class' => 'form-control','class'=>'livicon','data-name'=>'user']) !!}
like this

how to retrieve data from database into input values of a form in laravel php to complete the update action?

I'm trying to populate data into input values from mysql database so that I can see and change the data.
I have a form named form.blade.php which will be included in both create.blade.php and edit.blade.php.
What I need is to know is how to retrive data from database to populate data to those inputs when I click on edit.Then when I click on Add button in index.blade.php ,I'll be redirected to create.blade.php and then inputs must be empty.
I tried to add some code functions to every input value
{{old('title',$page->title}} then {{ isset($page) ? page->title: ''}}
but its not working
I'm using one form to perform create and update actions
This is the edit.blade.php
#extends('layouts.master')
#section('content')
<div class="card">
<div class="card-header card-header-success">
<h4 class="card-title ">Pages Data Table</h4>
<p class="card-category"> Here you can update Page data</p>
</div>
<div class="card-body">
{!! Form::open(['action' => 'PagesController#store', 'method' => 'POST' ]) !!}
#csrf
#method('PUT')
#include('pages.includes.form')
{!! Form::close() !!}
</div>
</div>
#endsection
and this is the child form form.blade.php
<div class="form-group">
{{Form::label('title', 'Title')}}
{{ Form::text('title', '',['class' => 'form-control', 'placeholder' => 'Title']) }}
</div>
<div class="form-group">
{{Form::label('content', 'Content')}}
<br>
{{ Form::textarea('content', '',['class' =>'form-control', 'placeholder'=> 'Content']) }}
</div>
<div class="form-group">
<div class="form-check form-check-radio">
<label class="form-check-label">
{{Form::input('radio','status', '0',['class' => 'form-check-input','checked'=>'checked'])}} Status Off
<span class="circle">
<span class="check"></span>
</span>
</label>
</div>
<div class="form-check form-check-radio">
<label class="form-check-label">
{{Form::input('radio','status', '1',['class' => 'form-check-input'])}} Status On
<span class="circle">
<span class="check"></span>
</span>
</label>
</div>
</div>
<input type="submit" name="submit" class="btn btn-success" value="Save">
When you are using the same form for create and update, you can use the optional method.
<input type="text" class="form-control{{ $errors->has('date') ? ' is-invalid' : '' }}" name="date" id="date" value="{{ old('date', optional($expense)->date) }}">
In the create function send the variable as null like $expense = null;
And in edit function the variable has its own value from database. So when the variable has its value the input field will be filled by value from database otherwise it will be empty.

Modify database Laravel 5

I have to do modifications on a website using Laravel which i never used before.
I made this div in the form, in my view file :
<div class="form-group">
<label class="col-md-4 control-label">Organisme</label>
<div class="col-md-6">
#if(Auth::user()->hasRole(['responsable_rh', 'responsable_formation', 'responsable_qual']))
<input type="text" class="form-control" name="organismeShow" value="{{$orga_user[0]->nom}}" readonly>
<input type="hidden" name="id_organisme" value="{{$orga_user[0]->id_organisme}}">
#elseif(Auth::user()->hasRole(['admin', 'super_admin']))
<select class="form-control chosen" name="id_organisme" autocomplete="off">
#foreach($all_orga as $val)
#if($val->id_organisme==13)
<OPTION selected value="{{$val->id_organisme}}">{{$val->nom}}</OPTION>
#else
<OPTION value="{{$val->id_organisme}}">{{$val->nom}}</OPTION>
#endif
#endforeach
</select>
#endif
</div>
</div>
It works well, I have my list showing up. Now I need to make it change a value in the database but I don't know at all where to modify things. Know that this form already existed in the previous version, and already modify things in database, but I have to add this section.
Form opener :
{!!Form::open(array('id'=>'check_form', 'route' => 'rh_ajout_update'))!!}
{!!Form::close()!!}
{!!Form::open(array('class'=>'form-horizontal', 'route'=>'update_user', 'files' => 'true', 'enctype' => 'multipart/form-data'))!!}
{!!Form::hidden('id_compte', $compte->id_compte)!!}

Laravel reset value in input after validation fail?

I'm so confuse now, I'm learning Laravel from Laracast, according to the instructor, after validation fail, the form does not reset values that user entered. But when testing validation, when I submit the form reset every thing.
Another question is the undefined variable $errors when I try to access it.
My Controller
<?php
namespace App\Http\Controllers;
use App\Articles;
use App\Http\Requests\CreateArticle;
use Carbon\Carbon;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ArticlesController extends Controller
{
public function create()
{
return view('articles.create');
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
Articles::create($request->all());
return redirect('articles');
}
}
My View
#extends('app')
#section('content')
<h1>Create a new Articles</h1>
<hr/>
{!! Form::open(['url' => 'articles']) !!}
<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::label('published_at', 'Published On:') !!}
{!! Form::input('text', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('submit', ['class' => 'btn btn-primary']) !!}
</div>
#if(isset($errors))
{{var_dump($errors)}}
#endif
{!! Form::close() !!}
#stop
He use v5.0 and I'm using v5.2
return the following from your controller...
return redirect('articles')->withInput();
see if that helps. you can exclude certain fields if you wanted to like this..
return redirect('articles')->withInput($request->only('email'))
If your are not using
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', old('title'), ['class' => 'form-control']) !!}
</div>
but have simple input fields like
<input id="Email" name="Email" type="email" class="uit-input" placeholder="your email address" value={{old('Email')}}>
'prefill' the value with the old data.
There are a couple of issues here.
The first one, the inputs not being saved after validation fails. I believe this is because you are passing null into the functions which are building the inputs when you should be passing in the value you wish it to default to. Try the following...
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', old('title'), ['class' => 'form-control']) !!}
</div>
This should then populate the input element with the old data which was previously entered. Just follow the same procedure for the rest of the form inputs.
The second issue with the $errors value not being set is actually due to a change with Laravel 5.2. Many people have been having the same exact issue so I'd just like to refer you to a previous answer: Laravel 5.2 $errors not appearing in Blade
Best way for you
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="upload_title" class="form-control" value="{{ (old('title')) ? old('title') : $data->title}}">
</div>

Count only inputs with value in Laravel 4

I am trying to create a multiple choice quiz in Laravel,
This is the form the admin uses to insert the questions in the database, so this is not to play the quiz!
I used angular to show and hide answer C D E and F, showing only A and B when the page is loaded.
The idea is to create a for loop, so that I can insert every answer that is not empty.
How do I do this in laravel? I was thinking of count(Input::get('answer')) but it doesn't work. And even if it worked it will still count all my answers but not the ones that has a value.
All my answer inputs have the same name.
Please help!
{{ Form::open(['action' => 'QuizController#storeQuestions']) }}
{{ Form::hidden('id', $quiz->id) }}
<div class="form-group">
<label class="form-label">Question:</label>
{{ Form::text('question', Input::old('question'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
<label class="form-label">Answer A:</label>
{{ Form::text('answer', Input::old('answer'), array('class' => 'form-control')) }}
{{ Form::radio('is-correct') }}
</div>
<div class="form-group">
<label class="form-label">Answer B:</label>
{{ Form::text('answer', Input::old('answer'), array('class' => 'form-control')) }}
{{ Form::radio('is-correct') }}
</div>
<!-- De '+' krijgt de showInput van de vorige input! -->
<div class="form-group">
<span class="add-question-button"><a class="plus-input" ng-click="addAnswer(1)">+</a></span>
</div>
<div ng-show="showInput1" class="form-group">
<label class="form-label">Answer C:</label>
{{ Form::text('answer', Input::old('answer'), array('class' => 'form-control')) }}
{{ Form::radio('is-correct') }}
</div>
<div class="form-group">
<span class="add-question-button"><a ng-show="showInput1" ng-click="addAnswer(2)" class="plus-input">+</a></span>
</div>
<div ng-show="showInput2" class="form-group">
<label class="form-label">Answer D:</label>
{{ Form::text('answer', Input::old('answer'), array('class' => 'form-control')) }}
{{ Form::radio('is-correct') }}
</div>
<div class="form-group">
<span class="add-question-button"><a ng-show="showInput2" ng-click="addAnswer(3)" class="plus-input">+</a></span>
</div>
<div ng-show="showInput3" class="form-group">
<label class="form-label">Answer E:</label>
{{ Form::text('answer', Input::old('answer'), array('class' => 'form-control')) }}
{{ Form::radio('is-correct') }}
</div>
<div class="form-group">
<span class="add-question-button"><a ng-show="showInput3" ng-click="addAnswer(4)" class="plus-input">+</a></span>
</div>
<div ng-show="showInput4" class="form-group">
<label class="form-label">Answer F:</label>
{{ Form::text('answer', Input::old('answer'), array('class' => 'form-control')) }}
{{ Form::radio('is-correct') }}
</div>
{{ Form::submit('Next question', array('class' => 'btn btn-primary btn-block')) }}
<button class="btn btn-success btn-block">Finish</button>
{{ Form::close() }}
The way you've structured the form is a bit confusing.
If this is a multiple choice quiz, then the answers should be non-editable (labels), and the user should just be required to select the correct answer using a radio button.
In that case, you can set the radio button value to the answer text (or ID, if you're pulling the available answers from a database).
For example:
<label>Who was the first man on the moon?</label>
<label>
<input type="radio" name="man_on_moon" value="Armstrong" /> Neil Armstrong
</label>
<label>
<input type="radio" name="man_on_moon" value="Aldrin" /> Buzz Aldrin
</label>
<label>
<input type="radio" name="man_on_moon" value="Hanks" /> Tom Hanks
</label>
(New answer, in response to the revised question)
From the way you've worded the question, I've assumed the maximum number of answers for a question is fixed. Let's further assume you identify those with the letters A to F, as in your example.
Given your "answer" field accepts multiple responses, its name should include square brackets. We can identify each individual answer as follows:
<input type="text" name="answer[A]" />
We can identify the correct answer by constructing radio buttons of the form:
<input type="radio" name="correct_answer" value="A" />
Calling Input::get('answer') in your controller will return an associative array, where the key is the answer identifier (A to F), and the value is the answer text.
You can use PHP's array_filter function to filter out the empty answers.
$answers = array_filter(Input::get('answer'));

Categories