Permalink
Browse files

http://www.laragle.com/2017/04/twitter-from-scratch-using-laravel-cre…

  • Loading branch information...
darwinluague committed Apr 10, 2017
1 parent cd2f2e6 commit 958d3763676ca14d5fc801543803164ab1581fb5
Showing with 46 additions and 1 deletion.
  1. +15 −1 app/Http/Controllers/User/TweetController.php
  2. +7 −0 app/Tweet.php
  3. +10 −0 app/User.php
  4. +14 −0 tests/Feature/UserTest.php
@@ -9,6 +9,16 @@
class TweetController extends Controller
{
/**
* Create a new controller instance
*/
public function __construct()
{
$this->middleware('auth', [
'only' => ['create', 'store', 'edit', 'update', 'destroy']
]);
}
/**
* Display a listing of the resource.
*
@@ -37,7 +47,11 @@ public function create()
*/
public function store(Request $request)
{
//
$this->validate($request, [
'body' => 'required'
]);
return $request->user()->tweets()->create($request->only(['body']));
}
/**
@@ -6,6 +6,13 @@
class Tweet extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['body'];
/**
* Get the owner of the tweet.
*
@@ -38,6 +38,16 @@ class User extends Authenticatable
'password', 'remember_token',
];
/**
* Get all the user tweets
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tweets()
{
return $this->hasMany(Tweet::class);
}
/**
* Get the route key for the model.
*
@@ -69,4 +69,18 @@ public function a_user_can_verify_their_email()
$this->assertNull($user->fresh()->email_verification_token);
}
/** @test */
public function a_user_can_create_a_tweet()
{
$user = factory('App\User')->create();
$tweet = 'My first tweet';
$this->actingAs($user)
->post(route('tweet.store', ['username' => $user->username]), [
'body' => $tweet
]);
$this->assertDatabaseHas('tweets', ['body' => $tweet]);
}
}

0 comments on commit 958d376

Please sign in to comment.