samedi 9 septembre 2017

Session values gets destroyed when it is redirected after registration in laravel 5.1

I want to show errors and session flash message after redirection,I have dumped the session values after redirection.It shows only token value.All other session values are lost.My routes are grouped and its middleware is Auth.



via Chebli Mohamed

vendredi 8 septembre 2017

Laravel controller session store/retrieve troubles

I'm having troubles storing/retrieving data from session. My session variable from_date is not changing. At beginning, I'm checking if session has no value.

    if (!($request->session()->has('from_date')))
    {
        $from= date('Y-m-d', mktime(0, 0, 0, date("m") - 1, "01", date("Y")));
        session(['from_date' => $from]);            
    }
    else
    {
       $from=$request->session()->get('from_date');              
    }



via Chebli Mohamed

jeudi 7 septembre 2017

How to fetch values from URL in laravel 5.1

I have well researched and found that this question is unique. I am newbie to Laravel, I am sending link in email for forget password while in link there are 3 parameters email,id and time all encoded. Now When user click on that link further operations will begin. Now I am stuck at how to get all values from URL.

Here is my Link Code:

$url = $baseurl . "/changepwd/" . $id_enc . "/" . $email_enc . "/" . $time;

Here is my route:

Route::get('/changepwd/{$id}/{$email}/{$time}', 'UserController@change_password_web');

When user clicks on link that function:

public function change_password_web($id ,$email, $time)
    {
        echo $request->route('id');
    }

My Url says:

http://localhost/laravelproject/changepwd/MQ==/amF5bWluemFwQGdtYWlsLmNvbQ==/1504772185

When I try this error says

Sorry, the page you are looking for could not be found. NotFoundHttpException

Can anyone help me to resolve it?



via Chebli Mohamed

mercredi 6 septembre 2017

How to send Plain text body in email in laravel 5.1?

My email is sending properly with subject name, now I dont know how to send email body with simple text. The thing just now I am doing is sending blank email with just a subject name on it. Can anyone help me?

$data = array('title' => 'Forget Password - App', 'content' => 'This is the content of mail body');
           Mail::send(['text' => 'view'],$data, function ($message) {
                $message->from('fromemail@gmail.com', 'Social Team');
                $message->to('randomemail@gmail.com');
                $message->subject('App - Forget Password');
            });



via Chebli Mohamed

REST API post field validation Laravel 5.1

I need to check if I have all posted variables are required or else throw error. Till Now I am doing like this

Routes.php

Route::post('/api/ws_fetchuser', 'UserController@fetch_user_details');

UserController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
  public function fetch_user_details(Request $request)
    { 
        if(!empty($request) && $request->id!='')
        {
            print_r($request->id);    
        }
        else
        {
            return response(array(
            'error' => false,
            'message' =>'please enter all form fields',
            ),200);        
        }

    }
}

I am checking like this $request->id!='', is there any validation rules or methods which I can use to check id is required field.

I have added this validation in my controller as well but what if id is not present how can I show the error?

Updated Validation Code:

public function fetch_user_details(Request $request)
    { 
        $this->validate($request, [
        'id' => 'required'
        ]);

        print_r($request->id);

    }



via Chebli Mohamed

samedi 2 septembre 2017

Class 'Illuminate\Foundation\Auth\User' not found JWT Auth Laravel

I have written code for registration and login using JWT authentication. In this code registration function works fine but login function doesn't works. Login function prompts an error as Class 'Illuminate\Foundation\Auth\User' not found

My user model is

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    protected $table = 'users';
    public $timestamps = false;
    protected $primaryKey = 'user_name';
    protected $fillable = ['user_name','password'];
}

My UserController is

class UsersController extends Controller
{

    public function login()
    {
        $credentials = request()->only('user_name','password');
        try{
            $token = JWTAuth::attempt($credentials);
            if($token){
                return response()->json(['error'=>'invalid_credentials'],401);
            }
        }
        catch(JWTException $e){
            return response()->json(['error'=>'something went wrong'],500);
        }
        return response()->json(['token'=>$token],200);
    }

    public function register()
    {
        $user_name = request()->user_name;
        $password = request()->password;
        $user = User::create([
            'user_name'=>$user_name,
            'password'=>bcrypt($password)
        ]);

        $token = JWTAuth::fromUser($user);

        return response()->json(['token'=>$token],200);
    }
}

The login function shows the error as

Class 'Illuminate\Foundation\Auth\User' not found



via Chebli Mohamed

vendredi 1 septembre 2017

Laravel scheduled task without overlapping, run on demand

I have a scheduled task with Laravel defined as below to run every 10 minutes. I also need the same job to be run on-demand without it overlapping if it is already running or preventing the scheduled job starting to run if the on-demand job is running.

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
  $schedule->call(function () {
    $job = new \App\Jobs\ImportJob();
    $job->handle();
  })->name('Import')->everyTenMinutes()->withoutOverlapping();
}

Is there a nice, simple way of achieving this with the schedular API or should the Job take care of its own mutex flag?



via Chebli Mohamed