Friday, August 26, 2016

How to use job(queue) with redis server in laravel 5.2

How to use job(queue) with redis server in laravel 5.2:

For using jobs in Laravel 5.2 with redis server, we have to do following steps:

1. First to install redis server on your machine for this, run this command on your terminal

sudo apt install redis-server

It will install your redis server. For start redis server run this command

redis-server

2. Now install the dependency in your project. For this write this in your composer.json

predis/predis ~1.0

and update composer via

composer update

3.  Now do some settings

(a) In .env file change QUEUE_DRIVER=sync to QUEUE_DRIVER=redis
(b) In config/queue.php file change 'default' => env('QUEUE_DRIVER', 'redis')
(c) In config/session.php file change  'driver' => env('SESSION_DRIVER', 'redis')

4. Now run this command on terminal for create a new queue

php artisan make:job InsertFeedData

5. Now go to this file job/InsertFeedData.php and write your code in handle function which you want to run as a job.


    class InsertFeedData extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;
    protected $video;
    protected $type;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user, $video, $type)
    {
        $this->user = $user;
        $this->video = $video;
        $this->type = $type;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $followers = Follower::where('user_id', $this->user)->pluck('follower_id');
        Feed::create(['video_id' => $this->video, 'follower_id' => $this->user, 'type' => $this->type]);
        foreach ($followers as $follower) {
            $feed = Feed::create(['video_id' => $this->video, 'follower_id' => $follower, 'type' => $this->type]);
            print_r($feed);
        }
    }
}



6. Now you can use this class in your controller via

$this->dispatch(new InsertFeedData(Utilities::getUserId(), $video->id, 'share'));

7. You can check it via running this command on terminal

php artisan queue:listen

Thanks

Friday, August 19, 2016

Search a full text using scout in Laravel 5.3

Search a full text using scout in Laravel 5.3:

In Laravel 5.3 we can search a full text using scout.

For this we have to import it in that model.

php artisan scout:import App\\Post
After that we can use it in model like this.

Post::search('Alice')->get();
Also we can use it in pagination.

Post::search('Alice')->paginate()
Also, we can use in simple where clauses.

Post::search(‘Alice’)—>where('acount_id', '>', 1)->paginate()
Thanks







Saturday, August 13, 2016

Laravel collection function for easy fetching data

Laravel collection function for easy fetching data:

For fetching data in easy way :

Let us assume I'm fetching an object form a table Likes and want to know, it is liked by which users.

For this :

$v->likes->keyBy('liked_by')->has($userId);

It will be very easy way for getting value from this collection object.

Thanks

Saturday, August 6, 2016

Install mysql without getting password prompt

Install mysql without getting password prompt :

For installing mysql without password prompt, we will run following command:

sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password root" 

sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password root

sudo apt-get  install mysql-server

After running these command, it will not ask for password. It will be set "root" password

Thanks