Multiple values in laravel post form (dropdown) - php

Good day fellow programmers,
I have been frustrated all day because I have to make a submit form for an hour register app I have to create for the company I'm an intern at.
So basically what has to be done: I have made a submit form with multiple values, I got asked to make a dropdown selection in which I can put the values of "company name" and "task name". (tasks are connected to the company name). So basically what i'm wondering is how I can put 2 values in one dropdown menu. Having the layout of "$company name - $task name"
create.blade.php
{!! Form::open(['url' => 'hoursregistrations/create','files'=>true]) !!}
<div class="form-group" hidden>
{!! Form::label('user_id', 'User ID: ') !!}
{!! Form::text('user_id', $authenticated, null, ['class' => 'form-control']) !!}
</div>
<select name="">
#foreach($items as $item)
<option value="{{ $item->id }}">
{{ $item->company->name }} - {{ $item->name }}
</option>
#endforeach
</select>
<div class="form-group">
{!! Form::label('note', 'Notitie: ') !!}
{!! Form::text('note', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('date', 'Datum: ') !!}
{!! Form::date('date', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('hours', 'Uren: (uren-minuten) ') !!}
{!! Form::time('hours', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<a class="btn btn-danger" href="{{ route('hoursregistrations.index') }}">
#lang('button.cancel')
</a>
<button type="submit" class="btn btn-success">
#lang('button.save')
</button>
</div>
</div>
{!! Form::close() !!}
In this code snippet the company variable is $project_id and the task id is $subproject_id.
Also the controller function to redirect the data to the create view
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
$authenticated = Sentinel::getUser()->id;
$activity_name = Subproject::pluck('id');
$items = array(
'company_name' => Company::where('status','client')->pluck('company_name', 'id'),
'subproject_id' => Subproject::pluck('title', 'id')
);
//dd($items);
return view('hoursregistrations.create', compact('subproject_id', 'authenticated',
'company_name', 'activity_name', 'items'));
}
Conclusion: How can I combine the $project_id and $subproject_id into ONE dropdown item.
p.s I'm sorry if it sounds vague I am bad at explaining things

Do you have to use The Form Facade? Although this may not be helpfull to you, you can write it as plain HTML with Blade's Syntax:
<select name="">
#foreach($tasks as $task)
<option value="{{ $task->id }}">
{{ $task->company->name }} - {{ $task->name }}
</option>
#endforeach
</select>

Related

How to use Select tags in Laravel Forms

For some reason the value is not outputting when I press submit. There is always an error it basically says there is no data.
<div class="form-group">
<div class="">
<label for="category">Size</label>
<br>
<select name="category" class="form-control">
#foreach($size as $s)
<option value="{{ $s->id }}">{{ $s->size }}</option>
#endforeach
</select>
</div>
</div>
just using pluck and map into Form select
in your controller
$model = Model::pluck('size', 'id')->toArray();
return view('view', compact('model'))
in view
{{ Form::select('category', $model, null, ['class' => 'form-control']) }}

Laravel 5 On POST Status 302 Found

I'm trying to create new post useing laravel , ajax and s3 , But every time i try submit the form i get Status Code:302 Found , I Hope really some help me
Firebug
Here in firebug result image
META
<meta name="csrf" value="{{ csrf_token() }}">
VIEW
The form view with csrf token
<div class="col-md-8 col-md-offset-2">
{!! Form::open(array(
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true
)) !!}
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
<label for="cats">Select Category list :</label>
<select class="form-control" id="category" name="category">
<option value="">Select Category</option>
#foreach($category as $cat)
<option value="{{$cat->id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="cats">Select Subcategory list :</label>
<select class="form-control" id="subcategory" name="subcategory">
<option value=>Select Subcategory</option>
<option value=""></option>
</select>
</div>
<div class="form-group">
{!! Form::label('image', 'Upload Image') !!}
{!! Form::file('image', 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('email', 'Your Email: ') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Post Free Ad', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
</div>
CONTROLLER
First valid the requist and than create new email for the user if he did't have and than save the post with the user
public function storePostAds(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'image' => 'required',
'category_id' => 'required',
'subcategory_id' => 'required',
]);
$email = $request['email'];
$title = $request['title'];
$description = $request['description'];
$category = $request['category_id'];
$subcategory = $request['subcategory_id'];
$image = $request->file('image');
$user = User::where('email', $email)->first();
if(!$user){
$user = new User();
$user->email = $email;
$user->save();
}
if($image->isValid()){
$name = $image->getClientOriginalName();
$key = 'images/'.$name;
Storage::disk('s3')->put($key, file_get_contents($image));
}
$post = new Post();
$post->title = $title;
$post->description = $description;
$post->category_id = $category;
$post->subcategory_id = $subcategory;
$post->image = $image;
$user->posts()->save($post);
return redirect('/');
}
Ajax
ajax to get subcategory foreach category after select
(function($){
$('#category').on('change', function(e){
var cat_id = e.target.value;
$.get('/ajax-subcategory?cat_id=' + cat_id, function(data){
var subcategory = $('#subcategory');
subcategory.empty();
$.each(data, function(index, subcatObj){
subcategory.append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');
});
});
});
}(jQuery));
The name of your category and subcategory fields are "category" and "subcategory" but are being referred to as "category_id" and "subcategory_id" respectively in your Controller code.
In case anyone else had my issue, when first creating your fields on the model, your fields may be required. if the controller is not built correctly, instead of errors it returns 302 on the post response. Try checking you enter data for all the form fields before submitting the form.

Merging laravel's Form class with Bootstraps form-group class

I have a form in laravel to edit a post using bootstrap:
#section('content')
<form method="PUT" action="{{ URL::route('post.update', array($post->id)) }}">
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" id="usr" name="title" value="{{$post->title}}">
<br>
<label for="comment">Details:</label>
<textarea class="form-control" rows="5" id="comment" name="detail"
placeholder="Write a new post.">"{{$post->message}}"</textarea>
<input type="hidden" name="id" value="add">
</div>
Cancel
<div class="pull-right">
<input type="submit" class="btn btn-success" value="Update">
</div>
</form>
#stop
It looks like this:
Now, if I use Laravel's Form class, it's much smaller and neater:
{{Form::model($post, array('method' => 'PUT', 'route' => array('post.update', $post->id)))}}
{{ Form::label('title', 'Post Title: ') }}
{{ Form::text('title') }}
{{ $errors->first('title') }}
<p></p>
{{ Form::label('message', 'Post Message: ') }}
{{ Form::text('message') }}
{{ $errors->first('message') }}
<p></p>
{{ Form::submit('Update') }}
{{ Form::close() }}
However, this causes the form to look quite ugly:
So, in short, is there anyway to use Laravel's form class and add boostrap styling/buttons to it? I want the functionality and easy of use of the form class with the visuals of bootstrap.
You need to add the class to each input (here is an exemple)
{{Form::model($post, array('method' => 'PUT', 'route' => array('post.update', $post->id)))}}
{!! Form::label('title','Post Title: ', array('class' => 'pull-left')) !!}
{!! Form::text('title', null, ['class' => 'form-control'])!!}
{!! Form::label('message','Post Message: ', array('class' => 'pull-left')) !!}
{!! Form::textarea('message', null, ['class' => 'form-control'])!!}
<br>
Cancel
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
{!! Form::close() !!}

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>

Laravel 5 defining old input on select

in my controller, I am passing a list of clients to the view
public function edit(Project $project)
{
$clients = Client::select('clientName', 'id')->get();
return View::make('projects.edit', compact('project', 'clients'));
}
Now in my view, I am currently doing this
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
<select class="clientName" name="clientName">
#foreach($clients as $client)
#if (Input::old('clients') == $client->id)
<option value="{{ $client->id }}" selected="selected">{{ $client->clientName }}</option>
#else
<option value="{{ $client->id }}">{{ $client->clientName }}</option>
#endif
#endforeach
</select>
</div>
</div>
What I am trying to do is have the default select option set as the old input. At the moment, the select displays with all the clients, but the old value is not default.
How would I go about making it the default option?
Thanks
Update
I do a alternative way I am trying. In my edit function I do
public function edit(Project $project)
{
$clients = Client::lists('clientName', 'id');
return View::make('projects.edit', compact('project', 'clients'));
}
And then in my view I do
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::select('clientName', $clients, Input::old('clients'), ['class' => 'clientName']) !!}
</div>
</div>
Seem to have the same issue though, the client is not the old client as the default selected option.
Thanks
Your select name is clientName but your old input is looking to a field with the name clients.
The following should work:
<option value="{{ $client->id }}" {!! old('clientName', $project->client->id) == $client->id ? 'selected="selected"' : '' !!}>{{ $client->clientName }}</option>

Categories