Building a Blog in Laravel – Part5 – User Profile and Comments

Oct 27,2017 at 01:11 pm By Ben Hutton

Now to finish things off.  All that’s left is to setup the User Profile view and allow adding Comments.  Again starting off with tests.

The first article in this series can be found here.

Starting off with Comments

UserTest.php

public function testAddComment()
{
    $this->withoutMiddleware();
    $user = $this->authenticateUser();

    factory(Posts::class)->create(
        [
            'author_id' => 1,
            'id' => 1,
            'title' => 'test34534',
            'body' => '123',
            'slug' => 'test234234',
            'active' => True,
        ]);

    $comment = factory(Posts::class)->raw(
        [
            'body' => 'some text',
            'on_post' => 1
        ]);

    $response = $this->actingAs($user)->post('/comment/add', $comment);
    $response->assertStatus(302);
}

CommentController.php

<?php

namespace App\Http\Controllers;

use App\Posts;
use App\Comments;
use Redirect;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    /**
     * Display create post form
     *
     * @param Request $request
     * @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        return view('posts.create');
        // if user can post i.e. user is admin or author
        if($request->user()->can_post())
        {
            return view('posts.create');
        }
        else
        {
            return redirect('/')->withErrors('You have not sufficient permissions for writing post');
        }
    }

    public function store(Request $request)
    {
        //on_post, from_user, body
        $input['from_user'] = $request->user()->id;
        $input['on_post'] = $request->input('on_post');
        $input['body'] = $request->input('body');
        $slug = $request->input('slug');
        Comments::create( $input );
        return redirect($slug)->with('message', 'Comment published');
    }
}

Add a new route to web.php

Route::post('/comment/add','CommentController@store');

 

Now for the User Profile

UserTest.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;


class UserTest extends TestCase
{
    use DatabaseMigrations;

    /**
     * Check user can login and see new-post form
     */
    public function testProfileView()
    {
        $this->authenticateUser();
        $response = $this->get('/user/1');
        $response->assertViewIs('admin.profile');
    }

    /**
     * Create mock user ensuring role is set to author
     *
     * @return User
     */
    public function authenticateUser(): User
    {
        return factory(User::class)->create([
            'id' => 1,
            'name' => 'fred',
            'role' => 'author']);
    }
}

Add the following to UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    /**
     * profile for user
     */
    public function profile(Request $request, $id)
    {
        $data['user'] = User::find($id);
        if (!$data['user'])
            return redirect('/');
        if ($request -> user() && $data['user'] -> id == $request -> user() -> id) {
            $data['author'] = true;
        } else {
            $data['author'] = null;
        }
        $data['comments_count'] = $data['user'] -> comments -> count();
        $data['posts_count'] = $data['user'] -> posts -> count();
        $data['posts_active_count'] = $data['user'] -> posts -> where('active', '1') -> count();
        $data['posts_draft_count'] = $data['posts_count'] - $data['posts_active_count'];
        $data['latest_posts'] = $data['user'] -> posts -> where('active', '1') -> take(5);
        $data['latest_comments'] = $data['user'] -> comments -> take(5);
        return view('admin.profile', $data);
    }
}

And finally add another route to web.php

Route::get('/user/{id}','UserController@profile')->where('id', '[0-9]+');

Leave a comment

Login to Comment

Copyright © 2017 | Ben Hutton