Can't get Another field from session - codeigniter

I got problem about session data, in my web, i want to make a different view based on previledge user, here my code in controller :
$previledge = $this->session->userdata['previledge'];
$data['previledge']=$previledge;
if($data['previledge'] == 'admin'){
$this->load->view("app_admin/global/header",$d);
$this->load->view("app_admin/materi/home");
$this->load->view("app_admin/global/footer");
}
else {
$this->load->view("app_admin/global/header2",$d);
$this->load->view("app_admin/materi/home");
$this->load->view("app_admin/global/footer");
}
}
if i run my code above, previledge with admin can't go to the right view.
if there any suggestion, please inform,
thanks

Its something like syntax error.
$previledge = $this->session->userdata('previledge');
This will help you.

Below code must have to work
$this->session->userdata('previledge')=='admin'

Related

Laravel getting ids of a collection without foreach

My code currently looks like this:
foreach ($things as $thing) {
$ids[] = $thing->id;
}
dd(Other::whereIn('thing_id', $ids)->get());
Thing model has many Other
public function others()
{
return $this->hasMany(Other::class);
}
It's working, but can I achieve this functionality without using the foreach? It doesn't seems to be clean for me. I've tried to give whole collection to where in like this:
dd(Other::whereIn('thing_id', $things)->get());
but this only returned where id was 1.
I'm looking for help to clean up this code, any help appreciated.
There is a function called "pluck"
You can apply it on a collection as following
$collection->pluck('id');
More can be seen on docs
https://laravel.com/docs/7.x/collections#method-pluck
I've already found a way to clean it up a bit, instead of foreach I can simply use :
$ids=$things->pluck('id');
If there's a cleaner way pls show me :)

getClientOriginalName() on null

I have a little problem with one request file in my Store controller !
I get the orginal file name extension like this :
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
Sometimes the user don't need to put a file "cert_medical_surclassement" i would like to do a condition like : If the file was added ok but if there is no files in the field i pass to the other variable .
Someone know how i could do this ? thanks a lot in advance
Use the hasFile() method
if ($request->hasFile('cert_medical_surclassement')) {
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
} else {
// else code
}
More info: Documentation

findOrFail() in Query\Builder

DB::table('amounts')->findOrFail($id);
return
Call to undefined method Illuminate\Database\Query\Builder::findOrFail()
I don't want to use models here.
What's the best way to achieve this ? (I just want to return a 404 when the id is not in the table.)
In the meantime I do:
$amount = DB::table('amounts')->find($request->input('amount'));
if(!$amount) abort(404);
As you can see in this API documentation
https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_find
https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_findOrFail
find($id) is Database Builder method but findOrFail($id) is not so you cannot use it directly that way.
I didn't really get why you don't want to use Eloquent Model but thats the good way to do or stick with what you are doing in meantime.
You can try with below code to get record if exits into table or not:
$result = DB::table('amounts')->find($id);
if($result == NULL) //check if no record found
{
//do your stuff here
}
else //if record found from table
{
//do your stuff here
}
Hope this helps.
If you don't want to use models then that will do the job. If you use models you can try and catch the
\Illuminate\Database\Eloquent\ModelNotFoundException
and abort 404 at that point.

Laravel Debugger showing duplicated queries

Does any one has an idea about this. I don't know why it is showing duplicated queries. I searched a lot, found one answer on stackoverflow, but didn't get proper answer. If anyone faced the same error then please let me know. Thank you
protected $_param;
public function __construct(Utility $utility)
{
$league = $utility->checkDomainController();
view()->share('league', $league);
$this->league = $league;
}
This is the code in controller. which shares league to all the views. But there is only one query in $league = $utility->checkDomainController();
Here is the checkDomainController
if(\Request::server('HTTP_HOST') == env('MAIN_DOMAIN'))
{
$leagueSlug = Route::current()->getParameter('league');
$league = League::where('url', $leagueSlug)->first();
}
else
{
$league = League::where('custom_domain', \Request::server('HTTP_HOST'))->first();
}
if(!$league)
{
//error page
}
return $league;
Anytime you call a property which relies on a related model in blade, Laravel executes a new query, unless you eager load and pass from the controller to the view the preloaded data.
This example you posted seems to me as a loop somewhere within your blade code. Maybe if you can share a bit more of the code related to the front-end, I can help you figure it out.

laravel 4.2. user permissions and hiding link source

I am a total newbie with Laravel and learning it now for a week. I have some basic questions that I can't find an answer to. Next week I will start with developing CRM system and I need some info from experienced developers who could tell me is the approach I am attending to make a good one.
I will need some authentication system, like 4 groups of users (Admin, Basic, Manager, Office) where Manager and Admin will add the Basic users. There will be few view and features and every groups will have defined access to each view and feature. Since few days I am searching for packages, watching the tutorials and learning. I found an interesting package for which I think it could help me with this user-group-permission things.The package is Sentry. Could this help me with my requirements?
What is the case when for example I have a user in group Basic and he deletes for example some comment with the button. On the left side down in the browser the user can see the link to this comment when he hovers the link. For example www.test.com/comments/345/delete where the id is 345. What if user types that with another id, that means he can delete another comment. I found some suggestions on how to solve this, to make it with jQuery and javascript so the link wouldn't be shown and POST would be made with for example with AJAX. But since I am a newbie, I am thinking how much time would this take and is this a good approach at all? Could package Sentry from 1. question help me with the permission on what route each group can access?
Any help or advice would be appreciated.
Sentry does what you want, yes. Here's a question with some answers explaining the permissions part.
The visible link part can be avoided by doing a POST request instead of a GET request.
When you open your form, you add a method attribute.
Form::open(array('url' => 'foo/bar', 'method' => 'post'))
A GET request will put the parameters in the URL, hence the visible ID. Using a POST request will put the parameters in the headers, thus hiding it from the URL.
An example could be deleting a comment. A GET request could look like this:
http://www.example.com/comments/delete/1
And the parameters would be defined in your method signature:
public function getDelete ($id) {
Comment::find($id)->delete();
}
Where the POST equivalent would be
http://www.example.com/comments/delete
And the parameters would be defined in your Input class, you would get them using the get method
public function postDelete() {
Comment::find(Input::get('id'))->delete();
}
1) The best package to help you with that is Sentry indeed.
2) To make sure an user can delete only his comments you can do something like this (but there are more solutions either you do it with Ajax or not):
public function destroy($id) {
$user = Sentry::getUser();
$comment = Comment::find($id);
if($comment) {
if($comment->user_id != $user->id) {
return Response::back(); // optional message: Permission denied!
}
$comment->delete();
return Response::back(); // optional with message: Deleted!
}
return Response::back(); // optional message: Comment not found!
}
You can use Sentry in this case to get the logged in user and check for user id. I think you should let user delete their own comments always but if you need special roles (Admins for example) to be able to delete any comment, or special permission comments.delete (For some Managers) - you can use Sentry as well:
public function destroy($id) {
$user = Sentry::getUser();
$comment = Comment::find($id);
if($comment) {
if($comment->user_id != $user->id && !$user->hasRole('Admin') && !$user->hasPermission('comments.delete'))) {
return Response::back(); // optional message: Permission denied!
}
$comment->delete();
return Response::back(); // optional with message: Deleted!
}
return Response::back(); // optional message: Comment not found!
}
A nicer way of making the DELETE thru a Form request check this:
Laravel RESTfull deleting

Resources