Skip to content

Instantly share code, notes, and snippets.

@siljanoskam
Last active April 25, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siljanoskam/8394a0ebd3a08737826e4d1cd016334a to your computer and use it in GitHub Desktop.
Save siljanoskam/8394a0ebd3a08737826e4d1cd016334a to your computer and use it in GitHub Desktop.
Models & Migrations
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('description');
$table
->dateTime('date')
->nullable();
$table->boolean('status');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table
->foreign('user_id')
->references('id')
->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Task extends Model
{
protected $fillable = ['title', 'description', 'date', 'user_id'];
/**
* The user that the task belongs to
*
* @return BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The tasks that the user has
*
* @return HasMany
*/
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment