Laravel用一个表单向多个关系表插入数据

Laravel insert data to multiple relational tables with a single form

我正在从事 Laravel 项目,我想知道: 如何向我的多个相关 table 插入数据? 我们如何在作者 table 的 author_type_id 字段中插入作者 ID? 如何在post中存储author_id? 所以我不知道如何使用表单插入相关模型。感谢您的帮助:)

我的模特

//Post型号

class Post extends Model
    {
        //
            
        protected $fillable = [
            'post_type_id','author_id','author_type_id','article'
        ];
    
    
        public function posttype()
        {
            return $this->belongsTo(Posttype::class);
        }
    
    
        public function author()
        {
           return $this->belongsTo(Author::class);
        }
    
        public function authortype()
        {
           return $this->belongsTo(Authortype::class);
        }
    }

//Post类型模型

class Posttype extends Model
{
    //
    protected $fillable = [
        'post_type'
    ];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

//作者模型

class Author extends Model
{
    //
    protected $fillable = [
        'author_name','author_first_name','author_type_id'
    ];
        
    public function posts()
    {
        return $this->belongsToMany(Post::class);
    }

    public function authortype()
    {
       return $this->belongsTo(Authortype::class);
    }
}

//作者类型模型

class Authortype extends Model
{
    //
    protected $fillable = [
        'author_type '
    ];
      
    public function author()
    {
        return $this->hasMany(Author::class);
    }
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

// PostsController 控制器

class PostsController extends Controller
{
    public function index()
    {
        return view('index')->with('posts',Post::all());
    }

    public function create()
    {
       return view('create')->with('posttypes',$posttypes)
        ->with('authors',$authors)
        ->with('authortypes',$authortypes);
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            "post_type_id"    => "required",
            "author_id"    => "required",
            "author_type_id"  => "required",
            "article"  => "required"
            ]);
//How can we insert author id in the author_type_id field of the Author table?

            $post = Post::create([
            "post_type_id"    => $request->post_type_id,
            "author_id"    => $request->author_id,
            "author_type_id"  => $request->author_type_id,
            "article"  => $request->article,
            ]);

            return redirect()->back();
    }
}

//创建postblade

    @section('content')
    <div class="container">
        <form action="{{route('store')}}" method="POST" enctype="multipart/form-data">
                        {{ csrf_field()}}
                            
                            <div class="form-group">
                                <label for="posttype">Post Type</label>
                                <select class="form-control" id="posttype" name="post_type_id">
                                @foreach ($posttypes as $posttype)
                                <option value="{{$posttype->id}}">{{$posttype->post_type}}</option>
                                @endforeach
                                </select>
                            </div>
                            //author type for author model (author_type_id)
                            <div class="form-group">
                                <label for="authortype">Author Type</label>
                                <select class="form-control" id="authortype" name="author_type_id">
                                @foreach ($authortypes as $authortype)
                                <option value="{{$authortype->id}}">{{$authortype->author_type}}</option>
                                @endforeach
                                </select>
                            </div>
    
    
                            <div class="form-group">
                                <label for="author_name">Author Name</label>
                                <input type="text" class="form-control" name="author_name" placeholder="your name">
                            </div>

<div class="form-group">
                                <label for="author_first_name">Author First Name</label>
                                <input type="text" class="form-control" name="author_first_name" placeholder="your first name">
                            </div>
                        
//How to store author_id in post 

                            <div class="form-group">
                                <label for="content">article</label>
                                <textarea class="form-control" name="article" rows="8" cols="8"></textarea>
                                
                            </div>
                            
                            
                            <button type="submit" class="btn btn-primary">{{__('main.save')}}</button>
            </form>
        </div>
    @endsection

我找到了解决办法,希望这对你以后有帮助。

$author = Author::create([ 
    'author_type_id' => $request->author_id, 
]);

$post = Post::create([
    "post_type_id" => $request->post_type_id,
    "author_id" => $author->id,
    "author_type_id" => $request->author_type_id,
    "article" => $request->article,
]);

Auther::create([
    'author_type_id' => $request->author_id,
]);