溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用laravel框架怎么實(shí)現(xiàn)表單操作

發(fā)布時(shí)間:2021-06-09 17:00:21 來(lái)源:億速云 閱讀:204 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了使用laravel框架怎么實(shí)現(xiàn)表單操作,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

1、MVC數(shù)據(jù)流動(dòng)

拿到一個(gè)laravel項(xiàng)目最基本的是弄清楚它的頁(yè)面請(qǐng)求、數(shù)據(jù)流動(dòng)是怎樣進(jìn)行的,比如當(dāng)通過(guò)get請(qǐng)求index頁(yè)面時(shí),如何顯示如下的學(xué)生信息列表:

使用laravel框架怎么實(shí)現(xiàn)表單操作

首先當(dāng)一個(gè)頁(yè)面請(qǐng)求到達(dá)時(shí),需要在routes/web.php中定義路由請(qǐng)求以及對(duì)應(yīng)的處理方法:

Route::get('index','StudentController@getIndex');

然后在.env文件下設(shè)置好數(shù)據(jù)庫(kù)連接,新建數(shù)據(jù)庫(kù)模型Student放在app/目錄下,在其中指定對(duì)應(yīng)的數(shù)據(jù)表為student

class Student extends Model
{
  protected $table='student';       //指定數(shù)據(jù)庫(kù)
  protected $fillable=['name','age','sex'];  //允許修改的字段
}

新建控制類(lèi)StudentController并實(shí)現(xiàn)getIndex方法,在getIndex方法中調(diào)用student/index.blade.php頁(yè)面,并通過(guò)Student模型查詢(xún)到學(xué)生信息傳遞給view

public static function getIndex(){
  return view('student.index',['students'=>Student::paginate(5)]);
}

實(shí)現(xiàn)頁(yè)面視圖,在resources/views文件夾下新建student文件夾用于存放student相關(guān)頁(yè)面。

采用模板的思路來(lái)實(shí)現(xiàn)index頁(yè)面:新建頁(yè)面的模板文件layout.blade.php文件,保留其中的公共部分,將其中不同的地方通過(guò)@section或者@yield替換。新建index.blade.php繼承l(wèi)ayout模板公共的部分,并在其中實(shí)現(xiàn)index頁(yè)面自定義的部分

@extends('student.layout')
@section('title')
  主頁(yè)
  @stop
@section('content')
  <!-- index頁(yè)面自定義內(nèi)容-->
  @stop
    在自定義內(nèi)容里通過(guò)@foreach將學(xué)生數(shù)據(jù)信息循環(huán)顯示到列表
@foreach($students as $student)
  <tr>
    <th scope="row">{{$student->id}}</th>
    <td>{{$student->name}}</td>
    <td>{{$student->age}}</td>
    <td>{{$student->sex}}</td>
    <td>{{$student->created_at}}</td>
  </tr>
@endforeach

這樣,當(dāng)用戶通過(guò)get請(qǐng)求index頁(yè)面時(shí),學(xué)生數(shù)據(jù)就從數(shù)據(jù)庫(kù)中取出并展示到了頁(yè)面內(nèi)。

2、在blade中引入頁(yè)面資源文件

雖然視圖文件放在resources/views目錄下,但是blade文件編譯完成后將位于public目錄下,所以其中的目錄是相對(duì)于public而言的,頁(yè)面所需要的靜態(tài)資源應(yīng)該放在public目錄下并通過(guò)asset函數(shù)相對(duì)public路徑來(lái)引入。

laravel默認(rèn)提供了bootstrap與jquery,分別對(duì)應(yīng)于public/css/app.css與public/js/app.js文件,如果需要可以引入。

<!-- Bootstrap CSS 文件 -->
<link rel="stylesheet" href="{{ asset('./css/app.css')}}" rel="external nofollow" >
<!-- jQuery 文件 -->
<script src="{{ asset('./js/app.js')}}"></script>

3、laravel中實(shí)現(xiàn)分頁(yè)

在laravel中可以很便捷地實(shí)現(xiàn)分頁(yè)數(shù)據(jù)顯示,第一步是在controller中分頁(yè)取出數(shù)據(jù)庫(kù)數(shù)據(jù)并傳遞給頁(yè)面:

return view('student.index',['students'=>Student::paginate(5)]);

第二部在頁(yè)面內(nèi)渲染分頁(yè)標(biāo)簽:

<ul class="pagination pull-right">
  {{$students->render()}}
</ul>

4、表單驗(yàn)證

laravel提供了validate方法來(lái)用于驗(yàn)證用戶提交的表單是否符合要求,例如在頁(yè)面通過(guò)post提交了學(xué)生表單form后,在controller中對(duì)其先進(jìn)行驗(yàn)證,如果正確則存入數(shù)據(jù)庫(kù),否則返回到上一頁(yè)面并拋出一個(gè)異常$errors,在頁(yè)面中顯示錯(cuò)誤$errors中的信息

//表單驗(yàn)證
$request->validate([
  'Student.name'=>'required|max:10',
  'Student.age'=>'required|integer',
  'Student.sex'=>'required',
],[
  'required'=>':attribute為必填項(xiàng)',
  'max'=>':attribut長(zhǎng)度過(guò)長(zhǎng)',
  'integer'=>':attribute必須為一個(gè)整數(shù)'
],[
  'Student.name'=>'姓名',
  'Student.age'=>'年齡',
  'Student.sex'=>'性別'
]);
//存入學(xué)生數(shù)據(jù)
$stu=$request->input('Student');
Student::create($stu);

validate()中第一個(gè)數(shù)組中定義字段的驗(yàn)證規(guī)則,其中Student.name是在提交的表單中定義的name

input type="text" name="Student[name]" placeholder="請(qǐng)輸入學(xué)生姓名">

required是你所需要的驗(yàn)證規(guī)則,中間用"|"隔開(kāi),詳細(xì)的規(guī)則可以看文檔

validate()第二個(gè)數(shù)組自定義驗(yàn)證出錯(cuò)后的提示信息,":attribute"為占位符

validate()第三個(gè)數(shù)組自定義每個(gè)字段的提示名字

在頁(yè)面中報(bào)錯(cuò)如下:

使用laravel框架怎么實(shí)現(xiàn)表單操作

可以通過(guò)$errors->all()獲取所有錯(cuò)誤后循環(huán)顯示出來(lái)

@if(count($errors))
  <div class="alert alert-danger">
    <ul>
      @foreach($errors->all() as $error)
        <li>{{$error}}</li>
        @endforeach
    </ul>
  </div>
  @endif

也可以$errors->first()獲取指定字段的驗(yàn)證錯(cuò)誤,顯示在每個(gè)輸入框之后

<p class="form-control-static text-danger">{{$errors->first('Student.name')}}</p>

當(dāng)驗(yàn)證失敗返回到表單頁(yè)面后,用戶原來(lái)的輸入信息會(huì)消失,這樣需要再填一遍,可以通過(guò)old方法顯示用戶原來(lái)的輸入

<input type="text" name="Student[name]" value="{{old('Student')['name']}}" >

5、錯(cuò)誤記錄

①、 MethodNotAllowedHttpException No message

這個(gè)錯(cuò)誤是因?yàn)槲野驯韱蔚膒ost請(qǐng)求發(fā)送到了Route::get()定義的路由上,它不會(huì)處理post請(qǐng)求,可以把路由通過(guò)Route::Match(['get','post'],)來(lái)定義

②、Action App\Http\Controllers\StudentController@delete not defined

這個(gè)錯(cuò)誤發(fā)生在我將在blade頁(yè)面請(qǐng)求跳轉(zhuǎn)到一個(gè)action,無(wú)法找到該Controller

<a href="{{action('StudentController@delete',['id'=>$student->id])}}" rel="external nofollow" >刪除</a>

但當(dāng)我在routes/web.php下注冊(cè)了該方法后報(bào)錯(cuò)消失

Route::get('delete/{id}','StudentController@delete');

③、The page has expired due to inactivity. Please refresh and try again.

這是由于laravel自動(dòng)設(shè)置了防止CSRF跨域攻擊,你需要在表單內(nèi)添加csrf_filed()來(lái)告訴laravel請(qǐng)求的發(fā)起人與表單提交者是同一個(gè)人。

<form class="form-horizontal" method="post" action="{{url('student/create')}}">
  {{ csrf_field() }}

上述內(nèi)容就是使用laravel框架怎么實(shí)現(xiàn)表單操作,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI