Laravel redirect inside of middleware? - laravel

I have a middleware check by login, how many games has the user, I want to show another view if user have more than one game, but I got everytime redirect error if user has more than one:
This is in case $assigned_games > 1
$games = Auth::user()->userGames;
$assigned_games = count($games);
if ($assigned_games == 1) {
return $next($request);
} elseif ($assigned_games > 1) {
return redirect()->route('games.board');
} elseif ($assigned_games < 1) {
echo "no game bought";
exit;
}
I tried also only with return route('games.board') but it don't work.
How can I set correct redirect here?

if both your route is inside middleware then you should check the current route and if its same as you are redirecting you shouldn't redirect to avoid redirect loop
$games = Auth::user()->userGames;
$assigned_games = count($games);
if ($assigned_games == 1) {
return $next($request);
} elseif ($assigned_games > 1 && \Route::currentRouteName() != 'games.board') {
return redirect()->route('games.board');
} elseif ($assigned_games < 1) {
echo "no game bought";
exit;
} else {
return $next($request);
}

Related

how to check count of likes and dislike for post for rating sysytem

i am making an system to check count of likes and dislike if like count is more then dislike count then it gives true
but i am getting an error
// if (Files::withCount('likes') >= Files::withCount('dislike')) {
// return response()->json(['true']);
// }elseif (Files::withCount('dislike') >= Files::withCount('like')) {
// return response()->json(['false']);
// }else{
// return response()->json(['error'=>'somethingwenrwrng']);
// }
// if( DB::table('files')->select('files_id')
// ->join('likes','files_id.files_id','=','files_id') > DB::table('files')->select('id')
// ->join('dislike','files_id.files_id','=','files_id') ){
// return response()->json(['true']);
// }else {
// return response()->json(['error'=>'somethingwenrwrng']);
// }
$file = Files::find($id);
if($file ->likes->count() > $file ->dislike->count() ){
return response()->json(['true']);
}else{
return response()->json(['error'=>'somethingwenrwrng']);
}
i have tried different method to check but getting an error
the withCount() method return a property of the related field_count counting related models
so
$file = Files::find($id)->withCount(['likes','dislikes']);
if($file->likes_count > $file->dislikes_count ){
return response()->json(['true']);
}

checkbox controller in laravel

Hello this is my controller in laravel and I am not getting the problem here. It checks the file if I manually enter a dataset with the value of 1 but if I do it with the system it does not update the value and the box remains to 0 with unchecked box.
What I am trying to do is tick the box if the user tick it and keep the box ticked until he ticks it off plus update the database value too. Thank you in advance
public function tickoffUpload($id, $type, User $user) {
$uploads = $this->upload->get($id);
// if($this->request->isMethod('get')) {
// if($user->can('untick', $uploads)) {
// $uploads = $this->upload = 0;
// } else {
// return $uploads = $this->upload = 1;
// }
// }
if($this->request->isMethod('get')) {
if($type == 0) {
$uploads = $this->upload = 1;
} else if($type == 1){
$uploads = $this->upload = 0;
}
}
if(isset($_POST["uploaded"]) && !empty($_POST["uploaded"])) {
$uploads->uploaded = $this->request->uploaded;
$uploads->save();
}
return redirect('/home');
}
I have sorted this out. I was not getting the requesting value from the form as you can see here $uploads->uploaded = $this->upload = 1; so here is the updated code which I did and worked.
public function tickoffUpload($id, $type, User $user) {
$uploads = $this->upload->get($id); //gets upload id
// requesting uploaded value either 0 or 1
if($this->request->isMethod('get')) {
if($type == 0) {
$uploads->uploaded = $this->request->upload = 1;
} else if($type == 1) {
$uploads->uploaded = $this->request->upload = 0;
}
}
$uploads->save(); // saving the value in database
return redirect('/home');
}
Thank you d3r1ck for the help though ... :)

Why can't if part not executed in laravel 5 controller method?

public function index1()
{
// $g = Request::input('grade');
//$s = Request::input('subject');
$g=$_POST['grade'];
$s=$_POST['subject'];
if(strcmp($g,'Select A Grade')==0 || strcmp($s,'Select A Subject')==0){
if(strcmp($s,'Select A Subject')!=0){
// Session::flash('msg', 'Please select the Subject.');
return redirect()->back()->withInput();
}
else if(strcmp($g,'Select A Grade')!=0){
// Session::flash('msg', 'Please select the Grade.');
return redirect()->back()->withInput();
}
}
else{
$u = DB::table('upldtbls')->where('grade',$g)->where('subject',$s)->get();
return view('2Eng',compact('u'));
}
}
Above is the controller method. Main else part executed correctly. But main if part not executed I guess. In main if condition I want to stay on the same page if drop box values are equal to that. Can any one figure out this mess?
I guess this is what you what to do:
if(strcmp($s,'Select A Subject') !== 0 ){
// Session::flash('msg', 'Please select the Subject.');
return redirect()->back()->withInput();
}
else if(strcmp($g,'Select A Grade') !== 0 ){
// Session::flash('msg', 'Please select the Grade.');
return redirect()->back()->withInput();
}
Ps.
Don't use $_POST, use this:
https://laravel.com/docs/5.2/requests
And for validation its more convenient to use:
https://laravel.com/docs/5.1/validation

Laravel-localization redirect issue

I'm trying to set up a good localization system in a Laravel App. Basically what I am after is this:
If a user is logged in and selects a language, I want to store that value and next time he logs in, the system automatically redirects him with the correct locale in the url.
If a user is not logged in, but have selected a language, I want that stored in the sessions, and use that session to correctly set the locale.
If the user has not selected a language, I want to use http://www.wipmania.com/ to set the locale.
I am able to store the selected values in my Users table and sessions. The problem is how to handle the redirects. I tried to set up a beforeFilter like this:
if(\Auth::check()) {
$user = \User::find(\Auth::user()->id);
if($user->localization != NULL) {
\LaravelLocalization::setLocale($user->localization);
\Redirect::to(\LaravelLocalization::getLocalizedURL($user->localization));
} else if(\Session::get('theSetLocale', 'none') != 'none') {
\LaravelLocalization::setLocale(\Session::get('theSetLocale', 'none'));
\Redirect::to(\LaravelLocalization::getLocalizedURL(\Session::get('theSetLocale', 'none')));
} else {
$ip = \Request::getClientIp();
$thestrings = 'http://api.wipmania.com/' . $ip . '?' . 'www.markmaster.com';
$country = file_get_contents($thestrings);
if ($country == 'NO') {
\LaravelLocalization::setLocale('no');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'no'));
} else if ($country == 'SE') {
\LaravelLocalization::setLocale('se');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'se'));
} else if ($country == 'DK') {
\LaravelLocalization::setLocale('dk');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'dk'));
} else {
\LaravelLocalization::setLocale('en');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'en'));
}
}
}else if(\Session::get('theSetLocale', 'none') != 'none') {
\LaravelLocalization::setLocale(\Session::get('theSetLocale', 'none'));
\Redirect::to(\LaravelLocalization::getLocalizedURL(\Session::get('theSetLocale', 'none')));
} else {
$ip = \Request::getClientIp();
$thestring = 'http://api.wipmania.com/' . $ip . '?' . 'markmaster.com';
$country = file_get_contents($thestring);
if ($country == 'NO') {
\LaravelLocalization::setLocale('no');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'no'));
} else if ($country == 'SE') {
\LaravelLocalization::setLocale('se');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'se'));
} else if ($country == 'DK') {
\LaravelLocalization::setLocale('dk');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'dk'));
} else {
\LaravelLocalization::setLocale('en');
\Redirect::to(\LaravelLocalization::getLocalizedURL( 'en'));
}
}
While this does not work, it might help you guys understand what I am trying to achieve.

Ignoring Codeigniter's _remap for certain URLs?

I use Codeigniter's _remap function to remap all urls to www.website.com. This works for 99% of cases, but I'd like it to ignore certain URLS (specifically /admin,/contact,/submit,/top,/browse). These URLs have content I'd like to display.
How would I achieve this?
public function _remap($urlname)
{
if($urlname == "index") {
// If this is the index, redirect to the most recent startup
redirect("s/".$this->Startup_model->getMostRecent(), 'refresh');
} else {
// If they didn't go to the index, find the startup they were after
$id = $this->Startup_model->matchName($urlname);
// Check to see if the name they entered was a real startup, if not redirect to missing page
if($id == null) {
$data['urlname'] = $urlname;
$this->load->view('header/header');
$this->load->view('content/missing', $data);
$this->load->view('footer/footer');
} else {
// They got the name right! Load the page...
$data['values'] = $this->Startup_model->buildStartup($id);
// If there was no next startup avaiable, pass that info through
$next = $this->Startup_model->findNext($id);
if($next == null) {
$next = '343z61v';
}
// If there was no previous startup avaiable, pass that info through
$previous = $this->Startup_model->findPrevious($id);
if($previous == null) {
$previous = '343z61v';
}
$data['next'] = $next;
$data['previous'] = $previous;
$this->load->view('header/header');
$this->load->view('content/startup', $data);
$this->load->view('footer/footer');
}
}
}
Try this:
function _remap($urlname, $params = array())
{
if(is_callable(array($this, $urlname))){ //If the function exists, is called, if not using the usual
return call_user_func_array(array(&$this, $urlname), $params);
}elseif($urlname == "index") {
// If this is the index, redirect to the most recent startup
redirect("s/".$this->Startup_model->getMostRecent(), 'refresh');
} else {
// If they didn't go to the index, find the startup they were after
$id = $this->Startup_model->matchName($urlname);
// Check to see if the name they entered was a real startup, if not redirect to missing page
if($id == null) {
$data['urlname'] = $urlname;
$this->load->view('header/header');
$this->load->view('content/missing', $data);
$this->load->view('footer/footer');
} else {
// They got the name right! Load the page...
$data['values'] = $this->Startup_model->buildStartup($id);
// If there was no next startup avaiable, pass that info through
$next = $this->Startup_model->findNext($id);
if($next == null) {
$next = '343z61v';
}
// If there was no previous startup avaiable, pass that info through
$previous = $this->Startup_model->findPrevious($id);
if($previous == null) {
$previous = '343z61v';
}
$data['next'] = $next;
$data['previous'] = $previous;
$this->load->view('header/header');
$this->load->view('content/startup', $data);
$this->load->view('footer/footer');
}
}
}
Note:
Function index() should not exist
So you're asking how to selectively not remap certain urls? If so, add this to your if statement before the else:
else if (in_array($urlname, array('admin', 'contact', 'etc')))
{
$this->$urlname;
}

Resources