I've working on User custom permission in Laravel 4, After login permissions(json string) stored in Auth::user()->permissions. as following:
$permissions =array(101,102);
DB::table('user')->where('id', Auth::user()->id)->update(['permissions' => json_encode($permissions)]);
But while I've checking permissions every time need to decode it to array:
if(in_array(1000, json_decode(Auth::user()->permissions)){
}
but I want something that make it to work like following:
if(in_array(1000, Auth::user()->usr_rights){
}
You can add a Accessor to your model :
public function getPermissionsAttribute($value)
{
return json_decode($value);
}
and all you have to do is this :
if(in_array(1000, Auth::user()->permissions){
}
You could use Implode
This isnt the nicest way of doing it but works.
Example:
$array = array('lastname', 'email', 'phone');
comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
U could save this string in an additional row in user table.
and then convert it back with Explode
$pizza = "piece1, piece2, piece3, piece4, piece5, piece6";
$pieces = explode(",", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Related
Controller code
public function add_employee(){
$covid_statuses = Http::get("http://localhost:8000/api/v1/covid/status");
$covid_statuses = json_decode($covid_statuses['data'],200);
// $covid_statuses = json_encode($covid_statuses['data'],200);
return view('admin.hr.os.employee.add_employee',compact('covid_statuses'));
// return view('admin.hr.os.employee.add_employee',[
// 'covid_statuses' => $covid_statuses,
// // 'covid_statuses' => json_decode($covid_statuses['data']),
// ]);
}
The main respone is like {"data":[{"id":1,"name":"1st dose completed"},{"id":2,"name":"2nd dose completed"}],"success":true,"status":200}
When I decode only data, and pass into blade and in blade I use to debug like #foreach($covid_statuses as $covid_status) {{$covid_status}} #endforeach
but I got
json_decode() expects parameter 1 to be string, array given
When I use encode then I got
Invalid argument supplied for foreach()
How Can I solve this issue?
You could use array_walk for that purpose. Try:
$covid_statuses = array_walk($covid_statuses['data'], function(&$item, &$id) {
$item = $item['name'];
$id = $item['id'];
});
Like this, the array will only contain the names of the covid cases, by it will have the same id as in the original request
Here is the code in which I get the user if it's g10_eng_unwh is equal to 1.
public function purchased(User $user)
{
if ($user->g10_eng_unwh == 1) {
$token = "g10_eng_unwh";
}
if ($user->g7_eng_unwh == 1) {
$token = "g7_eng_unwh";
}
$time = Carbon::now()->format('Y-m-d');
dd($user->email, $time, $token);
}
Instead, I want to get the user only when it's g10_eng_unwh value changes from 0 to 1.
Is there any way?
You need to get the past value at the beginning so that you can apply the condition, it will have to be stored somewhere. Most likely you will need to create g10_eng_unwh_old or something like that. I assume that g10_eng_unwh is a column in the database, in this case create g10_eng_unwh_old and store the old value in it.
In this case, you could get it even on request. For example
DB::query()
->where('g10_eng_unwh', 1)
->where('g10_eng_unwh_old', 0)
->get();
I am trying to think of a way that a user can add a type(medical) on the frontend but a verifier has to approve that record? i can't seem to figure out the best solution for this, does anyone have any suggestions on how to make this work? It's getting stored in the medical table but not in Verifier table.
In Simple words, I am using the medicalRecord Controller to stored medical and Verifier details.
'document_id' = the medical_id;
'submitted_by' = who created the record
public function store Request $request, $id)
{
if (auth_user_cannot(Capability::CREATE_DRIVER_MEDICAL_RECORD)) {
return redirect($group['name'] . "/" . $group['userId']);
}
$all = $request->all();
if ($request->hasFile('myfile')) {
$result = upload($request->file('myfile'), 'nonzip', 'Medical');
$all['upload'] = $result['upload'];
$all['uploadId'] = $result['uploadId'];
}
// u'll need to save the medical to the user first, then save the verifier to the medical.
$driverMedical = DriverMedicalRecord::create($all);
// Then for the relation;
$medical = $driverMedical->find($id);
$verificationData = DocumentVerification::create( [
'document_id' => $medical,
'submitted_by'=> Auth::id(),
]) ;
$request->user()->posts()->save($post);
$driverMedical->verifier()->save($verificationData);
set a flag in the same table and change its value when it got verified.
ex: is_verfied it should be 0 when it is not approved when it got approved change it to 1 (this is what i understand from your question)
Find below the controller code:
public function domaincheck()
{
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=check&sld=unusualTVname&tld=tv&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);// Use true to get data in array rather than object
// dd($final_data);
$response1 = file_get_contents('http://resellertest.enom.com/interface.asp?command=GETNAMESUGGESTIONS&UID=resellid&PW=resellpw&SearchTerm=hand&MaxResults=5&ResponseType=XML');
$data1 = simplexml_load_string($response1);
$configdata1 = json_encode($data1);
$final_data1 = json_decode($configdata1,true);// Use true to get data in array rather than object
//dd($final_data1);
$response2 = file_get_contents('https://resellertest.enom.com/interface.asp?command=gettldlist&UID=resellid&PW=resellpw&responsetype=xml');
$data2 = simplexml_load_string($response2);
$configdata2 = json_encode($data2);
$final_data2 = json_decode($configdata2,true);
//dd($final_data2);
return view('clientlayout.main.test',array('final_data1' =>
$final_data1), array('final_data' => $final_data),
array('final_data2' => $final_data2));
}
Find the view code given below:
{{print_r($final_data)}}
<br><br><br>
{{print_r($final_data1)}}
<br>
{{print_r($final_data2)}}
Find the route code given below:
Route::get('/test','EnomController#domaincheck');
I need to return all the three arrays to the view page but When use the return view code giveb above I'm getting error as "Undefined variable: final_data2 " for the third array alone.If I declare only two array statements in the return view it works correctly. Suggest me a solution to solve this error.
in addition to #achraf answer if you dont want to use same variable names in your view as of your controller, you can pass data to your view like this. where final1, final2 and final3 will be the names of the variable in your view.
return view('clientlayout.main.test',['final1' =>$final_data ,'final2'=> $final_data1,'final3' => $final_data2]);
View take second param as array, your sending multiple arrays,
the solution is to create an array of arrays like this
return view('clientlayout.main.test',compact('final_data','final_data1','final_data2'));
I find an existing record like so:
$m = new Model();
$value = 1;
$m->whereField($field)->first(); // Get the model
if ($m->exists()) { // this is true so I know it's there
$m->anotherField = $value
$m->save(); // Laravel tries to INSERT vs UPDATE. Why????
}
I know the record can be found because exists() is true. Yet I try to save it and Laravel treats it like a brand new record and tries to insert instead of update. What did I do wrong here?
You need to set a variable to the found item
$m = $m->whereField($field)->first();
if ($m->exists()) {
$m->anotherField = $value;
$m->save();
}
As you have it now, $m is only refering to new Model(), not to the found item.
You could also do:
$m = $m->whereField($field)->firstOrFail();
//catch exception... (log or something else)
$m->anotherField = $field;
$m->save();
Doing this will let you catch the exception so you can log and display
an error page as necessary. To catch the ModelNotFoundException, add
some logic to your app/Exceptions/Handler.php file.
http://laravel.com/docs/5.0/eloquent