phplaraveleloquentlaravel-7laravel-response

Laravel insert data to multiple relational tables with a single form


I'm working on Laravel project and i would like to know: how to insert data to my multiple related tables ? How can we insert author id in the author_type_id field of the Author table? How to store author_id in post? So idon't know how to insert related models using a form. thanks for your help :)

my models

//Post model

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);
        }
    }

//Posttype model

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

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

//author model

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);
    }
}

//Authortype model

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 Contoller

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();
    }
}

//create post blade

    @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

Solution

  • I found solution, May this can help you in future.

    $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,
    ]);