├── README.md ├── resources └── views │ ├── news │ ├── show.blade.php │ ├── index.blade.php │ ├── create.blade.php │ └── edit.blade.php │ └── layouts │ └── master.blade.php ├── database ├── seeds │ └── NewsTableSeeder.php └── migrations │ └── 2016_08_26_114034_create_news_table.php └── app ├── Http └── routes.php ├── News.php └── Controllers └── NewsController.php /README.md: -------------------------------------------------------------------------------- 1 | Simple CRUD (Add, Edit, Delete, View) in Laravel 2 | ======== 3 | 4 | A simple and basic system to add, edit, delete and view using Laravel PHP Framework. 5 | 6 | Blog Article: [Laravel: Simple CRUD (Add, Edit, Delete, View)](http://blog.chapagain.com.np/laravel-simple-crud-add-edit-delete-view-beginner-tutorial/) 7 | -------------------------------------------------------------------------------- /resources/views/news/show.blade.php: -------------------------------------------------------------------------------- 1 | 2 | @extends('layouts.master') 3 | 4 | @section('title', $news->title) 5 | 6 | @section('sidebar') 7 | @parent 8 | // you can add something here 9 | @endsection 10 | 11 | @section('content') 12 |

{{ $news->title }}

13 | 14 | Category: {{ $news->category }} | Author: {{ $news->author }} | Published on: {{ $news->created_at }} 15 | 16 |

{{ $news->full_content }}

17 | 18 | @endsection 19 | -------------------------------------------------------------------------------- /database/seeds/NewsTableSeeder.php: -------------------------------------------------------------------------------- 1 | insert([ 19 | 'title' => $faker->sentence(), 20 | 'slug' => $faker->slug, 21 | 'short_description' => $faker->paragraph(), 22 | 'full_content' => $faker->paragraphs(3, true), // 3 paragraphs 23 | 'author' => $faker->name, 24 | 'category' => $faker->colorName, // color name as category 25 | 'created_at' => $faker->dateTime(), 26 | 'updated_at' => $faker->dateTime(), 27 | ]); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /resources/views/layouts/master.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | @yield('title') 4 | 5 | 6 | @section('header') 7 | Home | Add News 8 | @show 9 | 10 | @if(Session::has('flash_message')) 11 |
12 | {{ Session::get('flash_message') }} 13 |
14 | @endif 15 | 16 | @if($errors->any()) 17 |
18 | @foreach($errors->all() as $error) 19 |

{{ $error }}

20 | @endforeach 21 |
22 | @endif 23 | 24 |
25 | @yield('content') 26 |
27 | 28 |
29 | Footer @ 2016 30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /app/Http/routes.php: -------------------------------------------------------------------------------- 1 | 'news.index', 'uses' => 'NewsController@index')); 16 | Route::get('news/add', array('as' => 'news.create', 'uses' => 'NewsController@create')); 17 | Route::post('news/store', array('as' => 'news.store', 'uses' => 'NewsController@store')); 18 | Route::get('news/edit/{id}', array('as' => 'news.edit', 'uses' => 'NewsController@edit')); 19 | Route::patch('news/update/{id}', array('as' => 'news.update', 'uses' => 'NewsController@update')); 20 | Route::delete('news/delete/{id}', array('as' => 'news.destroy', 'uses' => 'NewsController@destroy')); 21 | Route::get('news/{slug}', array('as' => 'news.show', 'uses' => 'NewsController@show')); 22 | -------------------------------------------------------------------------------- /database/migrations/2016_08_26_114034_create_news_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('title',255); 19 | $table->string('slug',100)->unique(); 20 | $table->text('short_description'); 21 | $table->text('full_content'); 22 | $table->string('author',100); 23 | $table->string('category',100); 24 | $table->timestamps(); // timestamps() creates created_at & updated_at fields 25 | }); 26 | } 27 | 28 | /** 29 | * Reverse the migrations. 30 | * 31 | * @return void 32 | */ 33 | public function down() 34 | { 35 | // drop table 'news' 36 | Schema::drop('news'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/News.php: -------------------------------------------------------------------------------- 1 | News 13 | 14 | @foreach ($news as $data) 15 |
16 |

17 | {{ $data->title }} 18 |
19 | 20 |

21 | 22 | Category: {{ $data->category }} | Author: {{ $data->author }} | Published on: {{ $data->created_at }} | 23 | Edit   24 | 25 | 26 | 27 | {!! Form::open(array( 28 | 'method' => 'DELETE', 29 | 'route' => ['news.destroy', $data->id], 30 | 'onsubmit' => "return confirm('Are you sure you want to delete?')", 31 | )) 32 | !!} 33 | {!! Form::submit('Delete') !!} 34 | {!! Form::close() !!} 35 | 36 |
37 |

38 | 39 |

{{ $data->short_description }}

40 |
41 | @endforeach 42 | 43 | 44 | 48 |
{{ $news->links() }}
49 | 50 | 51 | @endsection 52 | -------------------------------------------------------------------------------- /resources/views/news/create.blade.php: -------------------------------------------------------------------------------- 1 | 2 | @extends('layouts.master') 3 | 4 | @section('title', $title) 5 | 6 | @section('sidebar') 7 | @parent 8 | // you can add something here 9 | @endsection 10 | 11 | @section('content') 12 | 13 |

{{ $title }}

14 | 15 | {!! Form::open([ 16 | 'route' => 'news.store' 17 | ]) !!} 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
{!! Form::label('title', 'Title', ['class' => 'control-label']) !!}{!! Form::text('title', null, ['class' => 'form-control', 'size' => 64, ]) !!}
{!! Form::label('slug', 'Slug', ['class' => 'control-label']) !!}{!! Form::text('slug', null, ['class' => 'form-control', 'size' => 64, ]) !!}
{!! Form::label('category', 'Category', ['class' => 'control-label']) !!}{!! Form::select('category', array('Politics' => 'Politics', 'Sports' => 'Sports', 'International' => 'International'), null, ['placeholder' => 'Select Category']) !!}
{!! Form::label('author', 'Author', ['class' => 'control-label']) !!}{!! Form::select('author', array('John' => 'John', 'Tom' => 'Tom', 'Jack' => 'Jack'), null, ['placeholder' => 'Select Author']) !!}
{!! Form::label('short_description', 'Short Description', ['class' => 'control-label']) !!}{!! Form::textarea('short_description', null, ['class' => 'form-control']) !!}
{!! Form::label('full_content', 'Full Content', ['class' => 'control-label']) !!}{!! Form::textarea('full_content', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'btn btn-submit']) !!}
49 | 50 | {!! Form::close() !!} 51 | 52 | @endsection 53 | -------------------------------------------------------------------------------- /resources/views/news/edit.blade.php: -------------------------------------------------------------------------------- 1 | 2 | @extends('layouts.master') 3 | 4 | @section('title', $title) 5 | 6 | @section('sidebar') 7 | @parent 8 | // you can add something here 9 | @endsection 10 | 11 | @section('content') 12 | 13 |

{{ $title }}

14 | 15 | {!! Form::model($news, [ 16 | 'method' => 'PATCH', 17 | 'route' => ['news.update', $news->id] 18 | ]) !!} 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
{!! Form::label('title', 'Title', ['class' => 'control-label']) !!}{!! Form::text('title', null, ['class' => 'form-control', 'size' => 64, ]) !!}
{!! Form::label('slug', 'Slug', ['class' => 'control-label']) !!}{!! Form::text('slug', null, ['class' => 'form-control', 'size' => 64, ]) !!}
{!! Form::label('category', 'Category', ['class' => 'control-label']) !!}{!! Form::select('category', array('Politics' => 'Politics', 'Sports' => 'Sports', 'International' => 'International'), null, ['placeholder' => 'Select Category']) !!}
{!! Form::label('author', 'Author', ['class' => 'control-label']) !!}{!! Form::select('author', array('John' => 'John', 'Tom' => 'Tom', 'Jack' => 'Jack'), null, ['placeholder' => 'Select Author']) !!}
{!! Form::label('short_description', 'Short Description', ['class' => 'control-label']) !!}{!! Form::textarea('short_description', null, ['class' => 'form-control']) !!}
{!! Form::label('full_content', 'Full Content', ['class' => 'control-label']) !!}{!! Form::textarea('full_content', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'btn btn-submit']) !!}
50 | 51 | {!! Form::close() !!} 52 | 53 | @endsection 54 | -------------------------------------------------------------------------------- /app/Controllers/NewsController.php: -------------------------------------------------------------------------------- 1 | paginate($itemsPerPage); 27 | 28 | return view('news.index', array('news' => $news, 'title' => 'News Display')); 29 | } 30 | 31 | /** 32 | * Show the form for creating a new resource. 33 | * 34 | * @return \Illuminate\Http\Response 35 | */ 36 | public function create() 37 | { 38 | return view('news.create', array('title' => 'Add News')); 39 | } 40 | 41 | /** 42 | * Store a newly created resource in storage. 43 | * 44 | * @param \Illuminate\Http\Request $request 45 | * @return \Illuminate\Http\Response 46 | */ 47 | public function store(Request $request) 48 | { 49 | $this->validate($request, array( 50 | 'title' => 'required', 51 | 'slug' => 'required', 52 | 'short_description' => 'required', 53 | 'full_content' => 'required', 54 | ) 55 | ); 56 | 57 | $input = $request->all(); 58 | //dd($input); // dd() helper function is print_r alternative 59 | 60 | News::create($input); 61 | 62 | Session::flash('flash_message', 'News added successfully!'); 63 | 64 | //return redirect()->back(); 65 | //return redirect('news'); 66 | return redirect()->route('news.index'); 67 | } 68 | 69 | /** 70 | * Display the specified resource. 71 | * 72 | * @param string $slug 73 | * @return \Illuminate\Http\Response 74 | */ 75 | public function show($slug) 76 | { 77 | $news = News::where('slug', $slug)->first(); 78 | return view('news.show', array('news' => $news)); 79 | } 80 | 81 | /** 82 | * Show the form for editing the specified resource. 83 | * 84 | * @param int $id 85 | * @return \Illuminate\Http\Response 86 | */ 87 | public function edit($id) 88 | { 89 | $news = News::findOrFail($id); 90 | 91 | return view('news.edit', array('news' => $news, 'title' => 'Edit News')); 92 | } 93 | 94 | /** 95 | * Update the specified resource in storage. 96 | * 97 | * @param \Illuminate\Http\Request $request 98 | * @param int $id 99 | * @return \Illuminate\Http\Response 100 | */ 101 | public function update(Request $request, $id) 102 | { 103 | $news = News::findOrFail($id); 104 | 105 | $this->validate($request, array( 106 | 'title' => 'required', 107 | 'slug' => 'required', 108 | 'short_description' => 'required', 109 | 'full_content' => 'required', 110 | ) 111 | ); 112 | 113 | $input = $request->all(); 114 | 115 | $news->fill($input)->save(); 116 | 117 | Session::flash('flash_message', 'News updated successfully!'); 118 | 119 | return redirect()->back(); 120 | } 121 | 122 | /** 123 | * Remove the specified resource from storage. 124 | * 125 | * @param int $id 126 | * @return \Illuminate\Http\Response 127 | */ 128 | public function destroy($id) 129 | { 130 | $news = News::findOrFail($id); 131 | 132 | $news->delete(); 133 | 134 | Session::flash('flash_message', 'News deleted successfully!'); 135 | 136 | return redirect()->route('news.index'); 137 | } 138 | } 139 | --------------------------------------------------------------------------------