Laravel changing route links based on database records - php

I'm building an application that schedules aircraft for flight schools/flying clubs, etc.
I currently have a schedule grid running that allows the user to click on a square to book a reservations starting at that time. That function works perfectly, however, if a single booking exists in the system, for some reason, Laravel deletes the final part of the URL, throwing a 404. Any thoughts on this? I inspected the page and it physically looks like the view is being altered, although the code clearly shows this part. See below:
When there are no records, if a user selects a box on the grid, they are directed to finalize the booking at:
/app/schedule/new/{time}/{date}/{resource}/{instructor?}
For example, a booking starting at 730am today with no instructor using resource #1 (plane 1) is:
/app/schedule/new/0730/20201204/1
However, when there is a booking in the system, all boxes on the grid point to
/app/schedule/new/{time}/{date}
Or, in our example:
/app/schedule/new/0730/20201204 (this is incorrect, there should be a /1 at the end)
Here is my route:
Route::get('/app/schedule/new/{time}/{date}/{resource}/{instructor?}', 'ScheduleController#show_new_booking_form');
And the view href which points to the correct link (instructor is optional):
<a href="/app/schedule/new/0730/{{$raw_date}}/{{$aircraft->aircraft_id}}">
(Edited) Complete steps for all this to happen.
User visits the bookings page with the schedule grid. This is where they would select the link from above. The controller method for that is:
public function show_schedule(Request $request)
{
// Reference Variables
$days = $request->days;
$org_id = Auth::user()->org;
// Information on Dates & Date Incremation
$today = Carbon::now('US/Eastern');
$date = $today->addDays($days);
$schedule_date = $date->isoFormat('dddd MMMM D, YYYY');
$raw_date = $date->isoFormat('YYYYMMDD');
$aircraft = DB::table('aircraft')->where('org', $org_id)->get(); // gets all aircraft for this organization
$bookings = DB::table('bookings')->where('org', $org_id)->get();
// Grid date info
$grid_date = $date->isoFormat('YYYY-MM-DD');
return view('schedule/schedule')->with(compact('raw_date','schedule_date', 'days', 'aircraft','bookings','grid_date'));
}
Then, they are presented with a form where they finish the booking if they are rerouted correctly. The view for the main schedule grid consists of a foreach loop to break the collection of aircraft into individual records:
#foreach ($aircraft as $aircraft)
<tr>
<th scope="row" data-toggle="tooltip" data-placement="right" title="{{$aircraft->type}}">{{ $aircraft->aircraft_n }}</th>
<td class="sh-td"><div class="td-clickable"></div></td>
... this continues for all times until 830pm (each gets it's own td)

Related

How to take the amount from an input and add(+) to the amount table in db? (Laravel)

How to take the amount from an input and add(+) to the amount table in db? (Laravel)
I have this view :
enter image description here
and i have a table named Collaborators (which has an column named istoric_proiecte_sum_suma(made with withSum() function from laravel, in relationship with another table ProjectHistory that contains details about what collaborator works in any project and how much the owner paid him. and if the collab work on multiple projects the owners has to pay him multiple times, and if that, we have multiple inserts in ProjectHistory table with sums and withSum() function makes the relation between those and add the sum and display it to me) which contains the expenses, payments from the owner of the company to a collaborator) like you see in "suma" column in first image (suma = amount). So if i have a - amount the owner of the company needs to pay the collaborator and he pay him from completing that input and press "realizeaza plata" (translated -> make the payment).
So when he press "make payment" -> realizeaza plata, i need to add (+ sign operator) the amount that i typed in "suma" input to the "suma" (amount) column in first image(which is column istoric_proiecte_sum_suma).
My question is how do i resolve this problem because i have no idea. Look what i've tried:
CollaboratorController.php
public function realizeazaPlata(Request $request, Colaboratori $colaborator, $id)
{
$colaboratori1 = Colaboratori::findOrFail($id);
$suma = $request->input('introduSuma');
dd($suma);
DB::update('UPDATE Colaboratori SET istoric_proiecte_sum_suma=? WHERE id=?', [($colaborator->istoric_proiecte_sum_suma + $suma), $colaboratori1->id]);
return back();
}
ColabsView.blade
<form action="{{ url('colaboratori') }}" method="POST">
#csrf
#method('GET')
<td>
<input type="text" name='introduSuma' placeholder="Suma">
<button type="submit" class="btn btn-primary">Realizeaza plata</button>
</td>
</form>
web.php
Route::get('colaboratori', [App\Http\Controllers\ColaboratoriController::class, 'realizeazaPlata'])->name('realizeazaPlata');
Can someone help me please. Thanks
Do you mean you need to convert negative number to positive and then SUM it?
You could use abs() function:
$suma = abs($request->input('introduSuma'));
You should use type=number, to prevent users from entering characters:
<input type="number" name='introduSuma' placeholder="Suma">
You can do it like the following:
$colaboratori1 = Colaboratori::findOrFail($id);
$suma = $request->input('introduSuma');
$colaboratori1->istoric_proiecte_sum_suma = $colaboratori1->istoric_proiecte_sum_suma + $suma;
$colaboratori1->save();

Add row to HTML table with Laravel Livewire - Issue

Firstly I'm new to Laravel and this is my first post. Please forgive me if I'm doing things wrong.
I have an HTML table, which is a Livewire component. In this HTML table, each row has a dropdown box and some text fields. This is for a timesheet application. The dropdown is the job and there are 7 text boxes, for each day of the week for the hours to be entered.
I have tried to include an Add button, I want this to add a new row at the end of the HTML table.
The issue I'm getting is when I click on Add, it will append the row, however, my dropdown is not shown, UNTIL I refresh the page, then it appears as it should.
The way I currently have my program is that when a user clicks on add, it will create a new record in my Entries table, with the timesheet ID included, and the rest of the values as null. Is there another way of temporarily storing the lines the user creates, then commit to the database? Is there a way to refresh my table so that it displays the new line correctly?
Please let me know if you need further details or a better explanation of my issue.
My code is as follows:
Controllers\Livewire\ShowTimesheet.php
...
public function mount($entries)
{
$this->entries = $entries;
}
public function render()
{
return view(‘livewire.show-timesheet');
}
public function add($i)
{
$entries_new = new Entries;
$entries_new->timesheet_id = $i;
$entries_new->save();
$new = array(
'id'=>$entries_new->id,
'timesheet_id'=>$i,
'job_id'=>null,
'worktype_id'=>null,
'hours_sun'=>null,
'hours_mon'=>null,
'hours_tue'=>null,
'hours_wed'=>null,
'hours_thu'=>null,
'hours_fri'=>null,
'hours_sat'=>null,
);
$this->entries[] = $new;
}
...
Layouts\livewire\show-timesheet.blade
...
#foreach($timesheet as $sheet)
...
<div wire:click="add({{$sheet['id']}})" class='cursor-pointer'>
...
#endforeach
...

How to select from mysql array text and compare

I am collecting user selected "text" as array in mysql using PDO connection from php form there are let suppose 3 check boxes with names
1- Apple
2- Banana
3- Cherry
In DB stored values are as per shown
apple,banana,cherry
What i want : I want to display "Apple" users in certain "/offer.php" page
while i am using SELECT query and it's not working i am using pure php mvc framwork.
In Controller i done this
public function offer(){
$this->view->title = User Selection;
$this->view->offerUser = $this->model->offerUser();
$this->view->render('dashboard/offer');
}
In Model I done this
public function offerUser()
{
return $this->db->select('SELECT * FROM users WHERE selection = apple ORDER BY points DESC ');
}
In View i done this
<?php
foreach($this->offerUser as $key => $value) {?>
<tbody>
<tr>
<td></td>
<td><strong><?php echo $value['selection']?></strong></td>
</tr>
The issue is i am not able to select "Text" from stored array with model and even not able to display selection in "View" user end page.
This code format was working fine with single numeric digit selection or compare but failed with text array.
Please help me out with this i really appreciate this i am new with text array fetching.
Hope someone answer my question soon....
Regards,
james
query is not working : use single quotation around apple
public function offerUser()
{
return $this->db->select("SELECT * FROM users WHERE selection = 'apple' ORDER BY points DESC ");
}

how to use Native sessions and MySQL to join information

I have never used sessions before and I am trying to figure out the best way to handle this. I basically am trying to do:
1 step selecting a service
2 step selecting a time
3 step review and book
I can get it to work with no problems using mysql. What I would usually do is save the information into the database after each step and by the time I get to the review part I would have all the information saved and was OK.
However I don't think this is the best way to approach this and might cause problems down the road (what if they stopped at step 2 blah blah)
I decided to try the Laravel 4 sessions and it was super easy to save the session and move on to the next step. However, when I get to the final step I need to join mysql tables to fully show the information about their booking. Can I use the session information to join the information? Can I use the Sessions Database to save this information? Or use different tables?
My controller that POST after reviewing the information:
public function getReview() {
//sets id of user
$user = User::find(Auth::user()->id);
//gets the time and date that they booked from #getSchedule
$scheduler = Session::get('schedule');
//formats time to put in db
$date = strtotime($scheduler['date']);
//same thing as the line above
$dateFormat = date('Y-m-d',$date);
//model to save the schedule
$schedule = new Schedule();
$schedule->userID = $user->id;
$schedule->date = $dateFormat;
$schedule->block = $scheduler['timeslot'];
$schedule->status = "1";
$schedule->save();
//gets the services the user picked from #getServices
$service = Session::get('service');
//saves the services as individual rows in db table
foreach($service as $services)
{
if(!empty($services)) {
$service = new Service();
$service->userID = $user->id;
$service->services = $services;
$service->save();
}
}
return Redirect::to('dashboard');
}
This is the GET review page (where I am having the issues with all the JOINS)
public function showReview() {
$user = User::find(Auth::user()->id);
//show the information and confirm that they want all this crap...if they do..save it and return them to their dashboard
$time = DB::table('bk_schedule')
->leftJoin('bk_timeslot', 'bk_schedule.block', '=', 'bk_timeslot.id')
->where('bk_schedule.id', Auth::user()->id)->first();
$date = strtotime($time->date);
$dateFormat = date('D: F d, Y',$date);
$service = Session::get('service');
$serviceSummary = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->get();
$total = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->sum('pr_service.price');
return View::make('book.review', array('pageTitle' => 'Booking Appointment Review and Finalize', 'service' => $service, 'date' => $dateFormat,
'time' => $time, 'email' => $user->email, 'serviceSummary' => $serviceSummary, 'total' => $total));
}
Is it possible to save the information at the GET and delete it if they don't submit to POST? Could I maybe use my session data to and use the MySQL queries I have?
You don't understand what the session is, with you approach users will not be able to fill several forms (open in several tabs) simultaneously.
So, general way to do this is:
First page shows just HTML code with some fields
User selects them and POST data back to server
Server validates data and open ANOTHER HTML page with new fields AND adds several "hidden" field with values selected in step 1 (of course server can present page1 with error messages)
Users posts this form, server can open THIRD form where new visible fields and ALL previous fields are stored in hidden inputs
Finally user posts form to last page, where you have all data from all previous pages.
Just to notice: another approach is to store "temporary" data in session, for this you will need to obtain some generated ID on 2nd step and pass it through pages as described before

Pagination script - getting data from two tables

I have a pagination script, which can be seen here:
http://www.automotori6282.tk/phpsandbox/
This is the source code:
http://pastebin.com/raw.php?i=63dCvfxD (PmcPagination.php)
http://pastebin.com/raw.php?i=3k4nqwRB (demo.php, the page with the code in)
and index.php for http://www.automotori6282.tk/phpsandbox/ simply uses PHP's include() function to show demo.php
What I'm trying to do is extract from two tables for the pagination.
This is my current database structure:
programme varchar(255)
channel varchar(255)
episode varchar(255)
series varchar(255)
setreminder varchar(255)
However, I have the episodes stored in a separate table, and would like to make a query which extracts from both epdata167 and episodes table (the episodes table only has episode, seriesno, episodeno and description as its fields).
One error I made in my pagination script shows here:
TV Show showing on Channel 1 August 3rd - 12:30pm "" Set Reminder
TV Show showing on Channel 1 August 3rd - 8:30pm "Episode 1" Set Reminder
and it still remains long after the event has happened.
This is how it should work:
TV Show showing on Channel 1 August 3rd - 12:30pm
TV Show showing on Channel 1 August 3rd - 2:45pm Set Reminder
TV Show showing on Channel 1 August 3rd - 8:30pm "Episode 1" Set Reminder
Notice how the Set Reminder field doesn't show when the event happens, and there's no quotation marks if an episode isn't selected.
(The setreminder field is basically a link that a user would click on to send a reminder to them saying "TV show is airing on Channel 1 August 3rd - 8:30pm Episode 1" or "TV show is airing today at 8:30pm Episode 1". However, it would not display for the show that is airing right now, it would display like this:
TV Show showing on Channel 1 August 3rd - 12:30pm
)
This is the pagination code causing trouble:
$result = mysql_query("SELECT programme, channel, airdate, expiration, episode, series, setreminder FROM epdata167 where airdate > curdate() GROUP by airdate");
and here is the part of the pagination code I edited to try and use an ifelse statement but wasn't sure what to do:
class paginationData {
private $program;
private $channel;
private $airdate;
private $exiration;
private $episode;
private $series;
private $setReminder;
public function __construct($program, $channel, $airdate, $expiration, $episode, $setReminder)
{
$this->program = $program;
$this->channel = $channel;
$this->airdate = strtotime($airdate);
$this->expiration = $expiration;
$this->episode = $episode;
$this->setReminder = $setReminder;
}
//This function shows the data
public function display()
{
if(date('Y-m-d') == date('Y-m-d', $this->airdate)) $dateFormat = 'g:ia';
elseif(date('Y') == date('Y', $this->airdate)) $dateFormat = 'F jS - g:ia';
else $dateFormat = 'F jS, Y - g:ia';
echo '<tr>'."\n".
' <td><strong>'.$this->program.'</strong></td>'."\n".
' <td>showing on '.$this->channel.'</td>'."\n".
' <td>'.date($dateFormat, $this->airdate).'</td>'."\n".
' <td><b>"'.$this->episode.'"</b><br></td>'. "\n".
' <td>'.$this->setReminder.'</td>'."\n".
'</tr>'."\n";
}
}
Basically, I'm having trouble trying to get data as this in my project:
TV Show showing on Channel 1 August 3rd - 12:30pm
TV Show showing on Channel 1 August 3rd - 2:45pm Set Reminder
TV Show showing on Channel 1 August 3rd - 8:30pm "Episode 1"
Series 1, episode 1 Set Reminder
(that's if the episode is part of a series with a series number)
Not sure what JOIN to use and where, all help is appreciated trying to get this to work since I've got the pagination working; just trying to get the finer details working!
I'm not asking for people to do it for me, I've been learning myself, and have done my research.
Sorry if this seems a bit long, but I hope I've explained it all.
Thanks.
You need to use a UNION to get this to work.

Categories