namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public $fillable = ['title','description'];
}
迁移文件
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop("posts");
}
}
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
factory(App\Post::class, 50)->create();
}
}
路由
Route::get('my-post', 'PostController@myPost');
控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
class PostController extends Controller
{
public function myPost(Request $request)
{
$posts = Post::paginate(6);
if ($request->ajax()) {
$view = view('data',compact('posts'))->render();
return response()->json(['html'=>$view]);
}
return view('my-post',compact('posts'));
}
}