Giter Site home page Giter Site logo

est-eloquent-sluggable-demo's Introduction

For Chinese

Description

cviebrock/eloquent-sluggable Easy creation of slugs for your Eloquent models in Laravel.

This project is a very simple demo to show you how to use Laravel eloquent-sluggable quickly.

This project was created by The EST Group and PHPHub.

The Demo

Screenshots

Run the demo

You can refer to this documentation to know how to run this demo.

Usage Scenarios

If you have an article that title called "My Dinner With Andr Fran OIS E & C", then you'll get the url like this:

http://example.com/post/My+Dinner+With+Andr%C3%A9+%26+Fran%C3%A7ois

When you use cviebrock/eloquent-sluggable, the converted URL as follows:

http://example.com/post/my-dinner-with-andre-francois

As you can see, eloquent-sluggable would make the URL readable and SEO friendly.

The Tutorial

Table of contents

  1. Installation
  2. Updating your Eloquent Models
  3. Basic Usage

1. Installation

1). To get started with eloquent-sluggable, add to your composer.json file as a dependency:

composer require cviebrock/eloquent-sluggable

2). Integration in Laravel

Add the service provider to providers:

'providers' => [
    // ...
    'Cviebrock\EloquentSluggable\SluggableServiceProvider',
],

Finally, from the command line again, run php artisan vendor:publish to publish the default configuration file.

2. Updating your Eloquent Models

use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;

class Post extends Model implements SluggableInterface
{
	use SluggableTrait;

	protected $sluggable = [
		'build_from' => 'title',
		'save_to'    => 'slug',
	];

}

Of course, your database will need a column in which to store the slug. You can do this manually, or use the built-in artisan command to create a migration for you. For example:

php artisan sluggable:table posts

After generating the migration, you will need to rebuild composer's auto-loader and run the migration:

composer dump-autoload
php artisan migrate

3. Basic Usage

$post = new Post([
	'title' => 'My Awesome Blog Post',
]);

$post->save();

And so is retrieving the slug:

echo $post->slug;

That's it! 🍻 🍻 🍻

You can refer to the documentation to learn more about Laravel Eloquent Sluggable.


欢迎关注 LaravelTips, 这是一个专注于为 Laravel 开发者服务, 致力于帮助开发者更好的掌握 Laravel 框架, 提升开发效率的微信公众号.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.