I have a form that has a start_odo and an end_odo for vehicle mileage tracking that user needs to enter. The problem is that currently, they can enter a start_odo that is higher than the end_odo and that results in a negative difference. Is any way to ensure that the end_odo will always have to be higher?
Being fairly new to Laravel, I read about the gt:field but I am not quite sure how to interpret it in my code as it looks different. Could someone nudge me in the right direction.
Controller:
public function store(Request $request)
{
// $energy = new Maintenance;
$energy = new VehicleLog();
$energy->start_odo = $request->input('start_odo');
$energy->end_odo = $request->input('end_odo');
$energy->km = $request->input('end_odo') - $request->input('start_odo');
$energy->save();
return redirect('/vmaintenance')->with('success', 'data added');
}
my view:
<div class="mb-3" style="float:left;" style="margin-left: 200px;">
<label for="recipient-name" style="width: 7em"class="col-form-label">Start ODO</label>
<input type="number"style="width: 7em" name="start_odo" class="form-control" id="recipient-name" min="0" required>
</div>
<div class="mb-3">
<label for="recipient-name" class="col-form-label">End ODO</label>
<input type="number" style="width: 7em" name="end_odo" class="form-control" id="recipient-name" min="0" required>
</div>
The validation code I saw while reading:
$request->validate([
'detail' => 'gt:20',
]);
you have to figure out yourself how to write code. there are lots of answers and tutorials out there regarding this. however i am adding this answer to get you started.
in your store method, validate the data before saving to database.
public function store(Request $request)
{
// validation logic
$request->validate([
'start_odo' => 'required',
'end_odo' => 'required|gt:start_odo',
]);
// if validation passes this will now store values in db
$energy = new VehicleLog();
$energy->start_odo = $request->input('start_odo');
$energy->end_odo = $request->input('end_odo');
$energy->km = $request->input('end_odo') - $request->input('start_odo');
$energy->save();
return redirect('/vmaintenance')->with('success', 'data added');
}
now explore the doc how to show validation errors in the form and other ways to validate data.
As #zahidhasanemon had already indicated. You can achieve it with customs rules. First step would be to define the new validation rule in your AppServiceProvider.
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Validator::extend('greater_than_field', function($attribute, $value, $parameters, $validator) {
$min_field = $parameters[0];
$data = $validator->getData();
$min_value = $data[$min_field];
return $value > $min_value;
});
Validator::replacer('greater_than_field', function($message, $attribute, $rule, $parameters) {
return str_replace(':field', $parameters[0], $message);
});
}
}
Then you can use this new validation rule in your controller like that:
$request->validate([
// other validations rules
'end_odo' => 'required|greater_than_field:start_odo',
]);
Related
I have a dynamic navigation bar, created however it won't show on the dynamic web page.
The current output is:
ErrorException
Undefined variable: navContent (View: C:\Users\Computer
Angel\Documents\blog\resources\views\page\dynamic.blade.php)
The desired output is my dynamic.blade.php where the pageContent is the dynamic page content the user inputted through a form and the dynamic navigation bar in the tags.
This is my dynamic.blade.php:
<nav>
#foreach($navContent as $nav)
{!!nav-navName!!}
#endforeach
</nav>
<body>
{!!$pageContent->pageContent!!}
</body>
This is my NavController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Nav;
use DB;
use Illuminate\Database\MySqlConnection;
class NavController extends Controller
{
public function index()
{
$navs = Nav::all();
return view('navManagement', compact('navs'));
}
public function create()
{
return view('createNav');
}
public function store(Request $request)
{
$data = request()->validate([
'navName' => 'required',
'navLink' => 'required',
]);
$nav = new Nav([
'navName' => $request->get('navName'),
'navLink' => $request->get('navLink'),
]);
$nav->save();
return redirect('/n');
}
public function show($navName)
{
$navContent = DB::table('navs')->where('navName',$navName)->first();
return view('page.dynamic', ['navContent' => $navContent]);
}
public function edit($navName)
{
$navContent = DB::table('navs')->where('navName',$navName)->first();
return view('editNav', ['navContent' => $navContent]);
}
public function update(Request $request)
{
$data = $request->validate([
'navName' => 'required|exists:navs,navName',
'navLink' => 'required'
]);
$obj = \App\Nav::where('navName', $request->navName)
->update([
'navLink' => $request->navLink
]);
return redirect('/n');
}
public function destroy(Request $request)
{
$obj = \App\Nav::where('navName', $request->navName)
->delete();
return redirect('/n');
}
}
This is my PageController.php:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Http\Request;
use DB;
use Illuminate\Database\MySqlConnection;
class PageController extends Controller
{
public function index()
{
$pages = Page::all();
return view('pageManagement', compact('pages'));
}
public function create()
{
//This will load create.blade.php
return view('createPage');
}
public function store(Request $request)
{
$data = request()->validate([
'title' => 'required',
'URI' => 'required|min:5|max:10|',
'pageContent' => 'required',
]);
$page = new Page([
'title' => $request->get('title'),
'URI' => $request->get('URI'),
'pageContent' => $request->get('pageContent'),
]);
$page->save();
return redirect('/p');
}
public function show($URI)
{
$pageContent = DB::table('pages')->where('URI',$URI)->first();
return view('page.dynamic', ['pageContent' => $pageContent]);
}
public function edit($URI)
{
$pageContent = DB::table('pages')->where('URI',$URI)->first();
return view('editPage', ['pageContent' => $pageContent]);
}
public function update(Request $request)
{
$data = $request->validate([
'title' => 'required',
'URI' => 'required|min:5|max:10|exists:pages,URI',
'pageContent' => 'required'
]);
$obj = \App\Page::where('URI', $request->URI)
->update([
'title' => $request->title,
'pageContent' => $request->pageContent
]);
return redirect('/p');
}
public function destroy(Request $request)
{
$obj = \App\Page::where('URI', $request->URI)
->delete();
return redirect('/p');
}
}
This is my Nav.php:
class Nav extends Model
{
protected $fillable = ['navName', 'navLink'];
}
This is my Page.php:
class Page extends Model
{
protected $fillable = ['title', 'URI', 'pageContent'];
}
This is my migration for pages:
public function up()
{
Schema::create('pages', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('URI');
$table->text('pageContent');
$table->timestamps();
});
}
This is my migration for nav:
public function up()
{
Schema::create('navs', function (Blueprint $table) {
$table->id();
$table->string('navName');
$table->string('navLink');
$table->timestamps();
});
}
This is my createNav.blade.php:
<form action="/storeNav" method="post">
#csrf
<label for="navName">Navigation Bar Option Name:</label><br>
<input type="text" id="navName" name="navName" autocomplete="off" value="{{ old('navName') }}">
<br>
#error('navName') <p style="color: red">{{ $message }}</p> #enderror
<label for="navLink">Navigation Bar Option Link:</label><br>
<input type="text" id="navLink" name="navLink" autocomplete="off" value="{{ old('navLink') }}">
<br>
#error('navLink') <p style="color: red">{{ $message }}</p> #enderror
<input type="submit" value="Submit">
</form>
This is my createPage.blade.php:
<form action="/storePage" method="post">
#csrf
<label for="title">Title:</label><br>
<input type="text" id="title" name="title" autocomplete="off" value="{{ old('title') }}"><br>
#error('title') <p style="color: red">{{ $message }}</p> #enderror
<label for="URI">URI:</label><br>
<input type="text" id="URI" name="URI" autocomplete="off" value="{{ old('URI') }}"><br>
#error('URI') <p style="color: red">{{ $message }}</p> #enderror
<label for="pageContent">Page Content:</label><br>
<textarea id="pageContent" name="pageContent" value="{{ old('pageContent') }}"></textarea>
#error('pageContent') <p style="color: red">{{ $message }}</p> #enderror
<input type="submit" value="Submit">
</form>
This is my web.php:
Route::get('/page/{URI}', 'PageController#show');
Route::get('/page/{URI}/edit', 'PageController#edit');
Route::get('/p', 'PageController#index');
Route::get('/createPage', 'PageController#create');
Route::post('/storePage', 'PageController#store');
Route::patch('/page/{URI}', 'PageController#update');
Route::delete('/page/{URI}', 'PageController#destroy');
Route::get('/nav/{navName}/edit', 'NavController#edit');
Route::get('/n', 'NavController#index');
Route::get('/createNav', 'NavController#create');
Route::post('/storeNav', 'NavController#store');
Route::patch('/nav/{navName}', 'NavController#update');
Route::delete('/nav/{navName}', 'NavController#destroy');
Below is my github repository link, if you want to take a look at my full code, or you want to try run the code in your Integrated Development Environment.
https://github.com/xiaoheixi/blog
Thanks for reading! :D
If is complaining that it can't find a variable called $navContent.
I can't see you passing it to either of the views you are calling from your index() functions.
return view('navManagement', compact('navs'));
return view('pageManagement', compact('pages'));
You would need to set that varaible and pass it to the view
// Get the nav content however you want, this is just a crude example
$navContent = $this->getMyNavContent();
return view('navManagement', [
'navs' => $navs,
'navContent' => $navContent
]);
I'm guessing you want to find a way to embed the dynamic nav without having to add the navContent every time you render a view, as that's what the other answer is suggesting. You could achieve this using a middleware, say dynamicNav and registering your routes under that middleware group.
In that middleware you can do all the logic of fetching the nav content and then use something like merge, as shown here:
$request->merge(['dynamicNav' => $navContent]);
This way your middleware will add the data to every request which goes through it, although I wouldn't recommend this solution.
What I would do is cache the nav content and retrieve in the view using the cache() helper, then override the save() method of the model to also update the cache when the DB is updated (to avoid duplicate code, you could create a trait for the fetching of the nav). Example:
// in your model
public function save(array $options = [])
{
Cache::put('nav-content', getNavContent());
parent::save();
}
In my controller, I validate the input fields and if any validation fails I want to return back to the form and refill the previous values that the user was inputing (if there is any other easier way instead this please tell me). But here is what I'm doing:
Controller.php:
public function store(Request $request, AnexoController $anexoController)
{
$validator = Validator::make(
$request->all(),
$rules,
$messages
);
if($validator->fails())
{
return Redirect::to($request->headers->get('referer'))
->withErrors($validator)
->withReq($request->all());
}
// continues the function...
}
At the view.blade.php:
<input type="text"
maxlength="256"
id="text-input"
name="id"
placeholder="Código"
class="form-control"
#if(isset($req)) value="{{$req->id}}"
#else value="$REQ VARIABLE NOT SET"
#endif
required>
And when I test it, the field id value is set to "$REQ VARIABLE NOT SET" but $errors from $validator variable is set. So, why isn't the variable $req being set in this context? Thanks in advance.
Use this
$request->validate([
//rules
]);
It will automatically return to form page with all the errors and inputs,
In your form you'll neet do add a helper in value of inputs for old inputs
<input type="text" value={{ old('email')}} name="email">
see https://laravel.com/docs/5.8/validation & https://laravel.com/docs/5.8/helpers
I'm using Laravel 5.6 to develop a website.
Currently, I want to write a test codes for the website. I'm also new to building a website in general and this is learning curve for me to learn what I'm doing wrong.
I created a Profile based on a User model and the Profile should only be editable by the authenticated User only.
The form is actually working without errors on the browser side but once i run phpunit, it will fail.
Test Script:
/** #test */
public function an_authenticated_user_can_view_the_profile_page()
{
// Generate fake instance of authenticated user
$this->be($user = factory('App\User')->create());
// Will get the URL
$response = $this->get('/profile/'.$user->name);
// Check whether the string exists
$response->assertSee('Personal details for '.$user->name);
}
Controller:
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(User $user)
{
return view('user.profiles.show', compact('user'));
}
public function update(Request $request)
{
$this->validate(request(), [
'company' => 'required',
'street' => 'required',
'city' => 'required',
'zip_code' => 'required',
'state' => 'required',
'country' => 'required',
'phone' => 'required',
]);
$profile = \Auth::user()->profile;
$profile->update($request->all());
return back()->with('success', 'Profile updated!');
}
}
View:
<div class="heading">
<h3 class="text-uppercase">Personal details for {{ $user->name }}</h3>
</div>
<form method="POST" action="/profile">
{{method_field('PATCH')}}
{{csrf_field()}}
<input type="hidden" value="{{ $user->profile->id }}" name="id">
<div class="col-md-6">
<div class="form-group">
<label for="company">Company</label>
<input id="company" type="text" class="form-control" name="company" value="{{ $user->profile->company }}" required>
</div>
</div>
</form>
Image of the commented out Form test:
Commented Form
Image of the not commented Form test:
Not commented Form
I am rather confused why my test is failing once I insert the form with a value tag. If i commented out the form or just remove the value tag, the test will pass.
Been searching for the few days and still can't find the right answer to this. Am i using the right Assertion? What am I missing here? Any inputs will help me to further understand this. Thanks!
I found the answer. It was actually the factory that I've created.
In the User model, every registration leads to creating an empty Profile.
This is the new way of how I write the test script:
/** #test */
public function an_authenticated_user_can_view_the_profile_page()
{
//Generate a fake profile
$profile = factory('App\Profile')->create();
// Assign it to the user
$user = $profile->user;
// Authenticate the user
$this->be($user);
// Will get the URL
$response = $this->get('/profile/'.$user->name);
// Check whether the string exists
$response->assertSee('Personal details for '.$user['name']);
}
There are two mysql tables 1.seats (id,number), 2.reservedseats(id,seat_id,sceering_id).
I show all the seats of a specific sceering as checkboxes in show.blade:
{!!Form::model($screening,['method'=>'post', 'action'=>
['ReserveController#store',$screening->auditorium->id]])!!}
<input type="hidden" name="screening_id" value="{{$screening->id}}">
#foreach($seats as $seat)
<label class="checkbox-inline">
{!!Form::checkbox('seat_id[]',$seat->id,null)!!} Number: {{$seat->number}}
</label>
#endforeach
<div class='form-group'>
{!!Form::submit('Create Post',['class'=>'btn btn-primary '])!!}
</div>
{!!Form::close()!!}
When I click a checkbox it goes the the seat_id[] array. So I send a hidden input and an array with the seat_ids then I want to store in the reservedseats Mysql table. But in the store controller I have the problem. I'm trying something like:
public function store(Request $request){
$screening_id = $request->screening_id;
$seat_ids = $request->seat_id;
foreach($seat_ids as $seat_id){
Seatreserved::create($seat_id,$screening_id);
}
}
So it not working but how can I solve that?
Try this code
public function store(Request $request)
{
$screening_id = $request->screening_id;
$seat_ids = $request->seat_id;
foreach($seat_ids as $seat_id) {
Seatreserved::create([
'seat_id' => $seat_id,
'screening_id' => $screening_id
]);
}
}
Also you can use
public function store(Request $request)
{
$screening_id = $request->screening_id;
$seat_ids = $request->seat_id;
$data = [];
foreach($seat_ids as $seat_id) {
$data[] = [
'seat_id' => $seat_id,
'screening_id' => $screening_id
];
}
Seatreserved::insert($data);
}
That is better way to perform this as it will interact with database for once.
You can also create a new instance of your model to store values.
Example:
foreach($seat_ids as $seat_id) {
$reserved = new Seatreserved();
$reserved->seat_id = $seat_id;
$reserved->screening_id = $screening_id;
$reserved->save();
}
I have a dropdown like this
How could i sent the option selected on dropdown into database? I need blade syntax to send them.
Here is my piece of code for validation:
FrontController
public function insert(Request $request)
{
$request->validate([
'jenis' => 'required|string',
'kategori' => 'required|string',
'nopol' => 'required|string',
]);
DB::table('park')->insert([
'jenis' => $request->input('jenis'),
'kategori' => $request->input('kategori'),
'nopol' => $request->input('nopol'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'status' => true;
]);
}
home.blade.php
<div class="control-group">
<label class="control-label" for="typeahead">Jenis Kendaraan</label>
<div class="controls">
<select id="opt" name="jenis">
<option value="1">Motor</option>
<option value="2">Mobil</option>
</select>
</div>
</div>
Should i use #foreach or directly use #if #else #endif ?
I think you want to save jenis with value either 'Motor' or 'Mobile' because you are validating as a string. Just put as
<select id="opt" name="jenis">
<option value="Motor">Motor</option>
<option value="Mobil">Mobil</option>
</select>
Whatever you write in value will go in the request object.
<option value="1">Motor</option>
this what you wrote in the value , so on the form submission in request()->input('jenis') you will get 1.. you should give numeric validation or between(range) type of validation, please check the laravel docs.
In case anyone stumbled upon this via Googling how to validate actual Blade syntax, check this out.
Create a custom validation rule:
<?php
namespace App\Rules;
use Exception;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\File;
class BladeSyntax implements Rule
{
private $message;
public function passes($attribute, $value)
{
File::put(resource_path('views/validation.blade.php'), $value);
try {
view('validation')->render();
return true;
}
catch (Exception $exception) {
$this->message = $exception->getMessage();
return false;
}
}
public function message()
{
return explode('(', $this->message)[0];
}
}
Then use new BladeSyntax in your validation rules.