V
V
vladimirr892018-03-04 10:30:36
Laravel
vladimirr89, 2018-03-04 10:30:36

How to put slug in laravel 5.5 without using third party solutions?

Tell me how in this controller to make sure that the slug is automatically added to the database, and not an error pops up:

SQLSTATE[HY000]: General error: 1364 Field 'slug' doesn't have a default value (SQL: insert into `posts` (`title`, `date`, `description`, `content`, `user_id`, `updated_at`, `created_at`) values (Статья 4444, 2018-03-04, <p>Супер статья которая не опубликуется</p>, <p>Полюбому не опубликуется</p>, 10, 2018-03-03 21:36:45, 2018-03-03 21:36:45))

I want to note that I found a bunch of different information, but nowhere is there any specifics. Just stupidly Roughly speaking, everything comes down to this line or to even older incomprehensible options. But where to insert it so that it works in my particular case? No matter how I try what combinations and tricks to do, nothing comes of it. It is necessary that the title is converted to a slug and the article id is added to it. Basically like this:
$title = str_slug('Laravel 5 Framework', '-');
if (empty(Post::find(DB::table('posts')->max('id'))))
        $last_id = 0;
    else
        $last_id = Post::find(DB::table('posts')->max('id'))->id;
        $last_id++;
        'slug' => str_slug($title['title']) . "-" . $last_id,

Here is the article controller code:
<?php
 
namespace App\Http\Controllers\Admin;
 
use App\Post;
use App\Chainword;
use App\Category;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
 
class PostsController extends Controller
{
    public function index()
    {
        $posts = Post::all();
        return view('admin.posts.index', ['posts'=>$posts]);
    }
 
    public function create()
    {
        $categories = Category::pluck('title', 'id')->all();
        $chainwords = Chainword::pluck('title', 'id')->all();
 
        return view('admin.posts.create', compact(
            'categories',
            'chainwords'
        ));
 
    }
 
    public function store(Request $request)
    {
      $this->validate($request, [
            'title' =>'required',
            'content' => 'required',
            'date' => 'required',
            'image' => 'nullable|image'
        ]);

        $post = Post::add($request->all());
        $post->uploadImage($request->file('image'));
        $post->setCategory($request->get('category_id'));
        $post->setChainwords($request->get('chainwords'));
        $post->toggleStatus($request->get('status'));
        $post->toggleFeatured($request->get('is_featured'));
 
        return redirect()->route('posts.index');
    }
 
    public function edit($id)
    {
        $post = Post::find($id);
        $categories = Category::pluck('title', 'id')->all();
        $chainwords = Chainword::pluck('title', 'id')->all();
        $selectedChainwords = $post->chainwords->pluck('id')->all();
 
        return view('admin.posts.edit', compact(
            'categories',
            'chainwords',
            'post',
            'selectedChainwords'
        ));
    }
 
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' =>'required',
            'content' => 'required',
            'date' => 'required',
            'image' => 'nullable|image'
        ]);
 
        $post = Post::find($id);
        $post->edit($request->all());
        $post->uploadImage($request->file('image'));
        $post->setCategory($request->get('category_id'));
        $post->setChainwords($request->get('chainwords'));
        $post->toggleStatus($request->get('status'));
        $post->toggleFeatured($request->get('is_featured'));
 
        return redirect()->route('posts.index');
    }
 
    public function destroy($id)
    {
        Post::find($id)->remove();
        return redirect()->route('posts.index');
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tolmachev, 2018-03-04
@dark_tke

Look, you don’t create a slug anywhere in your code, if you don’t want to change especially the source code that is already written, use events, https://laravel.com/docs/5.6/events? put the handler at the time of creation, and you get what you need. In this case, the slug will be added or updated with any change through the ORM
. The second option is to call the creation of the slug in the process of creating and editing, for example
$post->slug = str_slug('Laravel 5 Framework', '-');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question