Laravel 5 defining old input on select - php

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>

Related

I want to show roles related to users in selection option using laravel

I am trying to show roles which is related to user but unfortunately userRoles are not showing related to user in selection option, please help me how can I match that thanks.
Note :- I am using spaite laravel permission docs
controller
public function edit(User $user)
{
$data = [
'isEdit' => true,
'user' => $user,
'roles' => Role::where('guard_name', '=', 'web')->select(['id', 'name'])->get(),
'userRole' => $user->getRoleNames(),
// i am getting userRole in array related to user
// ['employe']
];
// return $data['userRole'];
return view('cms.user_management.add-user', $data);
}
html view
<div class="col-md-6">
<div class="form-group">
<label>Role <span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled >please
select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ $userRole ==
$item->name ? ' selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}
</span>
</div>
Try the following changes in your HTML
If $userRole is array than check if $item->name exists in array, using in_array.
'userRole' => $user->getRoleNames()->toArray(),
html view
{{ in_array($item->name, $userRole) ? 'selected' : '' }}
<div class="col-md-6">
<div class="form-group">
<label>Role<span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled>please select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ in_array($item->name,$userRole) ? 'selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}</span>
</div>
you can use this to get the roles asssociated with current user or
Auth::user()->roles->pluck('name');
for any specific user roles.
User::find($id)->roles->pluck('name')

Displaying a category when editing a post in laravel

I am creating a blog in Laravel. I have two tables - posts and categories.
When i want to edit one post, i have a problem with categories. My post controller is sending categories to the view, i can see them while editing, but i don't know how to set the current category in the select option.
The tables are conected:
public function posts(){
return $this->hasMany('App\Post');
}
public function category(){
return $this->belongsTo('App\Category');
}
Controller:
public function edit(Post $post)
{
$categories = Category::all();
return view('admin.posts.edit', compact('post'))->withCategories($categories);
}
View:
<form action="{{ route('posts.update', ["post" => $post->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
#csrf
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Naslov</label>
<div class="col-md-4">
<input type="text" name='title' value='{{ old("title", $post->title) }}' class="form-control">
#if($errors->has('title'))
<div class='text text-danger'>
{{ $errors->first('title') }}
</div>
#endif
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Kategorija</label>
<div class="col-md-4">
<select class="form-control" name="category_id">
#if(count($categories) > 0)
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
#endif
</select>
#if($errors->has('category_id'))
<div class='text text-danger'>
{{ $errors->first('category_id') }}
</div>
#endif
</div>
</div>
How can i make the category display the current category of the selected post in the option select like i have for title? value='{{ old("title", $post->title) }}'
To show the currently associated Category, you can set a default "selected" option based on the $post->category_id:
<option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
This a simple ternary that sets the selected attribute of one of the options based on the old() value, or, if old() is not set, then based on the value of $post->category_id
What you are looking for is the selected property of the <option> element.
#forelse ($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == old('category_id', $post->category_id) ? 'selected' : '' }}>
{{ $category->name }}
</option>
#endforelse
A minor change as well in the in you blade template to make use of the #forelse directive for less code.
You could also change your controller code a bit:
return view('admin.posts.edit', compact('post', 'categories'));
Further the laravelcollective/html package might help you a lot in creating forms.

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']) }}

Multiple values in laravel post form (dropdown)

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>

How to show categories in product creating page in Laravel

I have this form in the Laravel admin backend which adds new products to database.
productCreate.blade.php
{{ Form::open(['files' => true]) }}
<div class="form-group">
<label for="title" class="control-block">Product title:</label>
{{ Form::text('title', null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
<label for="title" class="control-block">Product description:</label>
{{ Form::textarea('description', null, ['class' => 'form-control']) }}
<span class="help-block">HTML is allowed.</span>
</div>
<div class="form-group">
<label for="title" class="control-block">Product price:</label>
{{ Form::text('price', null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
<label for="title" class="control-block">Assign Product to Category:</label>
<select class="form-control">
<option value="one">Category 1</option>
<option value="two">Category 2</option>
<option value="three">Category 3</option>
<option value="four">Category 4</option>
<option value="five">Category 5</option>
</select>
</div>
<div class="form-group">
<label for="title" class="control-block">Product image:</label>
{{ Form::file('image', ['class' => 'form-control']) }}
</div>
<hr />
<button type="submit" class="btn btn-primary">Create new product</button>
{{ Form::close() }}
My question is how to show this select/options values from database. I want to be able to assign products to categories. Where I should make query and how to add them here in the view?
In admincontroller i have functions for product creation which load the view
public function productsCreate() {
return View::make('site.admin.products_create');
}
and I tried to make it like this
public function productsCreate() {
$categories = Categories::paginate(15);
return View::make('site.admin.products_create', [
'categories' => $categories
]);
//return View::make('site.admin.products_create');
}
but then I got message
production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Cannot redeclare AdminController::productsCreate() ...
So how exactly I can query database table categories and show categories in the form?
May be because I already have categories selection in admin controller?
public function categories() {
$categories = Categories::all();
return View::make('site.admin.categories', [
'categories' => $categories
]);
}
public function productsCreate() {
// $categories = Categories::paginate(15);
return View::make('site.admin.products_create', [
'categories' => $categories
]);
//return View::make('site.admin.products_create');
}
You pass the categories in theproductsCreate function, and you must remove the old productsCreate function, you declared it twice :
public function productsCreate() {
$categories = Categories::get();
return View::make('site.admin.products_create', [
'categories' => $categories
]);
}
For the html :
<div class="form-group">
<label for="title" class="control-block">Assign Product to Category:</label>
<select class="form-control">
#foreach($categories as $categorie)
<option value="{{ $categorie->id }}">{{ $categorie->name }}</option>
#endforeach
</select>
I hope that will help you.

Categories