Finish the good function of our first step.
Controllers/GoodController: Finish the good function of our first step. Controllers/UserController: Make session to be run rightly. app/Http/routes.php: Add middleware web to every route. database/migrations: Change column names such as 'good_id' to 'id'. views/User.register: Add csrf_field to the form. views/User.show: Add csrf_field to the form. views/good: Finish the good function of our first step.
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\GoodCat;
|
||||
use App\GoodInfo;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class GoodController extends Controller
|
||||
{
|
||||
public function getList(Request $request)
|
||||
{
|
||||
$cats = GoodCat::orderby('cat_name', 'asc')->get();
|
||||
$goods = GoodInfo::orderby('id', 'asc')->get();
|
||||
if($request->session()->has('userid')) $userid = $request->session()->get('userid'); else $userid = NULL;
|
||||
return view('good.goodList', [
|
||||
'cats' => $cats,
|
||||
'goods' => $goods,
|
||||
'userid' => $userid,
|
||||
]);
|
||||
}
|
||||
public function getInfo(Request $request, $good_id)
|
||||
{
|
||||
$goods = GoodInfo::where('id', $good_id)->get();
|
||||
if($request->session()->has('userid')) $userid = $request->session()->get('userid'); else $userid = NULL;
|
||||
return view('good.goodInfo', [
|
||||
'goods' => $goods,
|
||||
'userid' => $userid,
|
||||
]);
|
||||
}
|
||||
public function addPage(Request $request)
|
||||
{
|
||||
if(!$request->session()->has('userid')) return redirect()->back();
|
||||
$cats = GoodCat::orderby('cat_name', 'asc')->get();
|
||||
return view('good.addPage', [
|
||||
'cats' => $cats,
|
||||
]);
|
||||
}
|
||||
public function addGood(Request $request)
|
||||
{
|
||||
if(!$request->session()->has('userid')) return redirect()->back();
|
||||
$validator = Validator::make($request->all(), [
|
||||
'good_name' => 'required',
|
||||
'description' => 'required',
|
||||
'pricemin' => 'required',
|
||||
'pricemax' => 'required',
|
||||
'counts' => 'required',
|
||||
]);
|
||||
if($validator->fails()) return redirect('/good/addPage')->withInput()->withErrors($validator);
|
||||
$good = new GoodInfo;
|
||||
$good->good_name=$request->good_name;
|
||||
$good->cat_id=$request->cat_id;
|
||||
$good->description=$request->description;
|
||||
$good->pricemin=$request->pricemin;
|
||||
$good->pricemax=$request->pricemax;
|
||||
$good->type=$request->type;
|
||||
$good->counts=$request->counts;
|
||||
$good->good_tag=$request->good_tag;
|
||||
$good->user_id=$request->session()->get('userid');
|
||||
$good->save();
|
||||
return redirect('/good/addPage');
|
||||
}
|
||||
public function editPage(Request $request, $good_id)
|
||||
{
|
||||
$cats = GoodCat::orderby('cat_name', 'asc')->get();
|
||||
$goods = GoodInfo::where('id', $good_id)->get();
|
||||
return view('good.editPage', [
|
||||
'goods' => $goods,
|
||||
'cats' => $cats,
|
||||
]);
|
||||
}
|
||||
public function editGood(Request $request, $good_id)
|
||||
{
|
||||
if(!$request->session()->has('userid')) return redirect()->back();
|
||||
$validator = Validator::make($request->all(), [
|
||||
'good_name' => 'required',
|
||||
'cat_id' => 'required',
|
||||
'description' => 'required',
|
||||
'pricemin' => 'required',
|
||||
'pricemax' => 'required',
|
||||
'type' => 'required',
|
||||
'counts' => 'required',
|
||||
]);
|
||||
if($validator->fails()) return redirect('/good/'.$good_id.'/editPage')->withInput()->withErrors($validator);
|
||||
$good = GoodInfo::find($good_id);
|
||||
if($request->session()->get('user_id')!=$good->user_id) return redirect()->back();
|
||||
$good->good_name=$request->good_name;
|
||||
$good->cat_id=$request->cat_id;
|
||||
$good->description=$request->description;
|
||||
$good->pricemin=$request->pricemin;
|
||||
$good->pricemax=$request->pricemax;
|
||||
$good->type=$request->type;
|
||||
$good->counts=$request->counts;
|
||||
$good->good_tag=$request->good_tag;
|
||||
$good->update();
|
||||
return redirect('/good/'.$good_id.'/editPage');
|
||||
}
|
||||
public function deleteGood(Request $request, $good_id)
|
||||
{
|
||||
if(!$request->session()->has('userid')) return redirect()->back();
|
||||
$good = GoodInfo::find($good_id);
|
||||
if($request->session()->get('user_id')!=$good->user_id) return redirect()->back();
|
||||
$good->delete();
|
||||
return redirect('/good');
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use Illuminate\Support\Facades\Validator;
|
||||
use App\User;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class UserController extends Controller
|
||||
@@ -50,11 +51,11 @@ class UserController extends Controller
|
||||
public function login(Request $request)
|
||||
{
|
||||
$user = User::where('username',$request->username)->first();
|
||||
echo "ss";
|
||||
if($user->password==$request->password)
|
||||
{
|
||||
session(['username'=>$request->username]);
|
||||
return redirect('/userinfo');
|
||||
$request->session()->put('userid', $user->id);
|
||||
$request->session()->put('username', $user->username);
|
||||
return redirect('/good');
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
+44
-4
@@ -10,6 +10,7 @@
|
||||
| and give it the controller to call when that URI is requested.
|
||||
|
|
||||
*/
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
Route::get('/', function () {
|
||||
return view('welcome');
|
||||
@@ -31,14 +32,53 @@ Route::group(['middleware' => ['web']], function () {
|
||||
});
|
||||
|
||||
Route::post('/adduser', [
|
||||
"uses" => "UserController@adduser"
|
||||
"uses" => "UserController@adduser",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
Route::get('/show', [
|
||||
"uses" => "UserController@show"
|
||||
"uses" => "UserController@show",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
Route::get('/register', [
|
||||
"uses" => "UserController@register"
|
||||
"uses" => "UserController@register",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
Route::post('/login', [
|
||||
"uses" => "UserController@login"
|
||||
"uses" => "UserController@login",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
|
||||
Route::get('/good', [
|
||||
"uses" => "GoodController@getList",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
|
||||
Route::get('/good/addPage', [
|
||||
"uses" => "GoodController@addPage",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
|
||||
Route::post('/good/add', [
|
||||
"uses" => "GoodController@addGood",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
|
||||
Route::get('/good/{good_id}/editPage', [
|
||||
"uses" => "GoodController@editPage",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
|
||||
Route::post('/good/{good_id}/edit', [
|
||||
"uses" => "GoodController@editGood",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
|
||||
Route::get('/good/{good_id}/delete', [
|
||||
"uses" => "GoodController@deleteGood",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
|
||||
Route::get('/good/{good_id}', [
|
||||
"uses" => "GoodController@getInfo",
|
||||
"middleware" => "web"
|
||||
]);
|
||||
@@ -13,7 +13,7 @@ class CreateGoodCatTable extends Migration
|
||||
public function up()
|
||||
{
|
||||
Schema::create('good_cat',function(Blueprint $table){
|
||||
$table->increments('cat_id');
|
||||
$table->increments('id');
|
||||
$table->string('cat_name');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ class CreateGoodInfoTable extends Migration
|
||||
public function up()
|
||||
{
|
||||
Schema::create('good_info',function(Blueprint $table){
|
||||
$table->increments('good_id');
|
||||
$table->increments('id');
|
||||
$table->string('good_name');
|
||||
$table->integer('cat_id');
|
||||
$table->text('description');
|
||||
|
||||
@@ -14,7 +14,6 @@ class CreateTagsTable extends Migration
|
||||
{
|
||||
Schema::create('tags', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('tag_id');
|
||||
$table->string('tag_name');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ class CreateMessagesTable extends Migration
|
||||
public function up()
|
||||
{
|
||||
Schema::create('messages', function (Blueprint $table) {
|
||||
$table->increments('message_id');
|
||||
$table->increments('id');
|
||||
$table->integer('cat_id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('good_id');
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
<div>
|
||||
学号:<input type="text" name="stuid">
|
||||
</div>
|
||||
{!! csrf_field() !!}
|
||||
<div>
|
||||
<input type="submit" name="submit" value="注册">
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
{!! csrf_field() !!}
|
||||
<html>
|
||||
<title>登陆</title>
|
||||
<body>
|
||||
@@ -9,6 +8,7 @@
|
||||
<div>
|
||||
密码:<input type="text" name="password">
|
||||
</div>
|
||||
{!! csrf_field() !!}
|
||||
<div>
|
||||
<input type="submit" name="submit" value="登陆">
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<form action="/good/add" method="POST">
|
||||
good_name:<input name="good_name"/><br/>
|
||||
cat:<select name="cat_id">
|
||||
@foreach($cats as $cat)
|
||||
<option value="{{$cat->id}}">{{$cat->cat_name}}</option>
|
||||
@endforeach
|
||||
</select><br/>
|
||||
description:<textarea name="description"></textarea><br/>
|
||||
pricemin:<input name="pricemin"/><br/>
|
||||
pricemax:<input name="pricemax"/><br/>
|
||||
type:<select name="type">
|
||||
<option value="0">normal</option>
|
||||
<option value="1">auction</option>
|
||||
</select><br/>
|
||||
counts:<input name="counts"/><br/>
|
||||
good_tag:<input name="good_tag"/><br/>
|
||||
{!! csrf_field() !!}
|
||||
<input type="submit"/>
|
||||
</form>
|
||||
@if(count($errors) > 0)
|
||||
@foreach($errors as $error)
|
||||
{{$error}}
|
||||
@endforeach
|
||||
@endif
|
||||
<a href='/good'>GoodList</a>
|
||||
@@ -0,0 +1,27 @@
|
||||
@foreach($goods as $good)
|
||||
<form action="/good/{{$good->id}}/edit" method="POST">
|
||||
good_name:<input name="good_name" value="{{$good->good_name}}"/><br/>
|
||||
cat:<select name="cat_id">
|
||||
@foreach($cats as $cat)
|
||||
<option value="{{$cat->id}}" @if($good->cat_id==$cat->id) selected="selected" @endif>{{$cat->cat_name}}</option>
|
||||
@endforeach
|
||||
</select><br/>
|
||||
description:<textarea name="description">{{$good->description}}</textarea><br/>
|
||||
pricemin:<input name="pricemin" value="{{$good->pricemin}}"/><br/>
|
||||
pricemax:<input name="pricemax" value="{{$good->pricemax}}"/><br/>
|
||||
type:<select name="type">
|
||||
<option value="0" @if($good->type==0) selected="selected" @endif>normal</option>
|
||||
<option value="1" @if($good->type==1) selected="selected" @endif>auction</option>
|
||||
</select><br/>
|
||||
counts:<input name="counts" value="{{$good->counts}}"/><br/>
|
||||
good_tag:<input name="good_tag" value="{{$good->good_tag}}"/><br/>
|
||||
{!! csrf_field() !!}
|
||||
<input type="submit"/>
|
||||
</form>
|
||||
@endforeach
|
||||
@if(count($errors) > 0)
|
||||
@foreach($errors as $error)
|
||||
{{$error}}
|
||||
@endforeach
|
||||
@endif
|
||||
<a href='/good/{{$good->id}}'>GoodInfo</a>
|
||||
@@ -0,0 +1,8 @@
|
||||
@foreach($goods as $good)
|
||||
{{$good->description}}<br/>
|
||||
@if($userid==$good->user_id)
|
||||
<a href='/good/{{$good->id}}/editPage'>Edit</a><br/>
|
||||
<a href='/good/{{$good->id}}/delete'>Delete</a><br/>
|
||||
@endif
|
||||
@endforeach
|
||||
<a href='/good'>GoodList</a>
|
||||
@@ -0,0 +1,17 @@
|
||||
<ul>
|
||||
@foreach($cats as $cat)
|
||||
<li>{{$cat->cat_name}}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
<br/>
|
||||
<br/>
|
||||
<br/>
|
||||
@foreach($goods as $good)
|
||||
<div class="cat{{$good->cat_id}}"><a href="/good/{{$good->id}}">{{$good->good_name}}</a></div>
|
||||
@endforeach
|
||||
<br/>
|
||||
<br/>
|
||||
<br/>
|
||||
@if($userid!=NULL)
|
||||
<a href='/good/addPage'>Add</a>
|
||||
@endif
|
||||
Reference in New Issue
Block a user