Why laravel view data persists during testing? - laravel

I have encountered an awkward bug. I have the following code:
// if user clicked 'register with Facebook' button
if(session()->has('providerUser')){
$name = explode(' ', session('providerUser')->getName());
view()->share([
'first_name' => $name[0],
'last_name' => $name[1] ?: '',
'email' => session('providerUser')->getEmail()
]);
}
I have another method that clears the session, so when I get redirected back the view does not have variables $first_name, $last_name, $email.
But when I do the same checks in the session - they are present. What is strange - that {{session()->has('providerUser')}} is empty in the view and I have also debugged the controller it does not get inside the if(){} statement. And I am sure I am not declaring those variables anywhere else.

Related

Can't get data from Auth::user()

I am new to Laravel.
I am using external API into login page
So I use this into login controller
note : $userId, $nama are data I get from external API, and I want it to store in Auth.
$user = [
'id' => $userId,
'nama' => $nama,
'prodi' => $prodi,
'email' => $email,
'nidn' => $nidn,
'nip' => $nip,
'jabatanFungsional' => $jabatanFungsional,
'keaktifan' => $keaktifanDosen
];
$user = new AuthUser($user);
Auth::login($user);
return view('pages.biodata');
in view biodata, I use this code and that works
{{ auth()->user()->nama }}
but when I go to another view where code
{{ auth()->user()->nama }}
is still using, I got error:
Attempt to read property "nama" on null
even though I still don't use syntax Auth::logout()
Can some one tell me what is wrong?
I want to login using external API, and get data from that so I can pass that data to my view using Auth::user()->somedata.
For first return view from controller, I can do Auth::user()->nama and display a text, but when I go to another view and using Auth::user()->nama, an error Attempt to read property "nama" on null displays.

Backpack Laravel Admin Incorrectly Redirects after Editing Model

I have a CrudController created for a model using the Backpack Laravel Admin Library.
When I update the model it redirects me incorrectly to a 404 page with the message No query results for model [App\Models\Group].
It is redirecting me to the incorrect URL from what I can tell.
admin/group/261/ instead of admin/group/261/edit
The model also does not update.
I have the "Save and Edit" option set on the green save button. If I try to change this I get the same error, but it doesn't update.
I'm able to save every other model correctly.
The update method in the CrudController is just the following. I've removed all the extra code.
public function update(){
$response = $this->traitUpdate();
return $response;
}
Figured this out. It was because I was referencing the Primary Key -> 'id' in the fields within the Group Crud Controller.
$this->crud->addField([
'name' => 'id',
'type' => 'text',
'attributes' => ['disabled' => 'disabled'],
]);
u can use id, u need delete attribute 'disabled' like this:
[
'name' => 'id',
'label' => 'ID',
'attributes' => [
'readonly' => 'readonly',
],
],

Laravel exceptions not working at all. It return phpinfo.php in every case

Hi guys i'm working with laravel 5.6, i have enabled debugging in .env but still it is not returning any exception and error.
instead of any error or exception it shows a complete page of phpinfo.php
here is an example image what i am actually getting
https://www.hostinger.com/tutorials/wp-content/uploads/sites/2/2017/03/inside-the-php-info-section.png
Let me show you my code
public function store(Request $request)
{
$request->validate([
'first_name' => 'required|min:3',
'last_name' => 'required|min:3',
]);
...
}
the desired output was that if i have not entered any field i.e first_name or last_name it should provide an error that first_name or last_name is required but instead of this it return complete phpinfo.php page
Although the code sample you have provided does not have any error
I think you should try using the validation like this
$this->validate($request, [
'first_name' => 'required|min:3',
'last_name' => 'required|min:3',
]);
If you still face the error please try commenting you validation code for instance or using a dd() after validation so that we could ensure that the error is in the validation part or somewhere else.

Laravel, Forcing A Unique Rule To Ignore A Given ID

I am following the instructions from the Laravel Documentation but I can't seem to make the rule work. Basically, I want to add a unique rule for both email and username fields for updating the user profile, but it doesn't seem to work. Am I missing something?
Here is the function for updating user credentials
public function updateCredentials(Request $request, User $user)
{
Validator::make($request->all(), [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'username' => [
'required','string','min:4','max:255',
Rule::unique('users')->ignore($user->id),
],
'email' => [
'required','string','email','max:255',
Rule::unique('users')->ignore($user->id),
],
'asdf' => 'required',
'type' => 'required',
])->validate();
}
Thanks in Advance!
If you want to update user profile,without changing his id,you dont need to set specific rule for that.
Without explicitly setting the new user id,it should stay the same.
You can just name the data you want to change like this:
$user->name = request('name');
$user->lastname = request ('lastname);
$user->email = request('email');
...
$user->save();
session()->flash('message','Profile update complete!');
return redirect('/home');
Doing this the data you want will update,but the id will stay the same.
Seems to be some bug.
Adding filed name worked for me:
Rule::unique('users')->ignore($user->id, 'users')

Elimiating the need to check for the user in controller

To the point, I'm trying to tighten up my code and stop repeating myself. It's a constant struggle, haha
In both my views and my controllers I am checking to see if the user is signed in. That is fine, but I would like to only have to check to see if the user is in the view and then display the right content.
Originally, in my controller, I'm seeing if the user is there and then if it is I load the same views with different varables. It seems a bit repetative.
public function recentStories(){
//This pulls back all the recent stories
if (empty($this->user)) {
$this->load->view('header_view', array(
'title' => 'Recent Stories',
));
$this->load->view('storyList_view', array(
'query' => $this->data_model->pullAllStories(),
));
$this->load->view('footer_view');
}else{
$this->load->view('header_view',array(
'user' => $this->users_model->getUser($this->user->user_id),
'title' => 'Recent Stories',
));
$this->load->view('storyList_view', array(
'query' => $this->data_model->pullAllStories(),
'admin' => $this->admin_model->pulladminRecent(),
'user' => $this->users_model->getUser($this->user->user_id),
));
$this->load->view('footer_view');
}
}
Ultimitly, I would like to reduce that chunk down to something like this.
public function recentStories(){
//This pulls back all the recent stories
$this->load->view('header_view',array(
'title' => 'Recent Stories',
));
$this->load->view('storyList_view', array(
'query' => $this->data_model->pullAllStories(),
'admin' => $this->admin_model->pulladminRecent(),
'user' => $this->users_model->getUser($this->user->user_id),
));
$this->load->view('footer_view');
}
but when I do reduce that code and take out the part that checks the user I am getting back an error saying that I'm trying to get property of a non-object. Obviously, that is talking about the user.
To try and fix this I have tried both
<?if(isset($user)){
user header code in here
}else{
non-user header code in here
}?>
and
<?if(!empty($user)){
user header code in here
}else{
non-user header code in here
}?>
both have returned back "Trying to get property of non-object" Any ideas would be much welcomed. I really don't like having excess code.
public function recentStories(){
// default values
$header_data = array('title' => 'Recent Stories');
$storyList_data = array('query' => $this->data_model->pullAllStories());
// extended data for logged user
if (!empty($this->user)) {
$header_data['user'] = $this->users_model->getUser($this->user->user_id);
$storyList_data['admin'] = $this->admin_model->pulladminRecent();
$storyList_data['user'] = $this->users_model->getUser($this->user->user_id);
}
// load your views
$this->load->view('header_view', $header_data);
$this->load->view('storyList_view', $storyList_data);
$this->load->view('footer_view');
}
then in your views check "if (isset($user)) {" etc.

Resources