Saturday, July 30, 2016

Laravel : How to get user's tweets in json

Hi

Today we will learn how to get a user's tweets in json format in laravel 5.

Add the below line of code in your composer.json file.

{
    "require": {
        "j7mbo/twitter-api-php": "dev-master"
    }
}

And then update your composer with composer update command in your terminal.

Then in any controller where you want to get tweets, use below function.

public function twitter(){
        
        $settings = array(
            'oauth_access_token' => 'Your oauth access token',
            'oauth_access_token_secret' => 'Your oauth access token secret',
            'consumer_key' => 'Your consumer key',
            'consumer_secret' => 'Your consumer secret'
        );
        $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
        $getfield = '';
        $requestMethod = 'GET';


                $twitter = new \TwitterAPIExchange($settings);
                $feeds = $twitter->setGetfield($getfield)
                    ->buildOauth($url, $requestMethod)
                    ->performRequest();

        return json_encode($feeds);
        

    }


Please click or goto url https://themepacific.com/how-to-generate-api-key-consumer-token-access-key-for-twitter-oauth/994/, to generate your settings.

In above script if we use $getfield = '?user_id={user_id}', it will give tweets of that particular user. If it is left blank then it will give tweets of that user whom oauth access token belongs to. If you want to explore more then please goto url https://dev.twitter.com/rest/reference/get/statuses/user_timeline.

Hope it will help someone.

Thanks

Friday, July 22, 2016

Service Container in laravel 5

Hi

Today, we learn service container in larave 5.2

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.


<?php

namespace App\Jobs;

use App\User;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Bus\SelfHandling;

class PurchasePodcast implements SelfHandling
{
    /**
     * The mailer implementation.
     */
    protected $mailer;

    /**
     * Create a new instance.
     *
     * @param  Mailer  $mailer
     * @return void
     */
    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    /**
     * Purchase a podcast.
     *
     * @return void
     */
    public function handle()
    {
        //
    }
}


In this example, the PurchasePodcast job needs to send e-mails when a podcast is purchased. So, we will inject a service that is able to send e-mails. Since the service is injected, we are able to easily swap it out with another implementation. We are also able to easily "mock", or create a dummy implementation of the mailer when testing our application.

Thanks.

Saturday, July 16, 2016

PHP Dependency Injection with Laravel 5

Hi
Today, we will learn dependency injection in laravel.
In laravel 5, We can inject services or classes. These dependency injection techniques are very useful when we generating a large scale projects. Class dependencies are injected into the class through the constructor or setter methods.
Dependency solutions through service container simplifies the dependency injection logic for our controller by configuring the container. This really becomes crystallise if one class depends on a service, that service depends on other services and those services could also depend on more services, the container will manage that dependency results.
Example of container App/Repositories/BookDbRepository
<?php namespace App\Repositories;
use App\Book;
class BookDbRepository {
    public function all(){
       return Book::all();
    }
    public function insertData($requestData){
        $book = new Book;
        $book->title= $requestData['title'];
        $book->description= $requestData['description'];
        $book->author= $requestData['author'];
        $book->save();
    }
    public function updateData($id, $requestData){
        $book = Book::find($id);
        $book->title = $requestData['title'];
        $book->description = $requestData['description'];
        $book->author = $requestData['author'];
        $book->save();
    }
    public function deleteData($id){
        Book::find($id)->delete();
    }
}
Into BookDbRepository we can see 4 methods are specifies show, insert, update and delete. Where container has all power to change the database. Unfortunately, controller will talk to BookDbRepository for every new query requests of application.
After early changes, Lets see what effects has been done in our controller BookController class.
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\BookDbRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class BookController extends Controller {
    protected $repo;
    public function __construct(BookDbRepository $repo)
    {
        $this->repo = $repo;
    }
public function index()
{
$allBooks = $this->repo->all();
        return View('books.bookList', compact('allBooks'));
}
public function store(PublishBookRequest $requestData)
{
        $this->repo->insertData($requestData);
        return redirect()->route('book.index')->with('message', 'New record has been added!');
}
public function update($id, PublishBookRequest $requestData)
{
        $this->repo->updateData($id, $requestData);
        return redirect()->route('book.index')->with('message', 'Record has been updated!');
}

public function destroy($id)
    {
        $this->repo->deleteData($id);
        return redirect()->route('book.index')->with('message', 'Record deleted successfully!');
    }
}
Clearly we can see that in constructor of class BookController , we have injected the instance of BookDbRepository class.
Thanks

Friday, July 8, 2016

Laravel 5.2 and MySQL dates

Hi

Today we will learn, how to use MySQL dates with laravel 5.2.

Starting with MySQL 5.7, 0000-00-00 00:00:00 is no longer considered a valid date, since strict mode is enabled by default. All timestamp columns should receive a valid default value when we insert records into our database. We may use the useCurrent method in our migrations to default the timestamp columns to the current timestamps, or we may make the timestamps nullable to allow null values:

$table->timestamp('foo')->nullable();

$table->timestamp('foo')->useCurrent();

$table->nullableTimestamps();

Thanks