Laravel check input in array - php

How can I check if my form input value exist in controller array?
Code
$admins = User::role(['admin', 'superadmin'])->pluck('id'); // get array of admins id
if($request->input('user_id') == $admins) { // if user_id is include admins id...
// do something
} else {
// do something else
}

Use in_array (docs) to check whether something exists in an array.
if(in_array($request->input('user_id'), $admins)) { // if user_id is include admins id

Try this:
if(in_array($request->input('user_id'), $admins)) { // if user_id is include admins id...
// do something
} else {
// do something else
}

You could as well perform it in a single query:
$user_admin = User::role(['admin', 'superadmin'])->find($request->input('user_id')); // returns null if not found or the $user if found
if($user_admin) { /
// do something
} else {
// do something else
}

If you are working with collections you can use their methods.
In this case you could use contains() method.
$admins = User::role(['admin', 'superadmin'])->pluck('id'); //this return a collection
if($admins->contains($request->input('user_id'))) { // use the contains method of collection
// do something
} else {
// do something else
}

Related

checking data exists or not when editing the data in codeigniter

I have a problem when editing data from the database.
this is my controller function
public function cek_nip_update()
{
$this->load->model("model_dosen");
$unique_id = $this->input->post('unique_id');
$nip = $this->input->post('nip');
$hasil_nip = $this->model_dosen->cek_nip($nip);
$hasil_unik = $this->model_dosen->cek_unik($unique_id);
$hasil_cek = $this->model_dosen->cek_nipp($nip,$unique_id);
if (count($hasil_nip)==0)
{
echo 'gada';
}
else
{
if ($hasil_nip==$hasil_cek) {
echo "gada";
}
else
{
echo "ada";
}
}
}
and this is my model
public function cek_nip($nip)
{
$cek = $this->db->query("select nip from detail_dosen where nip='$nip'");
return $cek->result();
}
public function cek_unik($unique_id)
{
$cek = $this->db->query("select nip from detail_dosen where unique_id='$unique_id'");
return $cek->result();
}
public function cek_nipp($nip, $unique_id)
{
$cek = $this->db->query("select nip from detail_dosen where nip='$nip' and unique_id='$unique_id'");
return $cek->result();
}
The result I want is
When I type nip not exist, the span I make will not show something
When I type nip exits in the input, but with the same unique_id, the span will not show something
When I type nip exits in the input, with the different unique_id, the span will show "NIP is used by the other"
My code just works with number 1 condition, but when I type nip exists in the database, it shows "NIP is used by the other".
Can someone help me so my code can run like what I mentioned above?
Thanks in advance
In your model try this
return $this->db->get_where('detail_dosen', array('nip'=>$nip))->num_rows(); //it will give you number of rows in return. so you can check like this if(count($hasil_nip) > 0) according to your condition
if you want to get more result you can use
return $this->db->get_where('detail_dosen', array('nip'=>$nip))->result_array(); // OR row_array(); For this you can check like this if($hasil_nip){ //coding... }

How to check the given values are updated are not into Database in php

I'm doing webservice using laravel,Here I need to send response after the value get updated into database...
I tried something like this,
public function getPhoneverify(){
$_REQUEST['user_id'] = str_replace('"','', $_REQUEST['user_id']);
$_REQUEST['status'] = str_replace('"','', $_REQUEST['status']);
$user = \DB::table('tb_users')->where('id', $_REQUEST['user_id'] )->update(array('phone_verified' => $_REQUEST['status']));
if($user)
{
echo "success";exit;
}
else
{
echo "failed";exit;
}
}
But here,always it shows the else part message,even if the value get updated into the database..
How should I do this..
Is there any other option to do this!!..
Someone help me..
If you need to check if the query was successful, I'd suggest a different approach. Assuming the user_id field is unique, following should work:
$user = \DB::table('tb_users')->where('id', $_REQUEST['user_id'] )->first();
Or you can also retrieve the user like this:
$user = \DB::table('tb_users')->find($_REQUEST['user_id']);
And then update/save it:
$user->fill(array('phone_verified' => $_REQUEST['status']));
$saved = $user->save(); //this will always return true or false.
if($saved)
{
echo "success";exit;
}
else
{
echo "failed";exit;
}
You can use exception handling if you want
try {
$user = \DB::table('tb_users')->where('id', $_REQUEST['user_id'] )->update(array('phone_verified' => $_REQUEST['status']));
}catch(\Exception $e){
//write statements here if query fails
}
By the way as far I know DB::update() returns boolean
The return value will never be true since it returns the number of rows affected by the database transaction.
So you should check for the integer value, not if it is true or false

Php function include

So, I'm working on a dynamic forum signatures script and I alredy got it working. Now I will to make it so that only a specific user group can a specific design.
This is the function I made.
function userGroup($rank)
{
if ($rank == 38)
{
return true;
}
else
{
return false;
}
}
And used it like this.
if ($userGroup == true)
{
...
}
else echo('User with that ID doesn\'t exist in the database');
}
else echo('This user hasn\'t in the specific user group');
But it wont work like that.
Regards,
Lazar!
You made a function function userGroup($rank) and you are trying to call it as if its a variable $userGroup. What happened to the parameter? Your if-statement should be something like:
if (userGroup($var) == true) { ... }
You don't actually need the == true either:
if (userGroup($var)) { ... }
you can not use a function like this. you should have a function call with valid argument like this:
if (userGroup($intUserRank))
{
...
}
else echo('User with that ID doesn\'t exist in the database');
assume $intUserRank containing user rank.
You function could just be like this:
function userGroup($rank)
{
return ($rank == 38);
}
Then you would use it like this:
if (userGroup($some_rank))
{
...
}

Function parameters when variables values are not set

How can I call a function without throwing any errors when certain variables are not set?
For example, I need to logUserActivity($uName,$uId). This'll work ok if a user is logged in and both those variables are set. However, it will not work if a user is not logged in, and will throw an error.
In that case can, something be done to make those two values NULL?
I did this: logUserActivity($uName=NULL,$uId=NULL) but this makes them forever NULL.
I also did function logUserActivity($uName=NULL, $uId=NULL), by this still throws an error.
You have to set default value of a function to make it optional
Like try this
function logUserActivity($uName='', $uId='')
function logUserActivity($uName=NULL, $uId=NULL){
if($uName != ''){
echo 'Uname is set - '.$uName;
}
if($uId != ''){
echo 'uId is set - '.$uId;
}
}
logUserActivity(NULL,20);//O/p - uId is set - 20
logUserActivity('Test',NULL);//O/p - Uname is set - Test
logUserActivity('Test',20);//O/p - uId is set - 20,uId is set - 20
logUserActivity(NULL,NULL);//NO O/p
Write like this:
function logUserActivity($uName=NULL, $uId=NULL) // If nothing is passed it will take it as null
{
if($uName==NULL && $uId == NULL) {
//User is not logged in
}
else
{
//user is logged in
}
}
And Call it as when user is logged in
logUserActivity("userName", 12345);
And When user is not logged in
logUserActivity();
For more information you can check this out:http://us2.php.net/manual/en/functions.arguments.php
function logUserActivity($uName=null,$uId=null) {
if ( !isset($uName) || !$uName ) {
$uName = 'some default'; // optionally, return; could be sued
}
if ( !isset($uId) || !$uId) {
$uId = 'some default'; // optionally, return; could be sued
}
}

How to get value an array laravel 4?

I just want to ask, how do I get the value of this code with laravel 4?
$auth = DB::select(
'SELECT auth FROM users WHERE username like ?'
, array($username)
);
When I display the result of the code, its echoing "Array" and it always redirecting the user to 'anotherpage' I'm actually doing like:
if (Auth::attempt($userdata))
{
if ($auth==0)
{
return Redirect::to('home');
}
elseif ($auth==1)
{
return Redirect::to('dashboard');
}
else
{
return Redirect::to('anotherpage');
}
}
Please help. Thank you in advance.
First of all, why are you authenticating the user twice through Auth class and manual SELECT query? Auth::attempt is enough. Read it here.
Anyway, assuming you really wanna do it that way, your code isn't working properly because you're assigning $auth to 0 within the if statement; so this :
if($auth = 0)
Is basically this:
if(0) //Which is always false
So, i'd change the if statement to :
if($auth == 0)
Your final code should look like this:
if(Auth::attempt($userdata))
{
$auth = DB::table('users')->where('username', '=', $username)->first()->auth;//Don't use "LIKE" as it may cause huge security issues.
if($auth == 0) {
return Redirect::to('home');
}
else if($auth == 1) {
return Redirect::to('dashboard');
}
else {
return Redirect::to('anotherpage');
}
}
That's what happens when you try to echo an array. Use print_r() instead. It prints a readable output of the contents of an array (or a variable):
$myArray = DB::select('SELECT auth FROM users WHERE username like ?', array($username));
print_r($myArray);
That way, you'll see the contents of the array, and you can then display the specific item.
Enclosing it in <pre> tags will make the output, even more readable:
echo '<pre>';
print_r($myArray);
echo '</pre>';
Or simply:
echo '<pre>', print_r($myArray), '</pre>';
You're using the query builder wrong. Try this:
$auth = DB::table('users')->select('auth')->where('username', 'LIKE', $username)->get();
dd($auth);

Categories