How to get the value from another table under same DB in LARAVEL? - mysql

I want to ask how to call the value from another table under same database ?
Database table name: bit_app_policy_category
Under the database I have these columns:
ID
code
description
parent_id
status
Another Database Table name under same database : company_policy
Under the database i have these columns:
ID
policy_category_id
policy_title
version_no
policy_details
expiry_date
file_path
As for now I want to link the table bit_app_policy_category and get the columns of id's value into company_policy - policy_category_id. I don't know how to write the code.
Here is my current code :
<form method="post" action="{{route('policy.store')}}">
{{csrf_field()}}
#csrf
<div class="form-group">
<label for="bit_app_policy_category_parent">Parent Category</label>
<select id="bit_app_policy_category_parent" name="parent_id" class="form-control">
<option value=" {{"$parents->id"}} </option>n>
</select>
</div>
<div class="form-group">
<label for="company_policy_policy_title">Policy Title<span class="required">*</span></label>
<input id="company_policy_policy_title" type="text" name="policy_title" class="form-control" placeholder="Please Enter the policy title" />
</div>
<div class="form-group">
<label for="company_policy_version-no">Version-no<span class="required">*</span></label>
<input id="company_policy_version-no" type="text" name="version_no" class="form-control" placeholder="Please Enter the Version-no" />
</div>
<div class="form-group">
<label for="company_policy_policy_details">Policy Details<span class="required">*</span></label>
<input id="company_policy_policy_details" type="text" name="policy_details" class="form-control" placeholder="Please Enter the Policy Details" />
</div>
<div class="form-group">
<label for="company_policy_expiry_date">Expiry Date<span class="required">*</span></label>
<input id="company_policy_expiry_date" type="datetime-local" name="expiry_date" class="form-control" placeholder="Please Enter the Expiry Date time" />
</div>
<div class="form-group">
<label for="company_policy_file_path">Policy File Path<span class="required">*</span></label>
<input id="company_policy_file_path" type="text" name="file_path" class="form-control" placeholder="Please Enter the file path" />
</div>
<div class="form-group">
<input type="submit" class="btn btn btn-primary" />
Back</span>
Back to home</span>
</div>
</form>
</div>
</div>
#endsection

First create two model
1. PolicyCategory and put the code in model
public function companyPolicies()
{
return $this->hasMany('App\CompanyPolicy');
}
2.CompanyPolicy
public function policyCategory()
{
return $this->belongsTo('App\PolicyCategory',policy_category_id);
}
and in you controller then you can get all the info of related table. Like say, you have one/two company under the policy_category which id is 1. Now you can get all the companies info like below
$policy_category = PolicyCategory::find(1);
dd($policy_category->companyPolicies);
try this and let me know is this really you want or not

Related

Getting records from database, FROM a date TO a date

I'd like to obtain a record of a student's attendance from the "attendance" table. From a date to a date, e.g., from September 23, 2022, to December 10, 2022. Admin will enter the student's class and roll number, as well as a From date to date range.
Using his class and roll number. How to do this in Laravel 9 Thanks
this is the from through which a specific students record will be retirved.
<form class="mt-3" method="POST" action="\record">
#csrf
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Student Class</label>
<input type="number" name="class" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Roll Number</label>
<input type="number" name="roll number" class="form-control" id="exampleInputPassword1">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">From Date</label>
<input type="date" name="from_date" class="form-control" id="exampleInputPassword1">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">To Date</label>
<input type="date" name="to_date" class="form-control" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary">Generate</button>
You can use the whereBetween in laravel to achieve this.
Suppose you have a model named Student.
You'd then do this in your controller:
$from = now();
$to = now()->addDays(5);
$studentAttendance = Student::whereBetween('date', [$from, $to])->get();
Which will get the students who have a date between $from and $to dates.

Format of URL for advanced Google search engine query

I am trying to find the advanced search query for HTML to automatically search the results.
For example, a plain google.com query is https://google.com/search, while image search query is https://images.google.com/images
I can't find the query to automatically search the values from 4 text boxes through the Advanced Search engine.
The functionality I am looking for is that when I try searching for keywords from my web page it should go through the advanced search engine:
<form action="https://www.google.com/advanced_search?as_q=q&as_epq=r&as_oq=s&as_eq=t">
I found an alternative to going through the Advanced Search engine.
The Advanced Search has a few parameters that are passed in the URL for a search query:
all these words: the operator passed is as_q like this.
<input class="" type="text" name="as_q">
this exact word or phrase: the operator passed is as_epq
any of these words: the operator passed is as_oq
none of these words: the operator passed is as_eq
So I named the HTML textboxes with the corresponding names and concatenated the above parameters to the search query.
For the first section of the advanced search, try
<form action="https://google.com/search" name="f">
<div class="field">
<label for="field1">all these words:</label>
<input type="text" class="textinput" id="field1" value="" name="as_q">
</div>
<br>
<div class="field">
<label for="field2">this exact word or phrase: </label>
<input type="text" class="textinput" id="field2" value="" name="as_epq">
</div>
<br>
<div class="field">
<label for="field3">any of this words: </label>
<input type="text" class="textinput" id="field3" value="" name="as_oq">
</div>
<br>
<div class="field">
<label for="field4">none of these words: </label>
<input type="text" class="textinput" id="field4" value="" name="as_eq">
</div>
<br>
<div class="field">
<label for="field5">numbers ranging from: </label>
<input type="text" class="textinput1" id="field5" value="" name="as_nlo">
<span class="seperator">to</span>
<input type="text" class="textinput2" id="field5" value="" name="as_nhi">
</div>
<input type="submit" value="advanced search">
</form>
If you are trying to create HTML page, I would like to share this code.
You can add a space for a right indentation using & nbsp;.
Here is an example:
<form action="https://google.com/search">
All these words:<input type="text" name="as_q" >
<br><br>Exact word/phrase:<input type="text" name="as_epq" >
<br><br>Any of these words:<input type="text" name="as_oq" >
<br><br>None of these words:<input type="text" name="as_eq" >
<br><br> <input type="submit" value="advanced search">
</form>

is there any way i could make the first row as mandatory before going to second row in codeigniter?

My view:
<div class="col-md-3">
<div class="form-group">
<label>Employee Id</label>
<input type="text" class="form-control" placeholder="Enter employee id" name="empid">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Employee Name</label>
<input type="text" class="form-control" placeholder="Enter employee name" name="empname">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Order Number</label>
<input type="number" class="form-control" placeholder="Enter order number" name="ordernumber" value="<?php echo $order_number;?>">
</div></div>
<div class="col-md-3">
<div class="form-group">
<label></label>
<a onclick="myFunctionfirst()" class="form-control">Proceed to order Create</a>
</div></div>
Once you click on 'proceed to order create' the second row is created. I want this to happen only when the first 3 fields in first row are filled. Please suggest on this
And this is my controller:
public function index()
{
$empid = $_POST['empid'];
If ($empid == ""){
$this->load->model('invoice_model');
$data['stat']= $this->invoice_model->get_stationary();
$data['order_numbers']= $this->invoice_model->get_countnumber();
$data['order_number']= $data['order_numbers'][0]->count+1;
$data['page']='invoice/invoice_view';
$this->load->view("template",$data);
}
}
And it's throwing an error undefined index empid
Welcome to stackoverflow. what you can do is save the input as a variable Store input from a text field in a PHP variable
this method is however a bit unsafe. to secure the input you can use https://www.w3schools.com/php/func_string_htmlspecialchars.asp
https://www.php.net/manual/en/function.htmlspecialchars.php
and once you have the variables just simply check if the variable is empty or not
public function index()
{
$firstname = $_POST['firstname'];
If ($firstname != ""){
$this->load->model('invoice_model');
$data['stat']= $this->invoice_model->get_stationary();
$data['order_numbers']= $this->invoice_model->get_countnumber();
$data['order_number']= $data['order_numbers'][0]->count+1;
$data['page']='invoice/invoice_view';
$this->load->view("template",$data);
}
}
$_POST is how the data is being send. here they explain the difference between POST and GET https://www.quora.com/What-is-the-difference-between-PHPs-get-method-post-method
'firstname' is the name of the input. in your case its <input type="text" class="form-control" placeholder="Enter employee id" name="empid"> so it should be $_POST['empid']
the data is being send with the submit button. instead of using the <a> tag you should use the <button type="submit"> tag also, wrap the inputs in a form tag.

HTML Form naming conventions

If I have an HTML form with two inputs that would insert/modify a columns with the same name in two tables in a database.
What would be the best way to name my input elements?
<div class="form-group">
<input type="text" class="form-control" id="mothers_name" name="name" placeholder="Enter Mothers Name">
</div>
<div class="form-group">
<input type="text" class="form-control" id="childs_name" name="name" placeholder="Enter Child's Name">
</div>
db-
--table1
---name
--table2
--name
I am trying to save these information using laravels create(request->all())
I see that the request object contains the last input value.
I would name the inputs the same way you have named the ids. (Or something similar.)
<div class="form-group">
<input type="text" class="form-control" id="mothers_name" name="mothers_name" placeholder="Enter Mother's Name">
</div>
<div class="form-group">
<input type="text" class="form-control" id="childs_name" name="childs_name" placeholder="Enter Child's Name">
</div>
Within your Controller, you could do something like this:
public function storeNameTable1(Request $request) {
$store_name = new Table1;
$store_name->name = $request->mothers_name;
$store_name->save();
}
public function storeNameTable2(Request $request) {
$store_name = new Table2;
$store_name->name = $request->childs_name;
$store_name->save();
}

Chrome remembers postcode rather than Email/username

I am working on an MVC5 Web Application , I have a simple registration form containing Email , postcode and password. But when I register the chrome remembers the postcode and password rather than email and password; and next time when I try to enter email the postcodes appear as suggestions.
My markup for the form is as under:
I have edited the question and pasted the whole form. kindly suggest me what to do other than moving email and passwords up or down.
<div class="wrapper">
<div class="closeBetaWrapp">
<div class="simplePopup">
<div class="loginWrapp signupWrapp">
<div class="iconCont"><img src="images/orchaIcon.png" alt="Orcha"></div>
<form action="">
<div class="formrow ">
<div class="leftCol">
<label for="Email">Email</label>
<input class="formField" type="email" name="Email" id="Email" placeholder="Enter your email address here" />
</div>
<div class="leftCol">
<label for="Pcode">Postcode</label>
<input class="formField" type="tel" name="Pcode" id="Pcode" placeholder="Enter postcode here" />
</div>
<div class="leftCol">
<label>Year of Birth</label>
<select id="" class="dropdown">
<option>Choose your year of birth</option>
<option>1980</option>
<option>1981</option>
<option>1982</option>
<option>1983</option>
<option>1984</option>
<option>1985</option>
<option>1986</option>
<option>1987</option>
<option>1988</option>
<option>1989</option>
<option>1990</option>
<option>1991</option>
<option>1992</option>
<option>1993</option>
<option>1994</option>
<option>1995</option>
</select>
</div>
<div class="leftCol">
<label>Gender</label>
<select id="" class="dropdown">
<option>Choose your year of birth</option>
<option>Female</option>
<option>Male</option>
</select>
</div>
<div class="leftCol">
<label for="Password">Password</label>
<input class="formField" type="password" name="Password" id="Password" placeholder="Enter your password here" />
</div>
<div class="rightCol">
<label for="CPassword">Confirm Password</label>
<input class="formField" type="password" name="CPassword" id="CPassword" placeholder="Enter password again" />
</div>
</div><!-- formrow -->
<div class="formrow">
<div class="rightCol btnCol">
<input class="moreBtn" type="submit" value="Signup" />
<input class="moreBtn cnclBtn" type="reset" value="Cancel" />
</div>
</div><!-- formrow -->
</form>
</div>
</div>
</div><!-- closeBetaWrapp -->
Browsers detect passwords by form.elements[n].type == "password" and iterating through all the document. Then it detects the username field by searching backwards through form elements for the text field immediately before the password field.
So, In your case, It thinks your postcode as your username field and remembers this.
So, solution is to move the postcode field above the email field.
If even after moving, it doesn't work, delete previously saved postcode-password value from 'saved passwords' in chrome settings. Also delete any previously saved values by using down arrow key to select the suggestion and pressing shift + delete.
I got the information about how browsers detect username-password field here & here
You can use the autocomplete attribute to tell the browser what the field contains. For example:
<input class="formField" autocomplete="postal-code" type="text" name="PostCode" id="PostCode" placeholder="Enter your postcode here" />
See https://html.spec.whatwg.org/multipage/forms.html#autofill for more information.