Wednesday, March 28, 2018

need help for laravel: How i attach my disease table with others - development

need help for laravel: How i attach my disease table with others

I an new to Laravel.
i want when i store data that is storing correctly in multiple tables.
after storing it will also have the id of all three tables on disease_symptoms_medicines table( foreignkeys)
i not know how to do it.
help me!!!
In this code there is no error (if error due to poor english and typing mistakes)

here is my migrations

<?php

    use IlluminateSupportFacadesSchema;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateDatabaseMigrationsMigration;

    class CreateDiseasesTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('diseases', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name')->nullable();
                $table->string('slug')->nullable();
                $table->text('description')->nullable();
                $table->text('symptoms')->nullable();
                $table->timestamps();
            });
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('diseases');
        }
    }

symtom migration

public function up()
{
    Schema::create('symptoms', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

Medicine migration

public function up()
{
    Schema::create('diseases', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->nullable();
        $table->string('slug')->nullable();
        $table->text('description')->nullable();
        $table->text('symptoms')->nullable();
        $table->timestamps();
    });
}

created for foreign keys to attach Disease,Symptoms and Medicine

Disease Model

    class Disease extends Model
    {
protected $table ='diseases';
public function symptoms(){

    return $this->belongsTo('AppSymptoms');
}
public function medicines(){

    return $this->belongsTo('AppMedicine');
}

Symtom Model

    class Symptoms extends Model{
    protected $table ='symptoms';
    public function diseases()
    {
    return $this->hasMany('AppDisease');
}

}

Medicine Model

    class Medicine extends Model{
    protected $table ='medicines';
    public function diseases(){

    $this->hasMany('AppDisease');
}
}

here is my controller

    public function store(Request $request)
{
    $disease=new Disease;
    $disease->name=$request->input('name');
    $disease->slug=$request->input('slug');
    $disease->description=$request->input('description');
    $disease->save();
    $symptom=new Symptoms;
    $symptom->name=$request->input('symtom');
    $symptom->save();
    $medicine=new Medicine;
    $medicine->name = $request->input('medic');
    $$medicine->save();

    return redirect()->back();
}

i want when i store data that is storing correctly in multiple tables.
after storing it will also have the id on disease_symptoms_medicines table( foreignkeys)
i not know how to do it.
help me!!!



from Laravel Questions and Answers https://laravelquestions.com/php/need-help-for-laravel-how-i-attach-my-disease-table-with-others/
via Lzo Media

No comments:

Post a Comment