Laravel One To Many Relationship Role to preserve -


i have relationship order hasone ( belongsto ) location , location have many ( belongs many orders ) ..

which method should use each eloquent model .. model should has (has) method , , should has (belongs) method , depends on , can read in both ways ..

i need general role .. when read relationship in 2 ways .. order hasone location , location belongstomany orders . , .. order belongsto location , location hasmany orders .

what role here ??

this tables have ..

location table..          schema::create('locations', function (blueprint $table) {             $table->increments('id');             $table->integer('location_id')->unsigned()->index();             $table->integer('manager_id')->unsigned()->index();             $table->string('name');             $table->string('address')->nullable();             $table->string('city');             $table->float('latitude')->nullable();             $table->float('longitude')->nullable();             $table->timestamps();         }); ````  , orders table  ````         schema::create('orders', function (blueprint $table) {             $table->increments('id')->unsigned()->index();             $table->string('title');             $table->text('description');             $table->string('trade');             $table->string('contact');             $table->enum('priority',['regular-72h','important-48h','urgent-24h','crisis-psh']);             $table->text('notes')->nullable();             $table->integer('location_id')->unsigned();             $table->integer('user_id')->unsigned()->index();             $table->timestamp('entry');             $table->timestamp('exit');             $table->integer('close_key')->unsigned();             $table->softdeletes();             $table->timestamps();         }); 

i searched here same point asked did not find clear .

class location { ...     public function orders() {         return $this->hasmany('app\order');     } }   class order { ...     public function location() {         return $this->belongsto('app\location');     } } 

think of way:

your location can have many orders(hasmany) , 1 order belongs location(belongsto).

more general explanation can find here.


Comments