loginController.php
public function show(Request $request)
{
$name=$request->input('username');
$password1=$request->input('password');
$request->validate([
'username'=>'required',
'password'=>'required'
]);
$p=DB::select('select password from users where name=?',[$name]);
foreach ($p as $datum) {
$result[] = $datum->password;
}
if($p&&$result[0]==$password1){
//dd('登录成功');
return redirect('/blade');
}else{
//dd('登录失败!用户不存在或密码错误!');
return redirect()->back();
}
}
login.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用户登录</title>
</head>
<body>
<div class="login">
<form action="{{url('loginCheck')}}" method="post">
<h2>用户登录</h2>
<div>
<label for="username">用户名:</label>
<input id="username" type="text" name="email" placeholder="请输入用户名" required>
</div>
<div>
<label for="password">密码:</label>
<input id="password" type="password" name="password" placeholder="请输入密码" required>
</div>
<div>
<input type="submit" value='登录'>
<input type="reset">
</div>
@csrf
</form>
</div>
@if(session('message'))
{{session('message')}}
@endif
@if ($errors->any())
<div class="alert alert-danger" style="width:100%">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
<style>
body{
background-color: lightgoldenrodyellow;
}
body *{
box-sizing: border-box;
}
.login form{
display: flex;
flex-direction: column;
width: 300px;
background-color: lightblue;
margin: 50px auto;
padding:10px 40px;
box-shadow: 0px 3px 6px #888;
}
.login h2{
text-align: center;
color: red;
}
.login *{
margin-bottom: 10px;
}
form div:last-of-type{
/*margin-left: 80px;*/
}
form div:last-of-type input{
margin-left: 40px;
}
.login label{
display: inline-block;
width: 50px;
text-align: right;
}
</style>
{{-- {{$data['message']}} --}}
@if(session('data'))
<script>alert("{{session('data')}}")</script>
@endif
</body>
</html>
NewsController.php
public function store(Request $request)
{
$request->validate([
'title'=>'required|min:4',
'content'=>'required'
]);
//
// dd($request->all());
$data=$request->except('_token');
$data['user_id']=1;
// dd($data);
$res=DB::table('news')->insert($data);
if($res){
return redirect('/news');
}
}