I am new in laravel and i am developing an application that I need to display the persons first and last name upon registering on my system. But I am finding it it hard to achieve this instead my code retrieves all the first and last names from my table if i use the instance $contact =contacts::all();
My table named contact has fields like fname, lname,email,phone, body, updated_at,...
I have tried using the ...all()->pluck() to select only the first name and last name but my views displays everything first and last name from the table. I can do it in my views but i need your help in my controller.
Here is my controller
$contact =contacts::all()->pluck('lname','fname').
Anyone with an idea please
-> contacts::all() --- This will result all the data in contacts model
-> contacts::first() --- This will result only single row. please add a where condition with this for required result.
e.g contacts::where('name','=','abc')->first();
contacts::all() will retrieve every single row. If you want to limit the selection to contacts that only have a certain first/last name, then you'll want to use where and get(), which will only get those particular rows:
$fname = 'John';
$lname = 'Doe';
contacts::where('fname', $fname)->where('lname', $lname)->get();
The two variables, of course, would be populated with whatever you're looking for. You can also pass in a comparison as the second parameter, in case you want to check for not equals, LIKE (good for case insensitive searches or wildcard searches), or greater than/less than for numbers and dates.
contacts::where('fname', '=', $fname)->where('lname','=', $lname)->get();
Note that this will bring up everyone with the name John | Doe. It may not be the person who just registered. For that, you'll want to save the user ID somewhere such as in the session after they register, and instead look for that id, which will bring up only 1 record instead of a collection of records that get() would provide.
contacts::find($id);
Related
how to compaire two tables value and get the result of particular column value from both tables in laravel relationship method? I don't know how to do it with relationship, at the same time I don't know whether it is based on many to many or one to many relationship. at the same time I have to do it with where condition.
here is my mormal query example for my requested code:
select teacher.teacher_name, subject. subject_name from teacher join subject on teacher.subject_token = subject.token where teacher.teacher_token = '1';
As I understood you, you want to get a teacher and his/hers subject?
Teacher::select(['id','name','subject_token'])->with('subject')->where('id', $id)->first()
Explanation: From the teacher's table take a teacher with id $id (id passed to the method) and take his id, name, and subject_token, with this get the subject that has the same token taken from the teacher's table. (Usually, instead of tokens you could use id)
I have three tables, and I'm just looking for a way to make this work.
tbl_campaigns has the columns "id" and "campaign". This one is fairly straight forward, it's just campaign names with an ID number that is auto-incremented so they have unique IDs.
tbl_users has an "id" column so each user has a unique ID number, standard stuff.
tbl_permissions creates a new row whenever a new user is created. This means its "id" column has unique ID values that match to the ID of a user in 'tbl_users'. The columns have been named to match the ID value of a campaign each time a new one is created, for example, the column "campaign_1" is relevant to the campaign in 'tbl_campaigns' with the ID of 1. The idea is this table data is filled with either 1's or 0's.
If a row with the ID of 1 has the number 1 for the column "campaign_1", then the user with the ID of 1 is approved for the campaign with the ID of 1 in the campaign table. If it were 0 then they're not approved for it. The same logic applies for columns "campaign_2", "campaign_3" etc..
Anyways, the issue I'm having is displaying this information on a front-end, as I only want the user to be able to see the campaigns they are approved to run in a drop-down list. When the user is logged in it stores their User ID in a session, I'm not sure if there's a way around it with this method.
Is there any way to get around this? Please note I've done this in procedural PHP as I'm still in my early days, so if anyone has a solution along these lines it would be much appreciated. Sorry if it's a little confusing. I am aware it's a bit ham-fisted, but I just want it to work first.
I believe that your schema needs to be improved, as the table structure should not have to change every time that you add a new campaign.
keep tables tbl_campaigns and tbl_users as they are
create table tbl_permissions with 4 fields (id, user_id, campaign_id and permission)
To check if a user has permission use a query like this:
SELECT permission FROM tbl_permissions WHERE user_id = ? AND campaign_id = ?
So, every time you create a campaign add a corresponding record to the tbl_permissions table. No need to add a new column.
I think the best practice to do this is as follows:
- Create HTML to show to the user(if you don't have it, let me know so i can work on one you can use)
- Create JS archive that will be in charge of calling PHP file and show the result in your HTML(if you don't know how to make it let me know so i can help you)
- Create PHP file, this is going to be in charge of consulting your data base and give the result disired for your select (if you don't know how to make it, let me know)
It is pretty easy to make this work, let me know if you need more help.
I am making a website for college administration where professors log in and assign marks to the students they are teaching.
There's a table, called "IA_Marks" in my database:
|Student_ID|Subject_Code|Name|Marks1|Marks2|Marks3|Semester|Division|
There's also a table called "Classroom_Mapper" in my database, that helps map a professor to a classroom, with a subject:
|Prof_ID|Subject_Code|Semester|Division|
This is a method in my controller:
public function showTable(){
$sem = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('semester');
$division = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('division');
$data = DB::table('iamarks')->where([['semester','=',$sem],['division','=',$division]])->get();
return view('ia',compact('data'));
}
Using this, I can fetch rows that belong to the professor who has logged in.
But, there's a problem.
Say the professor teaches two subjects, in two semesters. Then, the where clause will return multiple results from the mapper table.
For example:
select semester from classroom_mapper where Prof_ID=auth()->user()->Prof_ID
output:
8
5
Then the students from both 5th and 8th semester will be shown on his dashboard. Our target semester was, say 5th. Then it'll be a problem.
Registering for a subject, is done as shown here:
form screenshot
Let's call the subject being registered in the screenshot "SUBJECT 4".
It is a subject for the 5th semester, division A.
I want to dynamically make a button(SUBJECT 4) on the dashboard, which when clicked, sends the semester(5) and division(A) of choice to the controller.
Dashboard Screenshot
This button should open a newly made page with name of the subject(subject4.blade.php), where the database table contents for target semester and division(5 and A) will be shown.
How do I make this dynamic view creating button which sends specific info to controller? Is it even possible?
There are a few ways to do this with Laravel, but my goto is usually to create a single blade template for each view (dashboard, subject, etc.) that can be dynamically populated -- assuming that the layout for each subject view is the same.
In your dashboard view, you could generate a url for each button that uses a format like this: http://cas.jce.in/subject/semester/5/division/a/
Next, create a route that uses a couple of paramaters, something like this:
Route::get('/subject/semester/{semester_id}/division/{division_id}', 'ControllerName#showSubject');
More info here: https://laravel.com/docs/5.8/routing#required-parameters
Then in your controller, add a showSemester function like this:
function showSubject($semester_id, $division_id){
$data = DB::table('table_name')->where('semester', '=', $semester_id)->where('division', '=', $division_id)->first();
return view('subject', ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);
}
Your route parameters are available to the controller, in order of appearance. So we can add $semester_id and $division_id as the first two parameters of our function. Next, we'll to the database work to retrieve the data we need before returning everything to a view.
Note here that we're using a single view rather than dynamically selecting one. You could create individual views for each subject, but im thinking you probably don't need to unless the layout of each one is unique in some way. In that case, you can simply do something like this, but I'd generally try to avoid it.
$view = 'subject'.$data->subject_id;
return view($view, ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);
Also, just a quick note ... you may consider adjusting your database queries from above to use a select statement rather than pluck. The end result is the same, but using a select can boost performance by only loading the data you want ... rather than loading everything up front and throwing most of it away.
$sem = DB::table('classroom_mappers')->where('Prof_ID', Auth()->user()->PID)->pluck('semester');
... becomes ...
$sem = DB::table('classroom_mappers')->select('semester')->where('Prof_ID', auth()->user()->PID)->get();
I have a database with employees. the columns are first name, last name, department, internal number etc..
As for today it is a database only for one organization but in future i want to add to this database employees from other relative organizations.
What is the right way to do it:
To add another field to the first table ?
To create another table with 3 fields: id, organization_name, employees ( where in this filed i would put comma separated values of id from first table) ?
if the second answer will be chosen what will happened when an update query will be executed simultaneously from different accounts to the same organization. For example: i will be adding a user with id 55 to organization 'Police' and at the same time another administrator will be adding to the same organization a user with id 65..
In this case is there a possibility of error or data-loss ???
If someone had this kind of problem before, i really would like to read about it..
Thank You..
If the organization is only a number to group the users, then I would suggest to put them into the employees table. However if you have more information about the organization (e.g. name, address .. ) then make a new table for the organization and save the primary key of the corresponding organization in the employees-table.
In a web form i will ask the user for their job experiences, this data will have no fixed lenght. I need to let the user insert all the items he needs, every item will content 3 fields; job title, description and year.
My firts problem is, how can i ask in the html form for the items? i mean, whats the best way to ask items with no fixed lenght using html/php (and maybe ajax)? I saw some sites that have a button (add one) when you hit it a new item slot is showed, but i have no idea of how to implement this, an example will be sufficient.
The second part is, how can i managed the data flow in post or get?, until now, i only use fixed fields, so i always know in my php script how many post or get vars i will get. How can i use multiple POST vars without knowing the amount of them?
And the last one (and the more important), how will be the best structure for my table in MySQL? If i get multiple items for a fixed table where i will have all my users, how can i resolve the multiple items issue? For example, if my table is:
User | password | job_experiences
admin | root | (this is just a cell, how can i save multiple items here?)
jonh | 1234 | (this is just a cell, how can i save multiple items here?)
Thanks for any help!!!!
Those are 3 questions, and it's best to post 3 questions, instead of discussing all of them. I will post the basics, and if you have specific questions, ask.
First, use button to add, and a JavaScript to clone an existing row (which can have more then one input field). For fieldnames use something like company_name[] - the [] is the important part, at this will send the field as an array. If you are editting profile, you can use company_name[$id] to preserve the mapping.
Second, in PHP you will receive this as $_POST['company_name'] which will be numeric array with all the company names. Or if you specify $id - with the corresponding keys. So, you have to loop trough all company_names, if there are other fields - you retrieve them the same way, using the current key. Example:
for (i =0; i<$_POST['comany_name'].length;i++) {
$company = $_POST['comany_name'][$i];
$start_year = $_POST['from'][$i];
...
}
Next, you need 1 table for the users (username, password), and another for job experiences (userid, company, description, from, to). This is called 1:M relation