Answer the question
In order to leave comments, you need to log in
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))
$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,
<?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
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 questionAsk a Question
731 491 924 answers to any question