Changes in Controller not reflecting showing previous return value - laravel

O make change in Controller but it is not effecting it is returning previous changed value not new.
I make many different kind but it is showing same first value which was returned.
I want to return new value from controller
//------------Logout---------------
public function logoutme() {
if (Session::has('RedPachUID')) {
Session::forget('RedPachUID');
return "yes";
}
}
New Function
//------------Logout---------------
public function logoutme() {
if (Session::has('RedPachUID')) {
Session::forget('RedPachUID');
return "No";
}
}

it was happening because I was using anchor tag when i used form to logout it helps me
here is complete solution logout function not invalidating session

Related

How can I persist Laravel session data using Inertia Vue?

I'm having trouble updating session values using Inertia.js. The behavior I was expecting was that the redirect to 'Home' would happen only once. Once the session gets cleared, any page reloads would go into 'Nok.' But that's not what happens. The $request->session()->forget(['ok']); is not persisted and always falls back to 'Home'. How do I get the expected behavior?
Example
public function home(Request $request)
{
$ok = $request->getSession()->get('ok', null);
if ($ok) {
$request->session()->forget(['ok']);
return Inertia::render('Home', [...]);
} else {
return Inertia::render('Nok', [...]);
}
}

Using latestOfMany() in Laravel

If I have a model called Placement which can have many PlacementTqLimit and I want to get the latest one, I'm assuming I can use this.
public function latestLimit()
{
return $this->hasOne(PlacementTqLimit::class)->latestOfMany();
}
And then add a custom attribute to get the value
public function getLatestLimitValueAttribute()
{
if (empty($this->latestLimit())) {
return '100';
}
return $this->latestLimit()->value;
}
However, when I dump the $this->latestLimit() I'm getting a Illuminate\Database\Eloquent\Relations\HasOne... I'm sure I shouldn't have to call return $this->hasOne(PlacementTqLimit::class)->latestOfMany()->latest(). Am I doing something wrong here?
$this->latestLimit() will return hasOne that extend query builder.
If you want to get the latest of PlacementTqLimit model just use $this->latestLimit without () or you can also use $this->latestLimit()->first()
So, to get the latest value limit, example code is:
public function getLatestLimitValueAttribute()
{
if (!$this->latestLimit) {
return '100';
}
return $this->latestLimit->value;
}

return view($viewVariable) from a trait in laravel 8

I have a trait that is being called by several controllers in laravel 8.
Every controller gives the name of the return view allong with function on the trait.
If I do a dd($viewVariable); in the trait then I see the correct refrence to the view. But the trait refuses to return the view. It just gives me a blank screen. First I thought it was a problem with "no quotes", "singel quote" or dubbel quotes" but I tried every variation but without any succes.
I've tried setting the view the normal way but it even refused to render that. I've checked the other functions and variables with the dd($var); and everything is working correct till it's time to return the view.
ChartTrait.php
public function setViewOptionsForChartGeneration($viewVariable)
{
// Check which role the user has in the application
if (Auth::user()->hasRole('admin')) {
$recorderCollection = $this->getRecorderCollectionWhenAdmin();
return view($viewVariable)->withRecorderCollection($recorderCollection);
} elseif (Auth::user()->hasRole('employee|user')) {
// Get all the valid timeslots that belongs to the authenticated user
$userTimeslotCollection = $this->getUserTimeslot();
// Get all the corresponding recorders from the valid timeslots that belongs to the authenticated user
$recorderCollection = $this->onlyCollectRecordersBasedOnValidTimeslot($userTimeslotCollection);
// return the view with all the possibility's the authenticated user has.
return view($viewVariable)
->withRecorderCollection($recorderCollection->flatten())
->withUserTimeslotCollection($userTimeslotCollection);
}
}
There nothing wrong with the function in ChartTrait.php. The Problem was in the controllers. You need to return the function you call to render the view.
Correct way of calling the trait
public function index()
{
$view = "".'content.export.export'."";
return $this->setViewOptionsForChartGeneration($view);
}
Wrong way of calling the trait
public function index()
{
$view = "".'content.export.export'."";
$this->setViewOptionsForChartGeneration($view);
}

How to trigger a method in all pages request in Yii?

In the header section of my website I want to show new message. I have a method that fetches new methods and return them. The problem is that header section is in thelayout section and I don't want to repeat one method in all of my controllers.
How to achieve this by not copying the method to all of my controllers? I want to trigger newMessages() method on every page request to gather new messages for logged in user. How to do this the right way?
In your controller overwrite the oOntroller class function beforeAction()
protected function beforeAction($event)
{
$someResult = doSomething()
if ($someResult == $someValue)
{
return true;
}
else
{
return true;
}
}
The return value can be used to stop the request dead in its tracks. So if it returns false, the controller action is not called, and vice versa().
References : http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail
You can use import controller in another controller action. something like below
class AnotherController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.admin.YourController'); // YourController is another controller in admin controller folder
echo YourController::test(); // test is action in YourController
}
}

MVC3 Controller.Redirect(string)

I have a private function like this in my controller.
private UserDetails GetUserDetails(int userid)
{
...
if (some condition check is false)
Redirect("some other page in a different subdomain");
return userDetails;
}
If the condition fails, Redirect statement is executed, but the execution doesn't stop. userDetails is returned to the calling function. There is no redirect to some other page.
How can I force the redirect?
Redirect returns a result - you're meant to use it from within an Action method as follows:
public ActionResult MyAction()
{
if (check is false)
{
return Redirect("other url");
}
// this code won't get executed after redirect
}
It looks like you should return null from the function you posted above, then check for null and return Redirect("...") if GetUserDetails returned null.
There are situations where you cannot redirect, although I am uncertain of what the stipulation is for the whole set of them. One way to get around that is to return a string to the view and have javascript do window.location(url) to force the redirect.

Resources