anyone can plz help,i don't think its a difficult bug,but since i am a newbie so i am having a lot of problems.
databse error
SQLSTATE[HY000]: General error: 1364 Field 'slug' doesn't have a default value (SQL: insert into posts (title, body, updated_at, created_at) values (hey there, my name is anon, 2017-02-08 07:49:34, 2017-02-08 07:49:34))
you need to define the slug for the post
don't know what you mean.coz i have already done all the coding part..will you plz look at my code?
"@extends('main') @section('title','| create new post') @section('content')
create new post
{!! Form::open(array('route' => 'posts.store','data-parsley-validate'=>'')) !!}
{{form::label('title','title:')}}
{{form::text('title',null,array('class'=>'form-control','required' =>'','maxlength'=>'255'))}}
{{Form::label('slug','slug:')}}
{{Form::text('slug',null,array('class'=>'form-control','required'=>'','minlength'=>'5','maxlength'=>'255'))}}
{{form::label('body',"Post Body:")}}
{{form::textarea('body',null,array('class'=>'form-control','required'=>''))}}
{{form::submit('create Post', array('class'=>'btn btn-success btn-lg btn-block','style'=>'margin-top:20px;'))}}
{!! Form::close() !!}
@section('scripts') {!! Html::script('js/parsley.min.js')!!} @endsection" this is my create.blade.php file that i am working on which have title,slug,body and create button.
Make sure that you define the field slug as nullable in the schema if you want it to be empty.
Head over to your posts migration file and do this:
$table->string('slug')->nullable();
If you don't want the slug to be nullable, you will have to set its value before creating the post OR set a default.
To set a default value in the migration file:
$table->string('slug')->default('post-slug');
"public function store(Request $request) { // validation $this->validate($request,array( 'title'=> 'required|max:255', 'slug'=>'required|alpha_dash|min:5|max:255', 'body'=>'required'));
//store in the db
$post= new Post;
$post->title=$request->title;
$post->slug=$request->slug;
$post->body=$request->body;
$post->save();
Session::flash('success', 'The blog post was successfully saved!');
return redirect()->route('posts.show',$post->id);
}"
this is my posts controller.where you can see slug have been validated,defined along with other three parts
@KamalKhan when i was filling my post i did give value to slugs,i am pretty sure that i left none column blank.title,slug and body all were filled.
Check the value of $request->slug using dd($request->slug) and see if it is set.
"<?php
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class AddSlugToUsers extends Migration { /** * Run the migrations. * * @return void */ public function up() { schema::table('posts',function($table){ $table->string('slug')->unique()->after('body');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
schema::table('posts',function($table)
{
$table->dropColumn('slug');
});
}
}" this is my create slug migration file.
The schema looks fine. You should try dd($request->slug); in your controller and see if it has a value.
"QueryException in Connection.php line 647: SQLSTATE[HY000]: General error: 1364 Field 'slug' doesn't have a default value (SQL: insert into posts (title, body, updated_at, created_at)"
this is error after i add this
"$table->string('slug')->default('post-slug');"
Did you re run the migration?
php artisan migrate:refresh
NOTE: This will flush your database so make sure you make a backup of your database.
Have you mentioned 'slug' field into 'Post' model's $fillable array ?
public $fillable = [
'title'
'slug'
'body'
];
Got my point ?
"post-up" this is what comes after and this is excatly what i entered in the slug area.so yes,it does have a value "dd($request->slug);"
Can you please show me code for 'post.php' model file ?
Yeah, I know but still we can check model file because there is no other error.. what says @KamalKhan ?
@KamalKhan khan yes,i actuallly wanted to add slug column to posts table,which went wrong so i did this(php artisan refresh),but there were few errors which making them again(max length 767 bytes) so,i simply deleted all the tables and "php artisan make"(changed max length to 191 manually in mysql in few columns like email and slug) to recreate all the tables.
@anmol Why don't you use a model event trait to automatically set the slug? A good use case for a slug is to generate it automatically based on another field, rather than asking the user to set it.
Create app/Sluggable.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
trait Sluggable
{
public static function bootSluggable()
{
static::registerModelEvent('creating', function(Model $model) {
$slug = $_slug = str_slug($model->title, '-');
for ($i = 1; ! is_null(static::whereSlug($slug)->first()); $i++) {
$slug = $_slug . "-{$i}";
}
$model->slug = $slug;
}, 0);
}
}
and add the trait to your post model class:
namespace App;
class Post
{
use Sluggable;
...
}
"<?php
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->timestamps(); }); }
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
} " my model file for creating posts in the database
thank you so much guys,you really saved me. i think that was the only thing required. "$table->string('slug')->default('post-slug');" thank you so much.
Rather than removing the slug or making it nullable.
just add
$post->slug = str_slug($request->title); // slugs are normally created from titles which replaces all special characters to - and convert it to lower case
@anmol Your error simply means that the data your trying to insert to your database is null for the slug, since strict is set to true in config/database.php, MySQL cant allow you to insert a null value into the database. My assumption is either your picking a null value from the slug part of your form or your handling the data from your form in a wrong way. Please post the code of your controller for further assistance. I have had this same problem a couple of times before and trust me, the error is either from your form or controller but not your migrations.
@brainlabs2010 You might want to be careful with that because a slug should be unique (at least thats the intended use case). If two titles match, the slug will be the same and cause an sql error.
But if a UNIQUE slug isn't required, then your solution is totally fine.
thank you so much guys,i have solved this problem already. i am working on authentication now. @KamalKhan bro,i just wanted a help if you can. i did php artisan make:auth and it created two files with name login.blade.php and register.blade.php just wanted to change the navbar and and write something else in place of laravel. in the code of login.blade.php, "@extends('layouts.app')"since this file extend layout .app file so i went there and edited the file which now looks like this "
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'laravel') }}</title>
<!-- Styles -->
<link href="/css/app.css" rel="stylesheet">
<!-- Scripts -->
<script>
window.laravel = {!! json_encode([
'csrfToken' => csrf_token(),
]) !!};
</script>
<!-- Collapsed Hamburger -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Branding Image -->
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'laravel') }}
</a>
</div>
<div class="collapse navbar-collapse" id="app-navbar-collapse">
<!-- Left Side Of Navbar -->
<ul class="nav navbar-nav">
</ul>
<!-- Right Side Of Navbar -->
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
@if (Auth::guest())
<li><a href="{{ route('login') }}">Login</a></li>
<li><a href="{{ route('register') }}">Register</a></li>
@else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
@endif
</ul>
</div>
</div>
</nav>
@yield('content')
</div>
<!-- Scripts -->
<script src="/js/app.js"></script>
"
i tried changing the title and 'laravel'in the navbar to my website name but it is not working.i know its just stupid front end thing but its just not working.i don't get it.
even after i edited the code,the source code is also not changing,even after i changed laravel to my website name it still shows laravel.
I don't know, make sure you haven't cached the views and also try removing everything from the layouts/app.blade.php file to see if you are really loading that view. Something will show up.
Note that {{ config('app.name', 'laravel') }} will load from the config/app.php name key even if you change laravel to something else.
problem solved,it was actually about app.name.. great brother thank you so much.
@anmol that is because you are trying to insert a null value in slug column in your table
Please or to participate in this conversation.