mardi 2 janvier 2018

how to fetch two records with reference to single table using foreign key in laravel

I am beginner to laravel. I am trying to display all table records based on foreign key . The problem i am facing is that my Travel_offering table have two columns beginning_location and Ending_location both are foreign key referenced to location table i want to retrieve both records. i know about relationships i.e belongs to and has many. but i am able to do it for multiple columns referenced to single table

travel_offerings table

    class CreateTravelOfferingsTable extends Migration
{

public function up()
{

    Schema::create('travel_offerings', function (Blueprint $table) {

        $table->engine = 'InnoDB';
        $table->increments('id');

        $table->integer('user_id')->unsigned();
        $table->integer('begning_location')->unsigned();
        $table->integer('dest_location')->unsigned();
        $table->time('leaving_time');
        $table->date('leaving_date');
        $table->float('offering_price');
        $table->boolean('active');
        $table->timestamps();

    });

    Schema::table('travel_offerings',function (Blueprint $table){

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('begning_location')->references('id')->on('locations');
        $table->foreign('dest_location')->references('id')->on('locations');

    });

}

public function down()
{
    Schema::dropIfExists('travel_offerings');
}

}

locations table

    class CreateLocationsTable extends Migration
{

public function up()
{
    Schema::create('locations', function (Blueprint $table) {
        $table->increments('id');

        $table->string('location_spot');
        $table->string('location_town');
        $table->string('location_city');
        $table->float('location_latitude');
        $table->float('location_longitude');

        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('locations');
}
}

locations model

    class locations extends Model
{

function location()
{
  //what should i write here

}
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2CHO26Y
via IFTTT

Aucun commentaire:

Enregistrer un commentaire