Friday, February 19, 2016

Laravel 5.2 : Many To Many Polymorphic Relations

Hi, Today we will discuss about many to many polymorphic relations in laravel 5.2.

Table Structure

In addition to traditional polymorphic relations, we may also define "many-to-many" polymorphic relations. For example, a blog Post and Video model could share a polymorphic relation to a Tag model. Using a many-to-many polymorphic relation allows us to have a single list of unique tags that are shared across blog posts and videos. First, let's examine the table structure:

posts
    id - integer
    name - string

videos
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer (post_id or video_id)
    taggable_type - string (type should be App\Post and App\Video)

Model Structure

Next, we're ready to define the relationships on the model. The Post and Video models will both have a tags method that calls the morphToMany method on the base Eloquent class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Get all of the tags for the post.
     */
    public function tags()
    {
        return $this->morphToMany('App\Tag', 'taggable');
    }
}

class Video extends Model
{
    /**
     * Get all of the tags for the post.
     */
    public function tags()
    {
        return $this->morphToMany('App\Tag', 'taggable');
    }
}

Defining The Inverse Of The Relationship

Next, on the Tag model, we should define a method for each of its related models. So, for this example, we will define a posts method and a videos method:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    /**
     * Get all of the posts that are assigned this tag.
     */
    public function posts()
    {
        return $this->morphedByMany('App\Post', 'taggable');
    }

    /**
     * Get all of the videos that are assigned this tag.
     */
    public function videos()
    {
        return $this->morphedByMany('App\Video', 'taggable');
    }
}

Retrieving The Relationship

Once our database table and models are defined, we may access the relationships via our models. For example, to access all of the tags for a post, we can simply use the tags dynamic property:

Open the terminal and run the below command

php artisan tinker

then $post = App\Post::find(1)->tags;

$video = App\Video::find(1)->tags;

We may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphedByMany. In our case, that is the posts or videos methods on the Tag model. So, we will access those methods as dynamic properties:

$tag = App\Tag::find(1)->video;


Thanks.

Friday, February 12, 2016

Laravel : How to make migration for change data type

Hi,

Today, we will learn how to make migration for change the data type of a column.

I needed to change the type of a column from VARCHAR to TEXT.

First on your terminal write the following comman to make migration file:

php artisan make:migration change_data_type_of_column_in_tablename

This will create a new file named date_change_data_type_of_column_in_tablename.php in database/migrations folder.

Open this file in your editor and write the following script as given.

public function up()
{
    DB::statement('ALTER TABLE tablename MODIFY COLUMN columnname TEXT');
}

public function down()
{
    DB::statement('ALTER TABLE tablename MODIFY COLUMN columnname VARCHAR(255)');
}

Then on your terminal run the command : php artisan migrate

This will change the given column data type.

Thanks

Friday, February 5, 2016

How to implement Breadcrumbs in Laravel 5.1

 Hi, today we will discuss about how to implement Breadcrumbs in Laravel 5.1.


1. Install Laravel Breadcrumbs


Note:-Laravel 5.0 or above is required – use the 2.x version for Laravel 4.


Install with Composer

Run this at the command line:

$ composer require davejamesmiller/laravel-breadcrumbs

This will both update composer.json and install the package into the vendor/ directory.

Add to config/app.php

Add the service provider to providers:

'providers' => [
    // ...
    DaveJamesMiller\Breadcrumbs\ServiceProvider::class,
],

And add the facade to aliases:

'aliases' => [
    // ...
    'Breadcrumbs' => DaveJamesMiller\Breadcrumbs\Facade::class,
],

2. Define your breadcrumbs


Create a file called app/Http/breadcrumbs.php that looks like this:

<?php

// Home
Breadcrumbs::register('home', function($breadcrumbs)
{
    $breadcrumbs->push('Home', route('home'));
});

// Home > About
Breadcrumbs::register('about', function($breadcrumbs)
{
    $breadcrumbs->parent('home');
    $breadcrumbs->push('About', route('about'));
});

// Home > Blog
Breadcrumbs::register('blog', function($breadcrumbs)
{
    $breadcrumbs->parent('home');
    $breadcrumbs->push('Blog', route('blog'));
});

// Home > Blog > [Category]
Breadcrumbs::register('category', function($breadcrumbs, $category)
{
    $breadcrumbs->parent('blog');
    $breadcrumbs->push($category->title, route('category', $category->id));
});

// Home > Blog > [Category] > [Page]
Breadcrumbs::register('page', function($breadcrumbs, $page)
{
    $breadcrumbs->parent('category', $page->category);
    $breadcrumbs->push($page->title, route('page', $page->id));
});

3. Choose a template


By default a Bootstrap-compatible ordered list will be rendered, so if you’re using Bootstrap 3 you can skip this step.

First initialise the config file by running this command:

$ php artisan vendor:publish

Then open config/breadcrumbs.php and edit this line:

'view' => 'breadcrumbs::bootstrap3',

The possible values are:

    Bootstrap 3: breadcrumbs::bootstrap3
    Bootstrap 2: breadcrumbs::bootstrap2
    The path to a custom view: e.g. _partials/breadcrumbs

4. Output the breadcrumbs


Finally, call Breadcrumbs::render() in the view template for each page, passing it the name of the breadcrumb to use and any additional parameters – for example:

{!! Breadcrumbs::render('home') !!}

{!! Breadcrumbs::render('category', $category) !!}