لاراول و crud-laravel

crud-laravel

در مطلب ” آشنایی با MVC در laravel” با نحوه ی ایجاد مدل و کنترلر آشنا شدیم
در این قسمت می خواهیم عملیات Create- Read-Update-Delete که به اختصار CRUD نوشته می شود , جزء پرکاربردترین عملیات در فریم ورک می باشد را آموزش دهیم.
خوب مدل article ما به صورت زیر می باشد که دارای ۴ فیلد meta_description,title, content ,meta_keyword

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = "article"; // مشخص کردن نام جدول این مدل
/*     * ************************** */
protected $fillable = ['title', 'content','meta_keyword','meta_description'];
}

در کنترلر articleController نیز :

<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;

class ArticleController extends Controller {
//
//
//
}

مرحله به مرحله هر بخش را تکمیل میکنیم.

مرحله اول در crud-laravel :

ایتدا اکشن index که همان بخش Read می باشد،را در کنترلر اضافه می کنیم.
در کنترلر :

<!-- /app/http/controllers/ArticleController.php -->
public function index() {
$model = Article::all();
//  return View::make('article.index')
//       ->with('model', $model);
return view('article.index', [
'model' => $model,
]);
}

و در ویو :

<!-- /resources/views/article/index.blade.php -->
<div class="col-lg-12">
@if(Session::has('success_msg'))
<div class="alert alert-success">{{ Session::get('success_msg') }}</div>
@endif
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>ID</td>
<td>Title</td>
<td>Content</td>
<td>meta_keyword</td>
<td>meta_description</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
@foreach($model as $key => $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->title }}</td>
<td>{{ $value->content }}</td>
<td>{{ $value->meta_keyword }}</td>
<td>{{ $value->meta_description }}</td>
<!-- we will also add show, edit, and delete buttons -->
<td>
<a class="btn btn-small btn-success"
href="<?php echo url("/article/show/{$value->id}"); ?>">
Show
</a>
<a class="btn btn-small btn-info"
href="<?php echo url("/article/edit/{$value->id}"); ?>">
Edit
</a>
<a class="btn btn-small btn-warning"
href="<?php echo url("/article/delete/{$value->id}"); ?>">
Delete
</a>
</td>
</tr>
@endforeach
</tbody>
</table>

و در routes:

<!-- /routes/web.php -->
Route::get('/article/index', 'ArticleController@index');

نتیجه :
CRUD in Laravel

مرحله دوم در crud-laravel :

اکشن create
به کنترلر خود متد زیر را اضافه می کنیم:

<!-- /app/http/controllers/ArticleController.php -->
public function create() {
$model = new Article;
return view('article.create', [
'model' => $model,
]);
}

با استفاده از کد بالا با فراخوانی اکشن یک نمونه از مدل ما ایجاد و به ویو ارسال می شود .

قبل از نمایش کدها در ویو تنظیمات route برای این اکشن را تنظیم می کنیم؛

<!-- /routes/web.php -->
Route::get('/article/create/', 'ArticleController@create');

نوبت به نمایش فورم در ویوی اکشن create می باشد :

با توجه به تنظیمات ما در ایجاد قالب , فورم من به صورت زیر می باشد :

@extends('layouts.main')
@section('title','Create')
@section('content')
@section('breadcrumb')
<li><a href="{{ url('/') }}" target="_blank">نمایش سایت</a></li>
<li><a href="{{ url('article/index') }}">مقالات</a></li>
@stop
@section('side')
@parent
<li class="list-group-item"><a href="{{ url('article/index') }}">بازگشت به مدیریت</a></li>
@endsection

{{ Form::open(array('url' => "article/store")) }}
{{ csrf_field() }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title',  $model->title , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('content', 'Content') }}
{{ Form::textarea('content',   $model->content , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('meta_keyword', 'SEO KEYWORD') }}
{{ Form::text('meta_keyword',  $model->meta_keyword , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('meta_description', 'SEO Description') }}
{{ Form::textarea('meta_description',  $model->meta_description , array('class' => 'form-control')) }}
</div>

{{ Form::submit('Update', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

@stop

در کد بالا در فورم ما مشخص کردیم که پس از ارسال فورم اطلاعات به اکشن store در کنترلر article ارسال می شوند :

{{ Form::open(array('url' => "article/store")) }}

پس از مراحل بالا شما باید تصویر زیر را مشاهده کنید :

crud-لاراول-laravel

مرحله سوم در crud-laravel :

ذخیره اطلاعات ارسالی در اکشن store

مطابق مراحل قبل در کنترلر اکشن store را با مشخصات زیر اضافه می کنیم:

public function store(Request $request) {
//validate post data
$this->validate($request, [
'title' => 'required',
'content' => 'required'
]);
//get post data
$articleData = $request->all();
//insert post data
Article::create($articleData);
//store status message
Session::flash('success_msg', 'Post added successfully!');
return redirect()->action('ArticleController@index');
}

در اکشن store آرگومانی با نام $request داریم که اطلاعات ارسالی را با استفاده از آن دریافت می کنیم.
در کد بالا با استفاده از validate مشخص کردیم که چه فیلد هایی اجباری است تا در صورت خالی بودن به کاربر پیام هشدار بدهد.
به وسیله ی $request->all() مقادیر را دریافت کردیم:
با استفاده از Article::create($articleData); فریم ورک اطلاعات ارسالی را مقدار دهی می کند و در دیتابیس ثبت می کند .
نکته : اطلاعاتی را مقدار دهی می کند که در مدل و در پروپرتی fillable تنظیم کرده باشیم:

protected $fillable = ['title', 'content','meta_keyword','meta_description'];

و در خط آخر مجدد اکشن index را صدا میزنیم.

قبل از اجرای این کد به یاد داشته باشید که در route باید مشخص کنید اکشن را و همچنین نحوه ی ارسال اطلاعات را

Route::post('/article/store', 'ArticleController@store');

ما در route از post استفاده کردیم یعنی اطلاعات به اکشن store باید post شده باشد.
در صورتی که مقادیر را از طریق متد get ارسال کرده باشید . سیستم به شما خطا می دهد.

مرحله چهارم در crud-laravel :

نمایش یک رکورد
در این بخش می خواهیم اطلاعات یک رکورد از دیتابیس را نمایش بدهیم
برای این کار اکشن show را به کنترلر خود اضافه می کنیم:

<!-- /app/http/controllers/ArticleController.php -->
public function show($id) {
$model = Article::find($id);
return view('article.show', [
'model' => $model,
]);
}

در کد بالا اکشن ما یک آرگومان با نام id و به صورت get دریافت می کند.
با استفاده از متد find و آرگومان دریافتی رکورد مورد نظر را در مدل پیدا می کنیم
با استفاده از return view فایل show.blade.php در فولدر article را با ارسال دیتای استخراجیمون صدا می زنیم.

در قسمت ویو با استفاده از کد زیر اطلاعات را نمایش می دهیم

<!-- /resources/views/article/show.blade.php -->
@extends('layouts.main')

@section('title','View')

@section('content')

@section('breadcrumb')
<li><a href="{{ url('/') }}" target="_blank">نمایش سایت</a></li>
<li><a href="{{ url('article/index') }}">مقالات</a></li>
@stop
@section('side')
@parent
<li class="list-group-item"><a href="{{ url('article/index') }}">بازگشت به مدیریت</a></li>
@endsection

<table class="table table-striped table-bordered">
<tr>
<td>ID</td>
<td>{{ $model->id }}</td>
</tr>
<tr>
<td>Title</td>
<td>{{ $model->title }}</td>
</tr>
<tr>
<td>Content</td>
<td>{{ $model->content }}</td>
</tr>
<tr>
<td>Meta_keyword</td>
<td>{{ $model->meta_keyword }}</td>
</tr>
<tr>
<td>meta_description</td>
<td>{{ $model->meta_description }}</td>
</tr>
<tr>
<td>Image</td>
<td>{{ $model->image }}</td>
</tr>
<tr>
<td>Publish_date</td>
<td>{{ $model->publish_date }}</td>
</tr>

</table>

@stop

همچنین فراموش نکنیم که باید کد مربوط به show را در قسمت route تنظیم نماییم :

<!-- /routes/web.php -->
Route::get('/article/show/{id}', 'ArticleController@show');

در کد بالا با استفاده از {id} مشخص می کنیم که باید آرگومان id دریافت شود . با چه متدی؟Route::get . بله با متد GET
اگر ویوی index را مشاهده کنید. متوجه می شود که ما با استفاده از کد زیر : نحوه ی قرار دادن لینک به اکشن show را نشان داده ایم:

<a class="btn btn-small btn-success" href="<?php echo url("/article/show/{$value->id}"); ?>">Show</a>

مرحله پنجم در crud-laravel :

حذف یک رکورد
در این مرحله با اضافه کردن اکشن delete عملیات حذف یک رکورد را قرار می دهیم:

<!-- /app/http/controllers/ArticleController.php -->
public function delete($id) {
$model = Article::find($id);
$model->delete();
Session::flash('success_msg', 'Post deleted successfully!');
return redirect()->action('ArticleController@index');
}

در کد بالا همانند اکشن show با استفاده از یک پارامتر رکورد مربوطه را پیدا می کنیم
سپس با استفاده از متد delete رکورد یافت شده را حذف می کنیم
و مجددا اکشن index که همان صفحه مدیریت مقاله های مان هست را اجرا می کنیم.

فرامشو نکنید که route را تنظیم کنید:

<!-- /routes/web.php -->
Route::get('/article/delete/{id}', 'ArticleController@delete');

لینک حذف یک رکورد در index.blade.php

<a class="btn btn-small btn-warning" href="<?php echo url("/article/delete/{$value->id}"); ?>">Delete</a>

مرحله ششم در crud-laravel :

به روز رسانی یا همان update :

به کنترلر خود اکشن edit را اضافه کنید:

<!-- /app/http/controllers/ArticleController.php -->
public function edit($id) {
$model = Article::find($id);
return view('article.edit', [
'model' => $model,
]);
}

در کد بالا اتفاق جدیدی نیافتاده است .
ما فقط اطلاعات یک رکورد از دیتابیس خود را خوانده و به ویو که دارای یک فورم می باشد ارسال می کنیم.
در

<!-- /resources/views/article/edit.blade.php -->

{{ Form::open(array('url' => "article/update/$model->id")) }}
{{ csrf_field() }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title',  $model->title , array('class' => 'form-control')) }}
</div>

<div class="form-group">
{{ Form::label('content', 'Content') }}
{{ Form::textarea('content',   $model->content , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('meta_keyword', 'SEO KEYWORD') }}
{{ Form::text('meta_keyword',  $model->meta_keyword , array('class' => 'form-control')) }}
</div>

<div class="form-group">
{{ Form::label('meta_description', 'SEO Description') }}
{{ Form::textarea('meta_description',  $model->meta_description , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('cat_id', 'Category') }}
{{ Form::select('cat_id', array('null' => 'Select a Level', '1' => 'Sees Sunlight', '2' => 'Foosball Fanatic', '3' => 'Basement Dweller'), Input::old('nerd_level'), array('class' => 'form-control')) }}
</div>

{{ Form::submit('Update', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

همانند ویوی create در اینجا نیز یک فورم داریم که اطلاعات خوانده شده را نشان می دهد و
در صورت کلیک توسط کاربر به اکشن update ارسال می شوند.

دوباره به کنترلر خود رفته و اکشن update را در آن قرار می دهیم:

<!-- /app/http/controllers/ArticleController.php -->
public function update(Request $request, $id) {
//validate post data
$this->validate($request, [
'title' => 'required',
'content' => 'required'
]);
//get post data
$articleData = $request->all();
$model = Article::find($id);
//update post data
$model->update($articleData);

//store status message
Session::flash('success_msg', 'Post updated successfully!');

return redirect()->action('ArticleController@index');
}

در اکشن فوق ما
ابتدا عملیات اعتبار سنجی را انجام میدیم
سپس با request->all اطلاعات را در articleData مقدار دهی می کنیم
پس از اینکه با id دریافتی مدل را فراخوانی کردیم با متد update اطلاعات را به روزرسانی می کنیم

توجه داشته باشید که تنظیمات route فراموش نشود

<!-- /routes/web.php -->
Route::post('/article/update/{id}', 'ArticleController@update');
Route::get('/article/edit/{id}', 'ArticleController@edit');

در نهایت کنترلر من به صورت زیر می باشد :

crud-laravel

در مطلب " <a href="http://www.goyii.ir/%D8%A2%D8%B4%D9%86%D8%A7%DB%8C%DB%8C-%D8%A8%D8%A7-mvc-%D8%AF%D8%B1-laravel/">آشنایی با MVC در laravel</a>" با نحوه ی ایجاد مدل و کنترلر آشنا شدیم
در این قسمت می خواهیم عملیات Create- Read-Update-Delete که به اختصار CRUD نوشته می شود , جزء پرکاربردترین عملیات در فریم ورک می باشد را آموزش دهیم.
خوب مدل article ما به صورت زیر می باشد که دارای ۴ فیلد meta_description,title, content ,meta_keyword


<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = "article"; // مشخص کردن نام جدول این مدل
/*     * ************************** */
protected $fillable = ['title', 'content','meta_keyword','meta_description'];
}

در کنترلر articleController نیز :

<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;

class ArticleController extends Controller {
//
//
//
}

مرحله به مرحله هر بخش را تکمیل میکنیم.

مرحله اول در crud-laravel :

ایتدا اکشن index که همان بخش Read می باشد،را در کنترلر اضافه می کنیم.
در کنترلر :

<!-- /app/http/controllers/ArticleController.php -->
public function index() {
$model = Article::all();
//  return View::make('article.index')
//       ->with('model', $model);
return view('article.index', [
'model' => $model,
]);
}

و در ویو :

<!-- /resources/views/article/index.blade.php -->
<div class="col-lg-12">
@if(Session::has('success_msg'))
<div class="alert alert-success">{{ Session::get('success_msg') }}</div>
@endif
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>ID</td>
<td>Title</td>
<td>Content</td>
<td>meta_keyword</td>
<td>meta_description</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
@foreach($model as $key => $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->title }}</td>
<td>{{ $value->content }}</td>
<td>{{ $value->meta_keyword }}</td>
<td>{{ $value->meta_description }}</td>
<!-- we will also add show, edit, and delete buttons -->
<td>
<a class="btn btn-small btn-success"
href="<?php echo url("/article/show/{$value->id}"); ?>">
Show
</a>
<a class="btn btn-small btn-info"
href="<?php echo url("/article/edit/{$value->id}"); ?>">
Edit
</a>
<a class="btn btn-small btn-warning"
href="<?php echo url("/article/delete/{$value->id}"); ?>">
Delete
</a>
</td>
</tr>
@endforeach
</tbody>
</table>

و در routes:

<!-- /routes/web.php -->
Route::get('/article/index', 'ArticleController@index');

نتیجه :
CRUD in Laravel

مرحله دوم در crud-laravel :

اکشن create
به کنترلر خود متد زیر را اضافه می کنیم:

<!-- /app/http/controllers/ArticleController.php -->
public function create() {
$model = new Article;
return view('article.create', [
'model' => $model,
]);
}

با استفاده از کد بالا با فراخوانی اکشن یک نمونه از مدل ما ایجاد و به ویو ارسال می شود .

قبل از نمایش کدها در ویو تنظیمات route برای این اکشن را تنظیم می کنیم؛

<!-- /routes/web.php -->
Route::get('/article/create/', 'ArticleController@create');

نوبت به نمایش فورم در ویوی اکشن create می باشد :

با توجه به تنظیمات ما در ایجاد قالب , فورم من به صورت زیر می باشد :

@extends('layouts.main')
@section('title','Create')
@section('content')
@section('breadcrumb')
<li><a href="{{ url('/') }}" target="_blank">نمایش سایت</a></li>
<li><a href="{{ url('article/index') }}">مقالات</a></li>
@stop
@section('side')
@parent
<li class="list-group-item"><a href="{{ url('article/index') }}">بازگشت به مدیریت</a></li>
@endsection

{{ Form::open(array('url' => "article/store")) }}
{{ csrf_field() }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title',  $model->title , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('content', 'Content') }}
{{ Form::textarea('content',   $model->content , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('meta_keyword', 'SEO KEYWORD') }}
{{ Form::text('meta_keyword',  $model->meta_keyword , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('meta_description', 'SEO Description') }}
{{ Form::textarea('meta_description',  $model->meta_description , array('class' => 'form-control')) }}
</div>

{{ Form::submit('Update', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

@stop

در کد بالا در فورم ما مشخص کردیم که پس از ارسال فورم اطلاعات به اکشن store در کنترلر article ارسال می شوند :

{{ Form::open(array('url' => "article/store")) }}

پس از مراحل بالا شما باید تصویر زیر را مشاهده کنید :

crud-لاراول-laravel

مرحله سوم در crud-laravel :

ذخیره اطلاعات ارسالی در اکشن store

مطابق مراحل قبل در کنترلر اکشن store را با مشخصات زیر اضافه می کنیم:

public function store(Request $request) {
//validate post data
$this->validate($request, [
'title' => 'required',
'content' => 'required'
]);
//get post data
$articleData = $request->all();
//insert post data
Article::create($articleData);
//store status message
Session::flash('success_msg', 'Post added successfully!');
return redirect()->action('ArticleController@index');
}

در اکشن store آرگومانی با نام $request داریم که اطلاعات ارسالی را با استفاده از آن دریافت می کنیم.
در کد بالا با استفاده از validate مشخص کردیم که چه فیلد هایی اجباری است تا در صورت خالی بودن به کاربر پیام هشدار بدهد.
به وسیله ی $request->all() مقادیر را دریافت کردیم:
با استفاده از Article::create($articleData); فریم ورک اطلاعات ارسالی را مقدار دهی می کند و در دیتابیس ثبت می کند .
نکته : اطلاعاتی را مقدار دهی می کند که در مدل و در پروپرتی fillable تنظیم کرده باشیم:

protected $fillable = ['title', 'content','meta_keyword','meta_description'];

و در خط آخر مجدد اکشن index را صدا میزنیم.

قبل از اجرای این کد به یاد داشته باشید که در route باید مشخص کنید اکشن را و همچنین نحوه ی ارسال اطلاعات را

Route::post('/article/store', 'ArticleController@store');

ما در route از post استفاده کردیم یعنی اطلاعات به اکشن store باید post شده باشد.
در صورتی که مقادیر را از طریق متد get ارسال کرده باشید . سیستم به شما خطا می دهد.

مرحله چهارم در crud-laravel :

نمایش یک رکورد
در این بخش می خواهیم اطلاعات یک رکورد از دیتابیس را نمایش بدهیم
برای این کار اکشن show را به کنترلر خود اضافه می کنیم:

<!-- /app/http/controllers/ArticleController.php -->
public function show($id) {
$model = Article::find($id);
return view('article.show', [
'model' => $model,
]);
}

در کد بالا اکشن ما یک آرگومان با نام id و به صورت get دریافت می کند.
با استفاده از متد find و آرگومان دریافتی رکورد مورد نظر را در مدل پیدا می کنیم
با استفاده از return view فایل show.blade.php در فولدر article را با ارسال دیتای استخراجیمون صدا می زنیم.

در قسمت ویو با استفاده از کد زیر اطلاعات را نمایش می دهیم

<!-- /resources/views/article/show.blade.php -->
@extends('layouts.main')

@section('title','View')

@section('content')

@section('breadcrumb')
<li><a href="{{ url('/') }}" target="_blank">نمایش سایت</a></li>
<li><a href="{{ url('article/index') }}">مقالات</a></li>
@stop
@section('side')
@parent
<li class="list-group-item"><a href="{{ url('article/index') }}">بازگشت به مدیریت</a></li>
@endsection

<table class="table table-striped table-bordered">
<tr>
<td>ID</td>
<td>{{ $model->id }}</td>
</tr>
<tr>
<td>Title</td>
<td>{{ $model->title }}</td>
</tr>
<tr>
<td>Content</td>
<td>{{ $model->content }}</td>
</tr>
<tr>
<td>Meta_keyword</td>
<td>{{ $model->meta_keyword }}</td>
</tr>
<tr>
<td>meta_description</td>
<td>{{ $model->meta_description }}</td>
</tr>
<tr>
<td>Image</td>
<td>{{ $model->image }}</td>
</tr>
<tr>
<td>Publish_date</td>
<td>{{ $model->publish_date }}</td>
</tr>

</table>

@stop

همچنین فراموش نکنیم که باید کد مربوط به show را در قسمت route تنظیم نماییم :

<!-- /routes/web.php -->
Route::get('/article/show/{id}', 'ArticleController@show');

در کد بالا با استفاده از {id} مشخص می کنیم که باید آرگومان id دریافت شود . با چه متدی؟Route::get . بله با متد GET
اگر ویوی index را مشاهده کنید. متوجه می شود که ما با استفاده از کد زیر : نحوه ی قرار دادن لینک به اکشن show را نشان داده ایم:

<a class="btn btn-small btn-success" href="<?php echo url("/article/show/{$value->id}"); ?>">Show</a>

مرحله پنجم در crud-laravel :

حذف یک رکورد
در این مرحله با اضافه کردن اکشن delete عملیات حذف یک رکورد را قرار می دهیم:

<!-- /app/http/controllers/ArticleController.php -->
public function delete($id) {
$model = Article::find($id);
$model->delete();
Session::flash('success_msg', 'Post deleted successfully!');
return redirect()->action('ArticleController@index');
}

در کد بالا همانند اکشن show با استفاده از یک پارامتر رکورد مربوطه را پیدا می کنیم
سپس با استفاده از متد delete رکورد یافت شده را حذف می کنیم
و مجددا اکشن index که همان صفحه مدیریت مقاله های مان هست را اجرا می کنیم.

فرامشو نکنید که route را تنظیم کنید:

<!-- /routes/web.php -->
Route::get('/article/delete/{id}', 'ArticleController@delete');

لینک حذف یک رکورد در index.blade.php

<a class="btn btn-small btn-warning" href="<?php echo url("/article/delete/{$value->id}"); ?>">Delete</a>

مرحله ششم در crud-laravel :

به روز رسانی یا همان update :

به کنترلر خود اکشن edit را اضافه کنید:

<!-- /app/http/controllers/ArticleController.php -->
public function edit($id) {
$model = Article::find($id);
return view('article.edit', [
'model' => $model,
]);
}

در کد بالا اتفاق جدیدی نیافتاده است .
ما فقط اطلاعات یک رکورد از دیتابیس خود را خوانده و به ویو که دارای یک فورم می باشد ارسال می کنیم.
در

<!-- /resources/views/article/edit.blade.php -->

{{ Form::open(array('url' => "article/update/$model->id")) }}
{{ csrf_field() }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title',  $model->title , array('class' => 'form-control')) }}
</div>

<div class="form-group">
{{ Form::label('content', 'Content') }}
{{ Form::textarea('content',   $model->content , array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('meta_keyword', 'SEO KEYWORD') }}
{{ Form::text('meta_keyword',  $model->meta_keyword , array('class' => 'form-control')) }}
</div>

<div class="form-group">
{{ Form::label('meta_description', 'SEO Description') }}
{{ Form::textarea('meta_description',  $model->meta_description , array('class' => 'form-control')) }}
</div>

{{ Form::submit('Update', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

همانند ویوی create در اینجا نیز یک فورم داریم
که اطلاعات خوانده شده را نشان می دهد و
در صورت کلیک توسط کاربر به اکشن update ارسال می شوند.

دوباره به کنترلر خود رفته و اکشن update را در آن قرار می دهیم:

<!-- /app/http/controllers/ArticleController.php -->
public function update(Request $request, $id) {
//validate post data
$this->validate($request, [
'title' => 'required',
'content' => 'required'
]);
//get post data
$articleData = $request->all();
$model = Article::find($id);
//update post data
$model->update($articleData);

//store status message
Session::flash('success_msg', 'Post updated successfully!');

return redirect()->action('ArticleController@index');
}

در اکشن فوق ما
ابتدا عملیات اعتبار سنجی را انجام میدیم
سپس با request->all اطلاعات را در articleData مقدار دهی می کنیم
پس از اینکه با id دریافتی مدل را فراخوانی کردیم
با متد update اطلاعات را به روزرسانی می کنیم

توجه داشته باشید که تنظیمات route فراموش نشود

<!-- /routes/web.php -->
Route::post('/article/update/{id}', 'ArticleController@update');
Route::get('/article/edit/{id}', 'ArticleController@edit');

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *