I am trying to understand how to set up this relation for my application:
one to many and many to one.
what I want to achieve is this:
User has many bookings and a room has many bookings, I'm not quite sure if I need a pivot table in order to achieve it.
so far I got this
Room model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Room extends Model
{
protected $table = 'rooms';
protected $fillable = ['name','enabled'];
public function category(){
return $this->belongsTo('\App\Category','category_id');
}
public function bookings(){
return $this->hasMany('\App\Booking','room_id','id');
}
}
User model
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function bookings(){
return $this->hasMany('\App\Booking','user_id','id');
}
}
Booking model
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Booking extends Model
{
protected $table = "bookings";
protected $fillable = ['day','start','end'];
protected $dates = ['day'];
/*protected $casts = [
'is_all_day' => 'boolean'
];*/
public function room(){
return $this->belongsTo('\App\Room','room_id');
}
public function user(){
return $this->belongsTo('\App\User','user_id');
}
}
this is my migration table for bookings
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBookingTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bookings', function (Blueprint $table) {
$table->increments('id');
$table->integer('room_id')->unsigned();
$table->foreign('room_id')->references('id')->on('rooms')->ondelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->ondelete('cascade');
$table->date('day');
$table->string('start');
$table->string('end');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('bookings');
}
}
BookingController
public function store(BookingRequest $request)
{
$room = Room::find($request->room);
$booking = new Booking(array_except($request->all(),['category','room']));
//dd($booking);
$room->bookings()->save($booking);
$room->bookings()->attach(Auth::user()->id);
return redirect('bookings');
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire