KHN-プログラミングブログ-

CRUD文

CREATE文

use App\Models\User;
use Illuminate\Http\Request;

public function userAdd($request){
    $user = new User;
    $user->name = "太郎";
    $user->kana = $request->kana;
    $user->save();
}

Read文

use App\Models\User;
use Illuminate\Http\Request;

public function Userfind(Request $id){
    $user = User::find($id);

    return $user->name;
}

Update文

use App\Models\User;
use Illuminate\Http\Request;

public function UserUpdate(Request $request){
    $user = User::find($request->id);
    $user->name = "新しい名前";
    $user->kana = $request->kana;
    $user->save();
}

Delete文

use App\Models\User;
use Illuminate\Http\Request;

  public function Userdelete(Request $request)
  {
    $user = User::find($request->id);
    $user->delete();
  }