I wanted to create a post with categery_id as a dropdown in post/create form, but I'm getting a SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category_id' cannot be null (SQL: insert into posts
(title
, body
, category_id
, author_id
, updated_at
, created_at
)...
I need a little help in finding what is causing this issue and here's what I have so far...
create.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<h1>Create Post</h1>
<div class="row post_row">
{!! Form::open(['action' => 'PostsController@store', 'method' => 'POST', 'class' => 'form']) !!}
<div class="col-md-8">
<div class'form-group'>
</div>
</div>
<div class="col-md-4">
<div class'form-group'>
<select class='form-control' title="category_id">
@foreach ($categories as $category => $value)
<option value=""></option>
@endforeach
</select>
</div>
</div>
</div>
<div class="row post_row">
<div class="col-md-8">
<div class'form-group'>
</div>
</div>
</div>
<div class'form-group' style="padding-top: 20px">
{!! Form::close() !!}
</div>
</div>
@endsection
PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Category;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
protected $limit = 3;
public function create()
{
$posts = Post::all();
$categories = Category::all();
return view('posts.create')->with(['posts' => $posts, 'categories' => $categories]);
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|max:255',
'body' => 'required'
]);
//create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->category_id = $request->input('category_id');
$post->author_id = auth()->user()->id;
$post->save();
return redirect('/posts')->with('success', 'Your post created created successfully');
}
public function update(Request $request, $id)
{
$updated = Category::findorFail($id);
$categories = $request->all();
$category_id = $request->get('category_id');
$updated->fill($categories)->save();
return redirect('/dashboard')->with('success', 'Your post created updated successfully');
}
Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;
class Post extends Model
{
//Table Name
protected $table = 'posts';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
protected $fillable = [
'title',
'excerpt',
'body',
'categery_id',
'image',
];
protected $dates = ['published_at'];
public function author()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function getImageUrlAttribute($value)
{
$imageUrl = "";
if( ! is_null($this->image))
{
$imagePath = public_path() . "/img/" . $this->image;
if(file_exists($imagePath)) $imageUrl = asset("img/" . $this->image);
}
return $imageUrl;
}
public function getDateAttribute()
{
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}
public function getExcerptHtmlAttribute()
{
return $this->excerpt ? Markdown::convertToHtml(e($this->excerpt)) : NULL;
}
public function getBodyHtmlAttribute()
{
return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL;
}
public function scopeLatestFirst($query)
{
return $query->orderBy('published_at', 'desc');
}
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
}
Category.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
protected $fillable = [
'title',
'categery_id'
];
public function posts()
{
return $this->hasMany(Post::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
CreatePostsTable.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->bigIncrements('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->mediumText('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
So here's what I have so far, does anyone see why I'm having this issue?
from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2LedSaG
via IFTTT
Aucun commentaire:
Enregistrer un commentaire