Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel中使用外键参照单个表获取两条记录_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 如何在laravel中使用外键参照单个表获取两条记录

Php 如何在laravel中使用外键参照单个表获取两条记录,php,laravel,laravel-5,Php,Laravel,Laravel 5,我是拉威尔的初学者。我正在尝试根据外键显示所有表记录。我面临的问题是,我的Travel\u offering表有两列,开始位置和结束位置都是外键引用到我要检索这两个记录的位置表。我了解人际关系,也就是说,我拥有很多。但我无法对引用到单个表的多个列执行此操作 旅游产品表 class CreateTravelOfferingsTable extends Migration { public function up() { Schema::create('travel_

我是拉威尔的初学者。我正在尝试根据外键显示所有表记录。我面临的问题是,我的Travel\u offering表有两列,开始位置和结束位置都是外键引用到我要检索这两个记录的位置表。我了解人际关系,也就是说,我拥有很多。但我无法对引用到单个表的多个列执行此操作

旅游产品表

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

}
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');
    }
}
位置表

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

}
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');
    }
}
位置模型

class locations extends Model
{
    function location()
    {
        //what should I write here

    }
}

您必须创建两个关系,每个关系一个。然后你可以说:
Location::with('dist1','dist2')->get()

你必须在locations.php中编写locations模型与另一个模型的关系 例如,如果您的位置模型与TravelOffering模型有一对一的关系,那么您的模型文件应该是这样的

class locations extends Model
{
    function travel_offering ()
    {
        return $this->hasOne('App\TravelOffering','foreign_key', 'local_key');
    }
}
您可以在这里定义多个关系。看看拉威尔的关系。通过定义关系,您可以基于关系检索值,这意味着您不再需要编写嵌套查询。 如果你有一套有说服力的地点模型,你也可以得到TravelOffering

$a = locations::find(1);
$TravelOffering = $a->travel_offering;
*您应该使用大写的单数单词命名模型,外键应与parent_table_name_id类似,因为elounce根据模型名称确定关系的外键。如果这样做,则不必指定与之相关的外键