Ich habe drei Modelle, die ich Post und Kommentar und Benutzer erstellt habe. Ich versuche, eine Beziehung dazwischen zu schaffen. Comment und Post Beide haben ein Feld "user_id", das mit der ID der User-Tabelle übereinstimmt. Im Folgenden sind die Beziehungen, die ich gesetzt bin versucht:Laravel gehört zu Beziehung funktioniert nicht
comment.php:
<?php
namespace App;
class Comment extends Model
{
//
public function post(){
return $this->belongsTo(Post::class);
}
public function user(){
return $this->belongsTo(User::class);
}
}
post.php:
<?php
namespace App;
class Post extends Model
{
public function comments(){
return $this->hasMany(Comment::class);
}
public function addComment($body, $name, $email){
$this->comments()->create(compact('body','name','email'));
}
public function user(){ // $comment->user->name
return $this->belongsTo(User::class);
}
}
User.php
<?php
namespace App;
use App\Post;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
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',
];
public function post(){
return $this->hasMany(Post::class);
}
}
wie Sie jetzt kann einen Kommentar anzeigen, der zu einem Benutzer gehört, und einen Beitrag zu einem Benutzer.
Wenn ich jedoch versuche, einen neuen Beitrag über Tinker zu erstellen (ohne Benutzer in Datenbank oder eingeloggten Benutzer erstellt), kann ich einen Post ohne Problem erstellen;
Das sagt mir, dass die Beziehung, dass ein Beitrag einen Benutzer und einen Kommentar haben muss, einen Benutzer haben muss und auch nicht funktioniert! Irgendeine Idee, wie man das repariert?
Migrationen:
create_posts_migration:
<?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->integer('user_id');
$table->string('title');
$table->text('body');
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
create_comment_migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->integer('user_id');
$table->string('email');
$table->string('name');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Bitte zeigen Sie Ihre User.php Datei an. –
Ich habe den Beitrag bearbeitet, so dass es jetzt dort ist –
Überprüfen Sie, ob Sie Spalte 'user_id' in Ihrer 'posts' Tabelle hinzugefügt haben. –