Modify database Laravel 5 - php

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

Related

How to solve the index problem of input and label prepared with for loop in PHP?

I am doing the structure I created with the for loop in the homepage.blade file. I have such a problem. Let's say I created 5 inputs. When I press the 5th label after the structures are created, the 1.input is always activated. How do I solve this?
homepage.blade.php
<h4>Checkbox</h4>
#include('items.checkbox')
#include('items.checkbox', array('error' => true))
#include('items.checkbox', array('label' => 'Inline Centered', 'labelClass'=> 'label-centered'))
checkbox.blade.php
<fieldset class="form-group checkbox #if (!empty($width)){{$width}}#endif #if(!empty($labelClass)) {{$labelClass}} #endif">
<input id="checkbox{{$i}}" type="checkbox" placeholder="Option text" />
<div class="wrapper">
<label for="checkbox{{$i}}">#if (!empty($label)) {{$label}} #else Checkbox #endif *</label>
<small>Helper text goes here</small>
</div>
</fieldset>

Laravel form select option

I have a landing page with Create, Edit and Delete buttons, The Edit button is linked to a select (dropdown) menu and my route for the URL is /events/{number}/edit.
How will I get my URL to write to this format as the page will already be loaded and then the selection is made.
<div class="col-sm-4">
<form method="get" action="{{ route('events.edit', ['event_id' => '2']) }}">
<label>Event Name</label>
<select name="event_edit" class="form-control form-control-lg">
#foreach($events as $event)
<option>{{$event->event_name}}</option>
#endforeach
</select>
<button type="submit" class="btn btn-primary btn-round">Edit</button>
</form>
</div>
The above works but I have to put the event_id manually which is set to 2, will javascript be needed for this one?
Route::resource('events', 'EventController');
GET|HEAD | events/{event}/edit | events.edit | App\Http\Controllers\EventController#edit | web
I do not have a larvel instance I can test on but simply put PHP is static so once a DOM is render you cannot update the output with PHP ie putting the $event->id in the action when a dropdown (or any element) is changed. If you wanted to you would need JavaScript. However, I would opt for a more straight forward approach.
You will need to create a generic edit route on your controller and put the id on the option as the value.
<div class="col-sm-4">
<form method="get" action="{{ route('events.edit') }}">
<label>Event Name</label>
<select name="event_edit" class="form-control form-control-lg">
#foreach($events as $event)
<option value="{{$event->id}}">{{$event->name}}</option>
#endforeach
</select>
</form>
</div>
Now when the form is submitted you will have the id of the event the user wanted to edit. Simply use that to get the event in the controller and render the edit screen for that event. If you want the url to be displayed nicely as site.com/edit/event/3 then you could use something like
return redirect()->route('route.name', [$param]);
which should redirect to that routes url with the param being the id then do all the logic for getting and displaying there.
hm. Do we need the form-action here? How about this:
<div class="col-sm-4">
<form method="get" action="">
<label>Event Name</label>
<select name="event_edit" class="form-control form-control-lg"
onChange="window.location.href='/events/' + this.options[this.selectedIndex].value + '/edit'">
#foreach($events as $event)
<option value="{{$event->id}}">{{$event->name}}</option>
#endforeach
</select>
</form>
</div>

How to pass/access data correctly to controller in Laravel 5.2

New to laravel and followed their intermediate task list through to try grasp the concepts.
I did a php artisan migrate in CLI to add 2 new columns to a task table,The first column is DATE called due & The Second column is TEXT called description,i have also added to the view so it now looks like this
<form action="/task" method="POST" class="form-horizontal">
{{ csrf_field() }}
<!-- Task Name -->
<div class="form-group">
<label for="task-name" class="col-sm-1 control-label">Task</label>
<div class="col-sm-3">
<input type="text" name="name" id="task-name" class="form-control" value="{{ old('task') }}">
</div>
<label for="task-due" class="col-sm-2 control-label">Due Date</label>
<div class="col-sm-4">
<input type="date" name="due_date" id="task-due" class="form-control" value="">
</div>
</div>
<!-- Add Task Button -->
<div class="form-group">
<label for="task-due" class="col-sm-1 control-label">Description</label>
<div class="col-sm-9">
<input type="text" name="description" id="task-description" class="form-control" value="">
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-default">
<i class="fa fa-btn fa-plus"></i>Add Task
</button>
</div>
</div>
</form>
Just some extra inputs called, due_date & description which I want to fill in a post to the database.
Controller :
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
]);
$request->user()->tasks()->create([
'name' => $request->name,
'due'=>$request->due_date,
'description'=>$request->description,
]);
return redirect('/tasks');
}
I'm not sure if I've written it right as when I post, the name of the task does into the database, but the columns due go to 0000-00-00 and column description is empty.
Am I calling the fields properly in my controller? I've tried swapping names around but I thought the $Request variable contained the form data.
All help and explanations are welcome.
You should make sure you have for your Task model set $fillable property with name, due and description. Now you probably have only name so other are filled by default values and not by those from input
Found the solution,
My fault for not reading the page through properly.
protected $fillable = ['name','due','description'];
I had forgot to increase the $fillable variable set inside the model, so the create() method was only putting data inside the $fillable in
Apologies for wasting anyones time

Posting to the same form in laravel

I'm trying to build a search form in Laravel such that when a user presses the search button, all the contacts matching the search criteria are displayed on the same page.
Below is my form:
{{ Form::open(array('url' => '/directory', 'class' => 'form-horizontal')) }}
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="lastname" placeholder="Enter Last Name">
</div>
</div>
<div class="form-group ">
<label for="phone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="phone" placeholder="Enter Phone number">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-sm-2 control-label">Email</label>
<div class="col-sm-6">
<input type="email" class="form-control" id="inputEmail1" placeholder="Enter Email - leave blank if you are not sure">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn-u btn-u-green">Search</button>
</div>
</div>
<div class="well">
#foreach($users as $user)
<li>{{$user->firstname}} {{$user->lastname}}</li>
#endforeach
</div>
{{ Form::close() }}
The screenshot below gives a pictorial view of my form:
My route is defined as follow:
Route::get('/directory', 'UsersController#filter');
Now, whenever I press the search button, I am getting a MethodNotFound exception.
What I really want to do is to show the search results below the search button.
Edit ..
Everything is working fine now except for one thing.
I'm displaying the data in my view (getting the list of all the users on my page which match the search criteria) but the for loop is not getting executed.
<li>Count of users = {{ $users->count() }}</li>
<li>{{$users->first()->lastname}}</li>
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
So: while I can see the count and the lastname of the first user in the resultant array of records, the foreach loop is not getting executed.
What am I doing wrong?
Laravel routes are bound to the form METHOD, so you need to create also a POST route:
Route::post('/directory', 'UsersController#filter');
You also need to add names to your form fields:
name="lastname"
for
<input type="text" name="lastname" class="form-control" id="lastname" placeholder="Enter Last Name">
And for all the others.
A controller method to handle that query could look like this:
<?php
class UsersController extends Controller {
public function filter()
{
$users = User::query();
if (Input::has('lastname'))
{
$users->where('lastname', Input::get('lastname'))
}
if (Input::has('phone'))
{
$users->where('phone', Input::get('phone'))
}
return View::make('your.view')
->with('userCount', $users->count());
->with('users', $users->get());
}
}
And in your view you can just:
<li>Count of users = {{$userCount}}</li>
#foreach ($users as $user)
<li>
{{ $user->id }} - {{$user->lastname}}
</li>
#endforeach
You have to tell Laravel that you want to use get method for submitting a form to the server. otherwise, Laravel will use post method by default.
Try the following:
{{ Form::open(array('url' => '/directory', 'method' => 'get', 'class' => 'form-horizontal')) }}
^^^

Laravel and angularjs form validation

I am new to angularjs and im playing around with it.
I'm stuck with one thing, in jQuery it's more easier to retrive the validation error messages json object from laravel, with angular i am able, but i am doing it this way and im sure there is a more effective way
My from
<div class="row">
<div class="col-lg-8">
<h5><?php echo Lang::get('auth.signup') ?></h5>
<div class="page-divider"></div>
<form name="myForm" ng-controller="formController" ng-submit="signupPost()" class="form-horizontal" novalidate>
<div class="form-group">
<label for="first_name" class="col-lg-3 control-label"><?php echo Lang::get('form.first_name') ?></label>
<div class="col-lg-8">
<input type="text" name="first_name" ng-model="formData.first_name" id="first_name" class="form-control input-small">
<span class="help-block" ng-show="errors['first_name'][0]">{{ errors['first_name'][0] }}</span>
</div>
</div>
<div class="form-group">
<label for="last_name" class="col-lg-3 control-label"><?php echo Lang::get('form.last_name') ?></label>
<div class="col-lg-8">
<input type="text" name="last_name" ng-model="formData.last_name" id="last_name" class="form-control input-small">
<span class="help-block" ng-show="errors['last_name'][0]">{{ errors['last_name'][0] }}</span>
</div>
</div>
<div class="form-group">
<label for="username" class="col-lg-3 control-label"><?php echo Lang::get('form.username') ?></label>
<div class="col-lg-8">
<input type="text" name="username" ng-model="formData.username" id="username" class="form-control input-small">
<span class="help-block" ng-show="errors['username'][0]">{{ errors['username'][0] }}</span>
</div>
</div>
<input type="submit" value="<?php echo Lang::get('auth.signup') ?>" class="btn btn-primary">
</form>
</div>
</div>
Angular controller
function formController($scope, $http)
{
$scope.formData = {};
$scope.signupPost = function() {
$http.post('signup', $scope.formData).success(function(data){
if(data.msg == "success")
{
$location.path(data.redirect)
}
else
{
$scope.errors = data.error_msg;
}
});
}
}
And the json what laravel retunrs if the form validation fails
$messages = $val->messages();
$data = array(
'error_msg' => array(
'first_name' => $messages->get('first_name'),
'last_name' => $messages->get('last_name'),
'username' => $messages->get('username'),
'profession' => $messages->get('profession'),
'location' => $messages->get('location'),
'email' => $messages->get('email'),
'gender' => $messages->get('gender'),
'password' => $messages->get('password'),
'dob' => $messages->get('dob'),
'confirm_password' => $messages->get('confirm_password'),
));
}
return Response::json($data);
I tried a few variations and currently it works like this in the form, show the form validation error messages if its set, this way errors['first_name'][0] for all fields.
My question is, is there a more effective way doing this? If someone could show me an example would be great
Thank you
Well you can do something like this
<div class="col-lg-8">
<input type="text" name="first_name" ng-model="formData.first_name" id="first_name" class="form-control input-small">
<span class="help-block" ng-show="errors.first_name[0]">{{ errors.first_name.toString()}}</span>
</div>
The toString() function would concatenate the string array using , as separator. If you want customization of the content your option are to
Write a javascript function, that takes and returns some formatted data.
More angular way would be to do a ng-repeat on the errors.
<span ng-repeat='error in errors.first_name'>
{{error}}
</span>
I know the question is old but I want to share my awesome new angular directive, I made a project on Github and I think that it just rocks compare to whatever is/was available...I based myself on the excellent Laravel PHP Framework and made it available under Angular... Since I use very similar approach compare to Laravel but using an AngularJS Directive (my own directive), you will find my implementation very easy to follow.
<!-- example 1 -->
<label for="input1">Simle Integer</label>
<input type="text" validation="integer|required" ng-model="form1.input1" name="input1" />
<span class="validation text-danger"></span>
<!-- example 2 -->
<label for="input2">Alphanumeric + Exact(3) + required</label>
<input type="text" validation="alpha|exact_len:3|required" ng-model="form1.input2" name="input2" />
<span class="validation text-danger"></span>
So I can define whatever amount of validation rules which I want in a simple directive validation="min_len:2|max_len:10|required|integer" and the error message will always display in the next <span> Don't you guys like it already? 1 line of code for your input, 1 line of code for the error display, you can't be simpler than that...oh and I even support your custom Regex if you want to add :)
No more clustered Form with 10 lines of code for 1 input when the only thing you need is 2 lines, no more, even for an input with 5 validators on it. And don't worry about the form not becoming invalid, I took care of that as well, it's all handled the good way.
Take a look at my Github project Angular-Validation and spread the word =)
EDIT
To make an even more smoother user experience, I added validation on timer. The concept is simple, don't bother the user while he's busy typing but do validate if he makes a pause or change input (onBlur)... Love it!!!
You can even customize the timer as per your liking, I've decided to default it to 1 second within the directive but if you want to customize you can call as for example typing-limit="5000" to make a 5 sec. timeout. Full example:
<input type="text" validation="integer|required" typing-limit="5000" ng-model="form1.input1" name="input1" />
<span class="validation text-danger"></span>
DEMO
Added a live demo on Plunker

Categories