سلام
به مشکل ارور خوردم
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: notes.card_id (SQL: insert into "notes" ("body", "user_id", "card_id", "updated_at", "created_at") values (asdasd, 1, ?, 2020-03-03 14:37:40, 2020-03-03 14:37:40))
به مدت یک هفته درگیری ام.
کسی هست کمک کنه؟
Schema::create('notes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer("card_id")->unsigned()->index();
$table->integer("user_id")->unsigned()->index();
$table->text("body");
$table->timestamps();
$table->foreign("user_id")->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign("card_id")->references('id')->on('cards')->onDelete('cascade')->onUpdate('cascade');
});
Schema::create('cards', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("title");
$table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name")->nullable();
$table->string("username")->unique();
$table->string("email")->unique();
$table->string("password");
$table->timestamps();
});
namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
protected $fillable = ['body'];
public function card()
{
return $this->belongsTo(Card::class);
}
public function user()
{
return $this->beLongsTo(User::class);
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
public function notes()
{
return $this->hasMany(Note::class);
}
}
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function notes()
{
return $this->hasMany(Note::class);
}
}
namespace App\Http\Controllers;
use Illuminate\Support\facades\DB;
use Illuminate\Http\Request;
use App\Card;
class CardsController extends Controller
{
public function index()
{
$cards = DB::table('cards')->get();
return view("card.index",compact('cards'));
}
public function show(Card $cards)
{
$cards->load("notes.user");
return view("card.show",compact("cards"));
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Note;
use App\Card;
class NotesController extends Controller
{
public function store(Request $request,Card $card)
{
$this->validate($request,[
'body'=>'required|min:3'
]);
$note = new Note($request->all());
$note->user_id = 1;
$card->notes()->save($note);
return back();
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>{{ $cards->title }}</h1>
<ul>
@foreach ($cards->notes as $note)
<li style="width: 100%;">
<a style="float:left;width:50%;" href="/notes/{{$note->id}}/edit">{{ $note->body }}</a>
<a href="#" class="float:left;width: 50%;">{{ $note->user->username }}</a>
</li>
@endforeach
</ul>
<h3>Add new note</h3>
<hr>
{!! Form::open(['url'=>"/cards/$cards->id/notes",'method'=>'post']) !!}
{!! Form::textarea('body', null) !!}<br>
{!! Form::submit('Add new note')!!}
{!! Form::close() !!}
@if(count($errors))
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
</body>
</html>
شما نباید فیلد card_id در جدول notes رو نال بزارید... با این کدی که در کنترلر میبینم این فیلد نال باقی مونده و به همین دلیل دیتابیس ارور میده...
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟