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!
Related
I try to make a simple form.
When i want to make form for edit the data, it always pop up error like that.
This is my edit form
#section('title', 'Form Edit Mata Kuliah')
#section('header')
<h1 class="page-header"><b>Form Mata Kuliah</b></h1>
{{ Form::open(array('url' => 'updatekuliah')) }}
#endsection
#section('navigation')
<p>Edit Data Kuliah</p>
#endsection
#section('content')
#foreach($datakuliah as $p)
<div class="form-group row">
<div class="col-md-4">
{{ Form::label('kodematakuliah', 'Kode Mata Kuliah') }}
</div>
<div class="col-md">
{{ Form::label('',':') }}
</div>
<div class="col-md-7">
{{ Form::text('kodematakuliah', $p->kodematakuliah_db, array('class' => 'form-control', 'placeholder' => 'Masukkan Kode Mata Kuliah')) }}
</div>
</div>
<br>
{{ Form::submit('Kirim', array('class' => 'form-control')) }}
{{ Form::close() }}
#endforeach
#endsection
This is my controller
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Pertemuan6Controller extends Controller
{
public function tampilkanformedit($kodeedit)
{
$varkuliah = DB::table('tabel_pertemuan6')->where('kodematakuliah_db', $kodeedit)->get();
return view("pertemuan6.formedit", ['datakuliah' => $varkuliah]);
}
}
?>
This is my route
Route::get('/formkuliah', [Pertemuan6Controller::class, 'tampilkanformtambah']);
Route::get('/editkuliah/{kodeedit}', [Pertemuan6Controller::class, 'tampilkanformedit']);
Route::post('/insertkuliah', [Pertemuan6Controller::class, 'insertdatakuliah']);
Route::post('/updatekuliah', [Pertemuan6Controller::class, 'updatedatakuliah']);
Route::get('/deletemhs/{nimhapus}', [Pertemuan6Controller::class, 'deletedatamahasiswa']);
Anyone know how to handle that error? Thanks before
You can pass only array and collections to foreach loop. Actually get method should return empty array if it doesn't have any value. If I'm not wrong, your query passes NULL. That's why you are getting this result. Check the value before passing it to the view.
This should return collection and not null:
`$results = DB::table('tabel_pertemuan6')->where('kodematakuliah_db',` `$kodeedit)->get();`
Do
var_dump($results);
or
dd($results);
and check if you get expected values. Expected is collection. Check here for more info here
I am trying to learn PHP by myself so i decided to create a little health appointment manager.
I tried to do a filter in the Controller so it only shows the future appointments in the index view but I can not find the right way to do it:
HereĀ“s is what I wrote down in my Controller and my view:
Controller:
{
$citas = Cita::all();
$fecha_hora = get('fecha_hora');
if($fecha_hora>date_default_timezone_get()){
return view('citas/index',['citas'=>$citas]);
}
}
and my index view:
#include('flash::message')
{!! Form::open(['route' => 'citas.create', 'method' => 'get']) !!}
{!! Form::submit('Crear cita', ['class'=> 'btn btn-primary'])!!}
{!! Form::close() !!}
{!! Form::open(['route' => 'citas.index', 'method'=>'get']) !!}
<div class="form-group">
<br>
{!! Form::submit('Citas pasadas',['class'=>'btn btn-default btn-sm']) !!}
{!! Form::close() !!}
{!! Form::open(['route' => 'citas.index', 'method' => 'get']) !!}
{!! Form::submit('Todas las citas', ['class'=> 'btn btn-link btn-sm pull-right'])!!}
{!! Form::close() !!}
<br><br>
What i want is to only show the future appointments in the main view but also have a tab "Citas pasadas" (Past appointments in english) where i can see only past appointments.
Maybe you can prepare scopes in your Modelfile. Something like this.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Cita extends Model
{
public function scopeUpcoming($query) {
return $query->where('start_time', '>', date('Y-m-d H:m:s'))->orderBy('start_time', 'desc');
}
public function scopePrevious($query) {
return $query->where('start_time', '<', date('Y-m-d H:m:s'))->orderBy('start_time', 'desc');
}
}
Then, access them like Cita::upcoming() or Cita::previous()
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>
I want to echo the selected value when I edit a specific resource in my table. When I edit the specific resource it should display the current data in my dropdown but in the real scenario it displays the first one on the list which is wrong. So how can I echo the selected value in the options of the dropdown using blade in laravel?
Here is some sample of my code in the view below
<!-- Shows Field -->
<div class="form-group col-sm-6">
{!! Form::label('show_id', 'Shows:') !!}
{!! Form::select('show_id', $shows, $shows, ['class' => 'form-control input-md','required'])!!}
</div>
{{-- {{ $channelshows->channel->name }} --}}
<!-- Client Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('channel_id', 'Channels:') !!}
{!! Form::select('channel_id', $channel, $channel, ['class' => 'form-control input-md','required'])!!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
and here is the code in my controller below.
public function edit($id)
{
$channelshows = $this->channelshowsRepository->findWithoutFail($id);
$shows = Show::pluck('name', 'id');
$channel = Channel::pluck('name', 'id');
if (empty($channelshows)) {
Flash::error('Assigned show not found');
return redirect(route('admin.channelshows.index'));
}
return view('admin-dashboard.channelshows.edit', compact('shows', $shows), compact('channel', $channel))->with('channelshows', $channelshows);
}
Everything here works fine even if I updated the specific resource. I just want to auto populate or select the current value of the resource that I will update because when I edit the specific resource it shows the first one on the list.
Am I going to use an #if statement in blade? But how can I do it using the blade template in my select form. Can somebody help me?
Appreciate if someone can help.
Thanks in advance.
Here is an example:
Open form:
{{ Form::model($service, array('route' => array('services.update', $service->id))) }}
Select form field:
<div class="form-group">
{{ Form::label('Related Agreement') }}
{{ Form::select('agreement', $agreementsList, null, array('class'=>'form-control', 'placeholder'=>'Please select ...')) }}
</div>
In Controller:
$agreementsList = Agreement::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');
(Include this when passing data to your view)
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) }}