I've got my relationships setup where my projects are related to clients, what I want to do is to be able to select a select in my create project view via drop down list.
This can be achieved as so:
{{ Form::select('client', array('s' => 's', 'm' => 'm'), 's');}}
However I'm not sure how I would do this dynamically from the data I am retrieving from the database, the code I have so far is:
{{ Form::open(array('action' => 'ProjectController#store', 'id' => 'createproject')) }}
<div class="form-group">
<? $projects = Auth::user()->clients; ?>
#if (Auth::check())
#if (count($projects) > 0)
#foreach ($projects as $client)
{{ Form::select('client', array('client_one' => 'client_one', 'client_two' => 'client_two'), 'client_one');}}
#endforeach
#else
<h3>You have no projects click here to create a project</h3>
#endif
#endif
</div>
<div class="form-group">
{{ Form::label('project_name', 'Project Name') }}
{{ Form::text('project_name', Input::old('project_name'), array('class' => 'form-control')) }}
</div>
Can anyone point me into the direction of how I can populate this select field dynamically?
Thanks in advance!
Doing the data processing in the view is not a good idea. What you should do is prepare the array in the controller and use that array in the view.
In your controller.
$clients = Auth::user()->clients;
$client_selector = array();
foreach($clients as $client) {
$client_selector[$client->name] = $client->name; // I assume name attribute contains client name here
}
return View::make('your.view', array(.., 'client_selector' => $client_selector, ...));
In your view.
#if(count($client_selector)>0)
{{Form::select('client', $client_selector, array_values($client_selector)[0])}}
#endif
I think this is a be better solution:
In your controller:
$clients = Auth::user()->clients->lists('name', 'name');
return View::make('your.view', array(.., 'clients' => $clients, ...));
In your view:
{{ Form::select('client', $clients) }}
Related
I have this code for my form and it works well:
<div class="form-group">
<select class="form-control" id="exampleSelect1">
<option>Assign</option>
#foreach ($projects as $project)
<option>{{ $project->name }} </option>
#endforeach
</select>
</div>
but what I'm trying to do is this with a contact form:
<div class="form-group">
{!! Form::label('name', 'Project') !!}
{!! Form::select (#theforeach with the values :c ) !!}}
</div>
I'm using the use App\Http\Requests\ContactFormRequest; and I have been searching the way of doing it but there is to few examples on google.
Form is part of the Laravel Collective HTML library, you can find the documentation here, specifically you're looking for the Select documentation:
echo Form::select('size', ['L' => 'Large', 'S' => 'Small']);
You have a Collection of Project models, each with a name and (presumably) an id which you need to turn into a key -> value array for the select method which you can do using pluck:
{!! Form::select('project', $projects->pluck('name', 'id')) !!}
Then within your controller you'll be able to find the project that has been selected using find, e.g:
Project::find($request->project);
If you want your select options to works, you need to call it properly,
From controller,
// Example : This is for single view page
$list_of_options = Products::pluck('name','id');
return view('your_view_name',compact('list_of_options'));
// Example : If you want select dropdown in all page ( within the controller views) then,
use Illuminate\Support\Facades\View;
public function __construct(){
View::share('list_of_options',Products::pluck('name','id'));
}
Now in blade,
{{ dd($list_of_options); }} // Check if the values are comming in proper formate
{!! Form::select('name_of_the_select', $list_of_options, null ,[
'class' => 'form-control',
'id' => 'name_of_the_select'
]);
!!}
Here is the button with <i> or <span> inside of it :-
{{ Form::button(
'<span class="fa fa-play fa-1x"></span>',
[
'class'=>'btn btn-info',
'type'=>'button'
])
}}
From your question ( UPDATED )
<div class="form-group">
{!! Form::label('exampleSelect1', 'Project') !!}
{!! Form::select('projects', $projects, null ,[
'class' => 'form-control',
'id' => 'exampleSelect1',
'placeholder' => 'Please select project'
]);
!!}
</div>
I hope this helps. :)
Try this
<div class="form-group">
{!! Form::label('project', 'Project') !!}
{!! Form::select ('project', $projects->pluck('name')) !!}}
</div>
See docs https://laravel.com/docs/4.2/html#drop-down-lists
<div class="form-group">
{!! Form::label('project', 'Project') !!}
{!! Form::select ('project', $projects->pluck('name', 'id')) !!}}
</div>
Ok, I guess the right way to start is: HALP!
So to start I will give you an introduction to my Problem:
I am working on a booking system for Vacation-Rental-Homes.
The user is creating new Bookings via a Form where he can select a guest, an house (both via dropdowns), a beginning date for the reservation and an ending date.
Problem:
I want that in the datepickers, the dates where the selected house is booked will be disabled. For solving this I have made an Helper-Function that is returning me all Bookings for a House.
<?php
namespace App\Http\Controllers;
use App\House;
use Illuminate\Http\Request;
use DB;
use App\Quotation;
use DateInterval;
use DatePeriod;
use DateIntercal;
class HelperFunctions extends Controller
{
public static function GetBlockedTimes(House $house)
{
$Bookings = DB::table('bookings')->where([
['House_id', $house->id],
])->get();
$BlockedTimes = [];
foreach($Bookings as $booking) {
for($i = strtotime($booking->From); $i <= strtotime($booking->Until); $i += 86400) {
array_push($BlockedTimes, date('Y-m-d',$i));
}
}
return $BlockedTimes;
}
}
Also I have stumbled upon This Datepicker Framemork, but to be honest, I (as a C# developer making a Laravel Thingy for the first time) have no Idea how I can implement it into my Laravel Collective Forms. Maybe you can help me there too.
For making a page where the User can create Bookings I have made the following View, which is fed by the Controller with all houses and Guests.
#extends('layouts.app')
#section('content')
<h1>Create Season</h1>
{!! Form::open(['action' => 'SeasonController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{ Form::label('SeasonName','Season Name') }}
{{ Form::text('SeasonName', '', ['class' => 'form-control', 'placeholder' => 'Season Name'] ) }}
</div>
<div class="form-group">
{{ Form::label('From','From') }}
#php
//HALP!!!!!
use App\Http\Controllers\HelperFunctions;
use App\House;
$house = House::find('1'); //This '1' should get replaced by the id selected in House_id
$home = HelperFunctions::GetBlockedTimes($house);
$getit = '[';
if($home == null){
echo "ERROR!!!";}
else{
foreach($home as $date){
$getit.='"'.$date.'"'.', ';
}
}
$getit .=']';
#endphp
{!! Form::date('From', \Carbon\Carbon::now()) !!} {{--In here dates should get disabled according to the select down below--}}
</div>
<div class="form-group">
{{ Form::label('Until','Until') }}
{!! Form::date('Until', \Carbon\Carbon::now()) !!} {{--In here dates should get disabled according to the select down below--}}
</div>
<div class="form-group">
{{ Form::label('Price','Price') }}
{!! Form::number('Price',null,['class' => 'form-control','step'=>'any']) !!}
</div>
<div class="form-group">
{{ Form::label('House_id','House') }}
{!! Form::select('House_id', $houses, null , ['placeholder' => 'Pick a house...',]) !!}{{--This select should change the disabled dates of the datepickers above.--}}
</div>
{{ Form::submit('Submit',['class' => 'btn btn-success']) }}
{!! Form::close() !!}
#endsection
If you need any other informations I am keen to help you!
Thank you for your service!
How would I go about searching my database using a keyword that was inputted by the user, and then displaying the most relevant results on another page?
The code I supplied down below just gives my a blank page, I first assumed this was simply because I had nothing in my database but when I checked there was an input with that keyword.
My search function:
public function searchUsers()
{
$searchWord = Input::get('searchBox');
return View::make('user.search')->with('users', User::where('fullname', 'LIKE', '%'. $searchWord .'%'));
}
The user inputs the word in this blade form:
{{ Form::open(array('url' => secure_url('user/searchUsers'), 'class'=>'form-group has-feedback')) }}
<h1 style="font-size:55px; text-align:center;">Search for Friendship!</h1>
<br>
{{ Form::text('searchBox', $value = null, array('placeholder' => 'Search', 'class'=> 'form-control input-lg','autofocus' => 'autofocus')) }}
{{ Form::close() }}
How I display my results:
#foreach ($users as $user)
<div id="commentPanel"class="panel panel-default">
<div class="panel-heading">{{ $user->fullname }}</div>
<div class="panel-body">
<p id="commentP">{{ $user->email }}</p>
</div>
</div>
Well you are not returning the users from database, you just set where condition to the Eloquent object which simply returns a query object. Change your return statement to be like this:
return View::make('user.search')
->with(
'users',
User::where('fullname', 'LIKE', '%'. $searchWord .'%')->get()
);
I am trying to update information in my database with Laravel. Not sure what I am doing wrong but I can't seem to find where the problem is. Here is the code for my edit page (This is the page where I would edit information taken from my DB).
{{ Form::open(['url'=>'portfolio/update']) }}
<div>
{{ Form::label('portfolio_title', 'Portfolio Title:') }}
{{ Form::text('portfolio_title',$work->portfolio_title) }}
{{ $errors->first('portfolio_title','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_description', 'Portfolio Description') }}<br>
{{ Form::textarea('portfolio_description', $work->portfolio_description, ['size' => '50x5']) }}
{{ $errors->first('portfolio_description','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_content', 'Portfolio Content') }}<br>
{{ Form::textarea('portfolio_content', $work->portfolio_content, ['size' => '50x5']) }}
{{ $errors->first('portfolio_content','<span class="error">:message</span>') }}
</div>
{{ Form::hidden('id',$work->id) }}
<div>
{{ Form::submit('Update Work') }}
</div>
{{ Form::close() }}
I have a controller called PortfolioController that will save info to database and what not.
public function edit($work_title){
$work = Portfolio::wherePortfolio_title($work_title)->first();
return View::make('portfolio/edit', ['work' => $work]);
}
public function update(){
$id = Input::get('id');
$input = Input::except('id');
if( !$this->portfolio->fill($input)->isValid()){
return Redirect::back()->withInput()->withErrors($this->portfolio->errors);
}
$work = Portfolio::find($id);
$work->portfolio_title = Input::get('id');
$work->save();
}
Here is my route that I am working with:
Route::resource('portfolio','PortfolioController');
Route::post('portfolio/update','PortfolioController#update');
I am able to get the form populated with the correct information but when i change something like the title and click update, the page reloads but does not save in the DB. Sometimes I will get an MethodNotAllowedHttpException error. This has been pretty frustrating for me so any help will be greatly appreciated.
Why don't you just actually use your resource route?
First, remove the portfolio/update route. You don't need it.
Then change your Form::open to this:
{{ Form::open(['route' => ['portfolio.update', $work->portfolio_title], 'method' => 'put']) }}
This way you target the update method in your RESTful controller.
Finally change that to use the portfolio_title as identifier and you should be able to remove the hidden id field from your form.
public function update($work_title){}
Please take a look at:
RESTful controllers
Opening a form
I wish to make a select box containing some categories of items. This select box should have the data from database.
so first i created my blade
admin.blade.php :
<h2>Add Category</h2>
{{ HTML::ul($errors->all(), array('class'=>'errors')) }}
{{ Form::open(array( 'url'=>'admin/category/add', $title="Admin Control Panel")) }}
<p>
{{ Form::label('Category Name:') }}
{{ Form::text('category_name', Input::old('category_name')) }}
</p>
<p>
{{ Form::select('parent_category', array('' => 'Select Category', '0 ' => 'Main Category',)) }}
{{ Form::label('Parent Category:') }}
</p>
<p>
{{ Form::submit('Submit', array('name' => 'save')) }}
</p>
{{ Form::close() }}
and then i created my controller:
class CategoryController extends BaseController
{
public function add()
{
$category_list = CategoryModel::select();
return View::make('admin')->with('category_list', $category_list);
}
i need to display this array in my select box. but i don't know how to do that. please suggest a proper way!
thanks!
return View::make('admin')->with('category_list', $category_list);
What this is saying is create a response and return it to the client. The response is going to be generated using the blade template admin, and we're going to pass the contents of the variable $category_list to the view which will be able to reference it internally as $category_list (the first string parameter to the with function).
Therefore, inside your view if you want to access your category list you can do this:
{{ $category_list->title }}
Assuming, of course, that $category_list here is a single model with an attribute title. If $category_list is a collection of models (or an array), you can just use foreach:
#foreach($category_list as $category)
{{ $category->title }}
#endforeach
If what you're doing is trying to build a select list based off of the entries in your database for a given model, you can simply do this:
{{ Form::select('category', CategoryModel::list('title', 'id')) }}
Of course, you can generate that array in your controller and pass it down as you are doing now, too.