Skip to content

Commit

Permalink
added front-main page for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
arguv committed May 11, 2016
1 parent 0b9a322 commit 8848d8c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 4 deletions.
31 changes: 31 additions & 0 deletions app/Http/Controllers/IndexController.php
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Post;
use App\User;
use Illuminate\Pagination\PaginationServiceProvider;

class IndexController extends Controller
{
/**
* Show the application main page.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$all_Posts = Post::latest()->published()->paginate(3);

foreach($all_Posts as $item){

$idOfUser = $item->user_id;
$userName = User::find($idOfUser)->name;
$item->setAttribute('user_name', $userName);
}

return view('index')->with('posts_data', $all_Posts);
}
}
6 changes: 4 additions & 2 deletions app/Http/routes.php
Expand Up @@ -11,9 +11,11 @@
|
*/

Route::get('/', function () {
/*Route::get('/', function () {
return view('welcome');
});
});*/

Route::get('/','IndexController@index');

Route::auth();

Expand Down
35 changes: 35 additions & 0 deletions app/Models/Post.php
@@ -0,0 +1,35 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Post extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'lrvl_posts';

protected $primaryKey = 'id';

protected $fillable = [

'title',
'content',
'user_id'
];

/**
* Function filtering date of posts.
*
*/
public function scopePublished($query){

$query->where('created_at', '<=', Carbon::now());
}

}
11 changes: 9 additions & 2 deletions app/User.php
Expand Up @@ -11,8 +11,14 @@ class User extends Authenticatable
*
* @var array
*/
protected $table = 'users';

protected $primaryKey = 'id';

protected $fillable = [
'name', 'email', 'password',
'name',
'email',
'password',
];

/**
Expand All @@ -21,6 +27,7 @@ class User extends Authenticatable
* @var array
*/
protected $hidden = [
'password', 'remember_token',
'password',
'remember_token',
];
}
34 changes: 34 additions & 0 deletions database/migrations/2016_05_11_094101_create_posts_table.php
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('lrvl_posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title',128);
$table->text('content');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('lrvl_posts');
}
}
22 changes: 22 additions & 0 deletions resources/views/index.blade.php
@@ -0,0 +1,22 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
@foreach($posts_data as $item)

<h1>{{$item->title}}</h1>
<p>{{$item->content}}</p>
<p>created by {{$item->user_name}}</p>
<hr>
@endforeach
</div>

<!-- Pager -->
<div class="col-md-7 col-md-offset-3">
{!! $posts_data->render() !!}
</div>
</div>
</div>
@endsection

0 comments on commit 8848d8c

Please sign in to comment.