Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
database/migrations/2016_05_11_094101_create_posts_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |