In my user table i have a role column its values Teacher and Student. I want to insert data if user role teacher. Otherwise give a message "Only for teacher".
Here is my controller:
$validator = Validator::make($request->all(), [
'title' => 'required|exists:users,role==[Teacher]',
]);
if ($validator->fails()) {
Flash::success('Only for Teacher');
return redirect(route('works',['class_id'=>$class_id]));
}else{
$works = new assainments();
$works -> title = $text;
//$works -> file = $fileName;
$works -> class_id = $class_id;
$works -> users_id = Auth::user()->id;
$works -> save();
Flash::success('Your works has been posted');
return redirect(route('works',['class_id'=>$class_id]));
But it's not working. It's only Show the error message "Only for Teacher"
laravel 5.2
The exists validator rules work given table , column and extra where conditions. See the more definition :
https://laravel.com/docs/5.2/validation#rule-exists
But you can solve this on another way by checking a if condition :
if( \Auth::user()->role == 'Student' )
{
Flash::success('Only for Teacher');
return redirect(route('works',['class_id'=>$class_id]));
}
Related
I got this problem trying to get id by comparing user input with data in database. I am new in this and I tried some solution which not help at all.
Below is my controller code:
public function assign(Request $request)
{
$courselist = new Courselist;
$coursecode = $request->input('coursecode');
$courseid = DB::select("select id from course where course_code = '$coursecode'");
$courselist -> course_id = $courseid;
$courselist -> coordinator = $request->input('coordinatorname');
$courselist -> internal = $request->input('imname');
$courselist -> external = $request->input('exname');
$courselist -> dean = $request->input('deanname');
$courselist -> save();
return back()-> with('alert-success','Data successfully added!');
}
$course = DB::table("your table name")->where("course_code ", "=", $coursecode)->first();
$courselist -> course_id = $course->id;
try this i hope it will work.
The problem is when I entered a new name no data is added. A similar thing happen when I entered an already existing name. Still, no data is added to the database. I am still new to CodeIgniter and not entirely sure my query builder inside the model is correct or not.
In the Model, I check if the name already exists insert data only into the phone_info table. IF name does not exist I insert data into user_info and phone_info.
Controller:
public function addData()
{
$name = $this->input->post('name');
$contact_num = $this->input->post('contact_num');
if($name == '') {
$result['message'] = "Please enter contact name";
} elseif($contact_num == '') {
$result['message'] = "Please enter contact number";
} else {
$result['message'] = "";
$data = array(
'name' => $name,
'contact_num' => $contact_num
);
$this->m->addData($data);
}
echo json_encode($result);
}
Model:
public function addData($data)
{
if(mysqli_num_rows($data['name']) > 0) {
$user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
$user_id = $user['id'];
$phone_info = array(
'contact_num' => $data['contact_num'],
'user_id' => $user_id
);
$this->db->insert('phone_info',$phone_info);
} else {
$user_info = array(
'name' => $data['name']
);
$this->db->insert('user_info', $user_info);
$user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
$user_id = $user['id'];
$phone_info = array(
'contact_num' => $data['contact_num'],
'user_id' => $user_id
);
$this->db->insert('phone_info', $phone_info);
}
}
DB-Table user_info:
DB-Table phone_info:
Extend and change your model to this:
public function findByTitle($name)
{
$this->db->where('name', $name);
return $this->result();
}
public function addData($data)
{
if(count($this->findByTitle($data['name'])) > 0) {
//.. your code
} else {
//.. your code
}
}
Explanation:
This:
if(mysqli_num_rows($data['name']) > 0)
..is not working to find database entries by name. To do this you can use codeigniters built in model functions and benefit from the MVC Pattern features, that CodeIgniter comes with.
I wrapped the actual findByName in a function so you can adapt this to other logic and use it elswehere later on. This function uses the query() method.
Read more about CodeIgniters Model Queries in the documentation.
Sidenote: mysqli_num_rows is used to iterate find results recieved by mysqli_query. This is very basic sql querying and you do not need that in a MVC-Framework like CodeIgniter. If you every appear to need write a manual sql-query, even then you should use CodeIgniters RawQuery methods.
I have many attributes for the users' table. Admin needs to register a new user. My question is how to log in only with email and password?. my code now when user login user will get this error and this users database table . I only want to insert email and password when login in.
Controller\Auth\AuthController.php
public function login(Request $request)
{
if (!\Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect()->back();
} else {
return view('layouts.admin');
}
}
Http\UserController.php
public function store(Request $request)
{
$user = new User();
$user->Id_staff = $request['Id_staff'];
$user->name = $request['name'];
$user->noIc = $request['noIc'];
$user->email = $request['email'];
$user->password = bcrypt($request['password']);
$user->pusat_tangungjawab = $request['pusat_tangungjawab'];
$user->jawatan = $request['jawatan'];
$user->user_group = $request['user_group'];
$user->user_level = $request['user_level'];
$user->phone_no = $request['phone_no'];
$user->save();
return redirect()->route('users.index');
}
If you create a migration for users table, maybe the reason for that error because you didn't set nullable for Id_staff. So if you create a user and didn't post a value of Id_staff you will get that error. Some solutions is set a default value by random or set nullable in your migration, for example:
$table->string('Id_staff')->nullable();
I am doing a peer marking system which requires a function that lecturer adds id list and when students enroll in a course, he enters his id needed to match the id on lecturer id list.
Controller
public function store(Request $request)
{
$this->validate($request, [
'course_code' => 'required',
'studentid' => 'required'
]);
$enrollment = new Enrollment;
$enrollment->user_id = auth()->user()->id;
$enrollment->course_id = $request->course;
$enrollment->user_StudentID = $request->studentid;
$input_course_id = $request->input('course_code');
$input_studentid = $request->input('studentid');
$course = Course::find($enrollment->course_id);
$course_identifiers = $course->identifiers;
// Need all the data in the database course table for comparison
//$course represents the contents of the course table in all databases, then you need to loop first, then judge
//$course stands for list $signleCourse for each piece of data
foreach ($course_identifiers as $course_identifier) {
// if ($course_identifier->studentid == $input_studentid )
if ($input_studentid == $course_identifier->studentid) {
if ($request->course == $input_course_id) {
//if true,save and redirect
$enrollment->save();
return redirect('/enrollment')->with('success', 'Course Enrolled');
} else {
return redirect('/enrollment')->with('error', 'Please Enter Correct Confirmation Code');
//If false do nothing
}
} else {
return redirect('/enrollment')->with('error', 'Please Enter Correct Student ID');
//If false do nothing
}
}
}
It can only match the first value, but other values I enter cannot be recognized.
Turn off your redirects. It's really hard to understand the context of that code but it looks like if it fails to match it redirects so doesn't go through the second and subsequent values of $course_identifiers.
I have built a contact page using laravel-4.2 allowing users to submit data. My contact page has a lot of information and the submitting form is at the bottom of the page. every time there is an error when submitting the form, the page refresh and i have to scroll down to the form to actually see the errors on the page.How to fix this. I guess i have to use ajax.
here is my ContactController.php
<?php
class ContactController extends BaseController{
public function getIndex (){
return View::make('Contact.contact');
}
public function postContact(){
$validator = Validator::make(Input::all(), array(
'name'=>'required',
'email'=>'required|email',
'sujet'=>'required',
'telephone'=>'required|numeric',
'message'=>'required'
));
if($validator->passes()){
$contact = new Contact();
$contact -> name = Input::get('name');
$contact -> email = Input::get('email');
$contact -> subject = Input::get('subject');
$contact -> telephone = Input::get('telephone');
$contact -> message = Input::get('message');
$contact->save();
return Redirect::route('contact')
->with('success','Message sent!');
}else{
return Redirect::route('contact')->withErrors($validator)->withInput();
}
}
}