여러분의 후원은 큰 힘이 됩니다. 기업은행 35611080101018
반응형
기본
/routes/api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostC;
use App\Http\Controllers\standard\login;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/posts',[PostC::class, 'create']);
App/Http/Controllers/PostC.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostC extends Controller
{
public function index(){
#use App\Models\Post;
return response()->json(Post::all());
}
public function create(Request $request) {
$subject = $request->input('subject');
$content = $request->input('content');
$post = new Post();
$post->subject = $subject;
$post->content = $content;
$post->save();
return request()->json($post);
}
}
App/Models/post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
}
/databases/migrations/2023_07_22_203808_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('subject', 196);
$table->longText('content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};


반응형
'PHP > Larabel' 카테고리의 다른 글
| 로컬에서 간편하게 개발환경 설치하기 (0) | 2023.07.23 |
|---|---|
| 미들웨어 (0) | 2023.07.21 |
| 서비스 컨테이너, 서비스 프로바이더. (0) | 2023.07.21 |
| Route Post (0) | 2023.07.17 |
| Route Get (0) | 2023.07.17 |