only show future appointments PHP - php

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()

Related

How to debug "Parse error: syntax error, unexpected '<', expecting ']'" in Laravel?

I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc). This form sends the data to a "orders" table.
I'm now getting the following error in orders.blade.php:
Parse error: syntax error, unexpected '<', expecting ']'
I don't understand why I'm getting '<'; I don't see this in the code!
This is my code so far:
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders($modelesc=null) {
$cars = DB::table('cars')->where('Model', '=', '$modelesc');
return view('orders', compact('cars'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
{!! Form::open(array('action' => 'index', 'method' => 'POST')) !!}
{!! Form::text('Model', $car->Model) !!}
{!! Form::hidden(users_id, Auth::user()->id) !!}
{!! Form::hidden(Fabrication_date, date(Y-m-d)) !!}
{!! Form::select('Colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{!! Form::hidden(Order_status_id, '1' !!}
{!! Form::close() !!}
This is the structure of the orders table. The *_id fields come from other tables, and I want to fill some values of the forms with the relevant entry (id, users_id, Model, Fabrication_date, Colour_id, Order_status_id).
Firstly you need to wrap the Form names within quotes, the Order Status ID was also missing a closing bracket:
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::hidden('Order_status_id', '1') !!}
Next, if $colours is a collection you can do the following in Laravel 5.4 (I'm unsure which version you're using)
{!! Form::select('Colour', $colours->pluck('Colour')) !!}
If you're on Laravel 5.1 or prior, you'll do the following:
{!! Form::select('Colour', $colours->lists('Colour')) !!}
This is because the lists method was removed in 5.2.
#pseudoanime is also correct with his answer, the database call needs the get method adding
try changing $cars = DB::table('cars')->where('Model', '=', '$modelesc');
$cars = DB::table('cars')->where('Model', '=', $modelesc)->get();
Your orders method in your CarController doesn't set $cars to what you'd think; without calling get(), it'll be an instance of Builder instead of an array! Furthermore, you're only fetching cars where the model equals the string "$modelesc" instead of the value of $modelesc.
Furthermore, in your orders.blade.php file, you seem to reference a $colours variable, but that doesn't get passed down in your CarController.
Change your orders method to the one below:
public function orders($modelesc = null)
{
$cars = DB::table('cars')
->where('Model', $modelesc)
->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
There is also an issue with your Form::select call in your orders.blade.php file; the Blade template directives (#foreach, #endforeach) are only for the "HTML" portion of the Blade file. Since we called pluck() on the Collection of colours in the CarController, we can simply pass in $colours for that method.
{{-- Incorrect --}}
{!! Form::select('Colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{{-- Correct --}}
{!! Form::select('Colour', $colours) !!}
You also appear to be using constants in your Form builders (instead of strings with quotes); these should be strings only.
{!! Form::open(['action' => 'index', 'method' => 'POST']) !!}
{!! Form::text('Model', $car->Model) !!}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::close() !!}

Laravel Not posting to database

There is a bug in the code somewhere and I cant seem to find it , any help would be great. Its a simple form that stores to a DB.
The app is using laravel 5.2 and all that is needed is to collect data. when the submit button is hit on the form nothing!
route
Route::resource('/form' , 'PagesController');
controller only needs to indes, create and store , thats all the app needs to do.
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use Data;
use App\Http\Requests\DataRequest;
use Carbon\Carbon;
class PagesController extends Controller
{
//Display Index
public function index()
{
return view ('welcome');
}
public function create()
{
return view ('create');
}
//Store Articles from form
public function store(DataRequest $request)
{
Data::create($request->all());
return redirect('create')->with('message' , 'Form submitted');
}
}
model uses a simple protected fields list and carbon to store timestamps
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Http\Requests\DataRequest;
use Carbon\Carbon;
class Data extends Model
{
protected $fillable = [
'name',
'email',
'phone',
'company',
'addcomments',
'published_at',
];
protected $dates = ['published_at'];
//Get all published articles by date
public function scopePublished($query)
{
$query->where('published_at' , '<=' , Carbon::now());
}
//Get all unpublished or future articles
public function scopeUnpublished($query)
{
$query->where('published_at' , '>=' , Carbon::now());
}
// Set form to publish articles with a time and date in the Published_at field
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d' , $date);
}
}
form
{!! Form::open(['url' => 'form']) !!}
<div class="form-group">
{!! Form::label('name' , 'Name:') !!}
{!! Form::text('name', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email' , 'EMail:') !!}
{!! Form::text('email', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone' , 'Phone Number:') !!}
{!! Form::text('phone', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('company' , 'Company:') !!}
{!! Form::text('company', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('addcomments' , 'Additional Comments:') !!}
{!! Form::textarea('addcomments', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Submit Now', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
So the issue was what was mentioned in the comments
use DATA;
to
use App\DATA;
and a mixture of a few little front end tweaks
all sorted

two or more forms in one view using one post request

I have only two forms for the moment in my view, they are both post method I tried to solve it like this
route
Route::post('view', function(){
if(Input::has('form1')){
'NameController#method1';
} elseif (Input::has('form2')){
'NameController#method2';
}
});
view
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', $trans->text) !!}
{!! Form::submit('Submit', array('name' => 'form1' )) !!}
{!! Form::close() !!}
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', 'text') !!}
{!! Form::submit('Submit', array('name' => 'form2')) !!}
{!! Form::close() !!}
And it throw's out this error
syntax error, unexpected ''ConfigurationController#title' (T_CONSTANT_ENCAPSED_STRING)
it was error in coding I corrected it but it won't do what I wish it simply returns blank screen it doesn't cycle trough controllers
I modified the code(removed return and closed the route)
What you want to do is create a method someMethodName in the NameController and in there
public function someMethodName()
{
if(Input::has('form1')){
$this->method1();
} elseif (Input::has('form2')){
$this->method2();
}
}
then replace all the route stuff with
Route::post('view', 'NameController#someMethodName')

Laravel form error message

I created a form for myself and it was working perfect. I did some rules like: if the user haven't wrote anything in the form or haven't wrote enough, give me some error message. This was working while used the 'former' package instead of the normally used 'form' package. Now I changed my form from "Former" to "Form" and now my error message are gone.. Well my form works and the rules too. If I don't write anything in my form or not at least 3 characters in the title/content section, it should redirect me with the errors I need. This works, but without the error message.
Here is my form:
#extends('master')
#section('content')
{!! Form::open(array('action' => 'Test\\TestController#store')) !!}
<div class="form-group">
{!! Form::label('thread', 'Title:') !!}
{!! Form::text('thread', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('content', 'Body:') !!}
{!! Form::textarea('content', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add Thread', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#stop
My Controller#store function:
public function store(StoreRequest $request){
Thread::create($request->all());
return redirect(action('Test\\TestController#index'));
}
my Model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
and now the rules ( StoreRequest.php ) :
<?php
namespace App\Http\Requests\Store;
use App\Http\Requests\Request;
class StoreRequest extends Request {
public function rules() {
return [
'thread' => 'required|min:3',
'content' => 'required|min:3'
];
}
public function authorize() {
return true;
}
}
The error message was getting automaticly generated. Laravel did that for me. But not anymore.
Does anybody see why?
You can check if the form returned any error with something along these line:
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
Or you could use BootForm and let it handle everything for you

Post Method Not Working in Laravel

Form:
#section('form')
<div class="col-sm-4">
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', 'Enter your name', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('contact', 'Contact Number:') !!}
{!! Form::text('contact', 'Enter your contact number', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('location', 'Location:') !!}
{!! Form::select('location', $locations, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('service', 'Service Required:') !!}
{!! Form::select('service', $services, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Just go Befikr >>', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
routes.php:
<?php
Route::get('/', 'PagesController#index');
Route::post('/', 'PagesController#store');
PagesController.php
<?php namespace App\Http\Controllers;
use App\service_location;
use App\service_type;
use App\service_request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
class PagesController extends Controller {
public function index()
{
$locations = service_location::lists('location_area', 'location_id');
$services = service_type::lists('service_desc', 'service_id');
return view('home.index', compact('locations','services'));
}
public function store()
{
$input = Request::all();
return $input;
}
}
Unfortunately, the code neither seems to return $input on the screen (implying that its not executing the store() function at all), nor does it throw any error/exception.
Can anyone please let me know why the post method is not resulting in appropriate results here?
Edit 1
I attempted to directly return via the route's inline function, but even that didn't work. Hence, now I'm pretty confident that the post method is not getting triggered at all. This is what I did to verify it:
Route::post('/', function()
{
return 'Hello';
});
Well, I figured the answer to my own question here.
Turns out, that in order to be able to execute a post request, you need to explicitly deploy a PHP server as:
php -s localhost:<AnyFreePort> -t <YourRootDirectory>
Whereas when I attempted to run it the first time through XAMPP, I was attempting to run the application out of sheer laziness, directly as:
http://localhost/myproject/public
which worked fine for a get request.
Even I had same issue, but your solution wasn't helpful for me since I was using Apache server instead of built in PHP server
then I found this solution which says just enter space to form action, weird issue and solution but it worked for me...
Eg.
{!! Form::open(array('url' => ' ', 'method' => 'post')) !!}

Categories