Dark Cyber
Dark Cyber

Reputation: 2231

Laravel 5.2 How laravel treat this belongsto relationship?

I have 3 tables, with the fields listed below:

Pricings

Routes

Cities

So far the relationships of fields are: pricings belong to a route, and routes belongs to city.

But I'm not sure about these relationships, since from_city_id and to_city_id are foreign keys relating to id on cities.

Maybe I'm wrong designing my table or something else.

route_id is a foreign key to id on the Routes table. from_city_id and to_city_id are foreign keys to id on the Cities table.

How I can define relationships of these 3 tables so I can get from city name and to city name from the pricings model, like $pricing->from_city->name and $pricing->to_city->name?

Any help appreciated.

UPDATE:

My Pricing Model:

public function route()
{
    return $this->belongsTo(Route::class);    
}

My Route Model:

public function pricing(){
    return $this->hasOne(Pricing::class);    
}

public function zone(){
    return $this->belongsTo(Zone::class);    
}

public function city(){
    return $this->belongsTo(City::class);    
}

public function from_city(){
    return $this->belongsTo(City::class);    
}

public function to_city(){
    return $this->belongsTo(City::class);
}

Now I can use $pricing->route->from_city->name and $pricing->route->to_city->name

It shows the correct result, but how can this be achieved using Laravel?

Does this mean Laravel will assume that the route table has fields to_city_id and from_city_id, since the method in the route model is to_city() and from_city()?

Thanks

Upvotes: 1

Views: 192

Answers (2)

jeremykenedy
jeremykenedy

Reputation: 4285

One solution may be to make a migration (new table or to change to existing table). https://laravel.com/docs/5.3/migrations

Laravel's schema build is super handy: https://laravel.com/docs/5.0/schema

An example of the routes migration would be:

  1. Make the migration:

    php artisan make:migration routes

  2. The migration would look something like:

```

use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;

class CreateUserRole extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('routes', function (Blueprint $table) {

        $table->increments('id');
        $table->foreign('id')->references('route_id')->on('pricings')->onDelete('cascade');

        $table->integer('from_city_id')->unsigned()->index();
        $table->foreign('from_city_id')->references('id')->on('cities')->onDelete('no action');             

        $table->integer('to_city_id')->unsigned()->index();
        $table->foreign('to_city_id')->references('id')->on('cities')->onDelete('no action');    

        $table->timestamps();

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

}

```

The above for some reason will not show correctly on here, so here is a cleaned up view link: http://viper-7.com/kfgUjt

Upvotes: 2

Balraj Allam
Balraj Allam

Reputation: 611

Try this one:

In Pricing Model:

//if pricing and route have one to one(hasOne) relationship or you may change it to one to many(hasMany)

public function routes(){
    return $this->hasOne('App\Routes','id','route_id'); 
}

and in Route Model:

public function from_city(){
   return $this->hasOne('App\Cities','id','from_city_id');
}

public function to_city(){
       return $this->hasOne('App\Cities','id','to_city_id');
}

Upvotes: 0

Related Questions