Skip to content

Instantly share code, notes, and snippets.

@peterfox
Last active November 3, 2016 13:18
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 peterfox/ddf772ce08f2f7128073d986570e4a73 to your computer and use it in GitHub Desktop.
Save peterfox/ddf772ce08f2f7128073d986570e4a73 to your computer and use it in GitHub Desktop.
Intro to Laravel Workshop 3/11/16
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Tasks</div>
<div class="panel-body">
<ul>
@foreach(Auth::user()->tasks()->get() as $task)
<li>
{{ $task->task }}
</li>
@endforeach
</ul>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Add a Task</div>
<div class="panel-body">
<form method="post" action="/home/task" >
<div class="form-group">
<label for="task">Task</label>
<input name="task" id="task" placeholder="I need to..." class="form-control" />
</div>
{{ csrf_field() }}
<button class="btn btn-default" type="submit">
Add
</button>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
protected $fillable = [
'task'
];
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->text('task');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TaskController extends Controller
{
public function create(Request $request)
{
$request->user()->tasks()->create($request->only(['task']));
return redirect('/home');
}
}
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
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',
];
public function tasks() {
return $this->hasMany('App\Task');
}
}
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::post('/home/task', 'TaskController@create');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment