Source Kode Random

Source Code Random


Thread -mr
Reply -mc
D: and cd camp/mysql/bin
mysql -u root
create database dbforum (choice nama db what u want)
config/app =>the name of laravel you can change it
php artisan make:model Thread -mr
php artisan make:model Reply -mc
php artisan make:migration tbl_sance --create=dalam_table

php artisan =>show option $threads = factory('App\Thread',50)->create();
$threads->each(function($thread){factory('App\Reply',10)->create(['thread_id'=>$thread->id]);});

php artisan migrate
php artisan migrate:refresh
php artisan serve
php artisan make:auth =>show login,register etc

laravel new forum

web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

// Auth::routes();

// Route::get('/home', 'HomeController@index')->name('home');

// Auth::routes();

// Route::get('/home', 'HomeController@index')->name('home');

// Route::get('/Threads', 'ThreadsController@index');

// Auth::routes();


Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
// Route::get('/Threads', 'ThreadsController@index');
 Route::get('/Threads/create','ThreadsController@create');
// Route::post('/Threads','ThreadsController@store');
Route::get('/Threads/{thread}', 'ThreadsController@show');
Route::resource('/Threads/','ThreadsController');
Route::post('/Threads/{thread}/replies','RepliesController@store');

home.blade.php

@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">Dashboard</div>

                <div class="panel-body">
                    You are logged in!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

show.blade.php
@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">
                    <a href="#">{{$thread->creator->name}}</a> Posted :
                    {{$thread->title}}
                 </div>

                <div class="panel-body">
                  {{$thread->body}}

                </div>
            </div>
        </div>
    </div>

     <div class="row">
        <div class="col-md-8 col-md-offset-2">
            @foreach($thread->replies as $reply)
                   @include('threads.reply');
            @endforeach
        </div>
    </div>

    @if (auth()->check())
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
           <form method="POST" action="{{$thread->path().'/replies'}}">            
            {{csrf_field()}}
              <div class="form-group">               
               <textarea name="body" id="body" class="form-control" placeholder="Have something to say" rows="5"></textarea>
               </div>
               <button type="submit" class="btn btn-default">Post</button>
           </form>
        </div>
    </div>
    @else
      <p class="text-center">Please<a href="{{route('login')}}"> Sign in </a>to participate in this discussion</p>
    @endif
</div>
@endsection

reply.blade.php
 <div class="panel panel-default">
                    <div class="panel-heading">                            
                       <a href="#">
                            {{$reply->owner->name}}</a>
                            said
                            {{$reply->created_at->diffForHumans()}}....
                         </div>                  
                        <div class="panel-body">
                          {{$reply->body}}
                     </div>
  </div>

index.blade.php
@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">Threads </div>

                <div class="panel-body">
                    @foreach($threads as $thread)
                     <article>
                     <a href="{{$thread->path()}}">
                         <h4>{{$thread->title}}</h4>
                    </a>
                         <div class="body">{{$thread->body}}</div>
                     </article>
                     <hr>
                     @endforeach

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

create.blade.php
@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">Create A New Threads </div>

                <div class="panel-body">
                    <form method="POST" action="/Threads">
                        {{csrf_field()}}
                        <div class="form-group">
                            <label for="title">title :</label>
                            <input type="text" class="form-control" id="title" placeholder="title" name="title">
                        </div>
                        <div class="form-group">
                            <label for="body">body</label>
                            <textarea name="body" id="body" class="form-control" rows="8"></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary">Publish</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection



Controller

ThreadsController.php
<?php

namespace App\Http\Controllers;

use App\thread;
use Illuminate\Http\Request;

class ThreadsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        // $this->middleware('auth')->only('store');
        $this->middleware('auth')->except(['index','show']);
    }


    public function index()
    {
        $threads=Thread::latest()->get();
        return view('threads.index',compact('threads'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('threads.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $thread=Thread::create([
            'user_id'=>auth()->id(),
            'title'=>request('title'),
            'body'=>request('body')
            ]);
        return redirect($thread->path()); 
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\thread  $thread
     * @return \Illuminate\Http\Response
     */
    public function show(thread $thread)
    {
        return view('threads.show',compact('thread'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\thread  $thread
     * @return \Illuminate\Http\Response
     */
    public function edit(thread $thread)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\thread  $thread
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, thread $thread)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\thread  $thread
     * @return \Illuminate\Http\Response
     */
    public function destroy(thread $thread)
    {
        //
    }
}

RepliesController.php
<?php

namespace App\Http\Controllers;
use App\thread;
use Illuminate\Http\Request;

class RepliesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
    public function store(thread $thread)
    {
    $thread->addReply([
    'body'=>request('body'),
    'user_id'=>auth()->id()
    ]);
    return back();
    }
}


Providers

Reply.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class reply extends Model
{
protected $guarded = [];
    public function owner()
    {
    return $this->belongsTo(User::class,'user_id');
    }
}

Thread.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class thread extends Model
protected $guarded = [];
    public function path()
    {
    return '/Threads/'.$this->id;
    }
    public function replies()
    {
    return $this->hasmany(Reply::class);
    }
    public function creator()
    {
    return $this->belongsTo(User::class,'user_id');
    }
    public function addReply($reply)
    {
    $this->replies()->create($reply);
    }
}


Database 

db_replis

<?php

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

class CreateRepliesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('replies', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('thread_id');
            $table->integer('user_id');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('replies');
    }
}

db_threads

<?php

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

class CreateThreadsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('threads', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('threads');
    }
}


db_create_pass

    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

db_user

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }