BTemplates.com

Advertisement

YoGiTeCh....... Powered by Blogger.

About Me

My photo
Mumbai, Maharastra, India

How to login with twitter in laravel 5.4

In this Tutorial will show you how to user login with twitter using laravel/socialite packages. social authentication is very importan...

Contact Form

Name

Email *

Message *

Search This Blog

Subscribe

Translate

Recent Posts

Posts

Pages

Blogger templates

Popular Posts

Tuesday, 28 March 2017

How to create CRUD(create, read, update, delete) operation in laravel



This tutorial will show you how to create CRUD (create, read, update, delete) operation in laravel 5.4. in this CRUD operation you can create post then if you want to edit particular post you can edit post and update it. suppose you don't want particular post you can also delete that post.

1. Create new Project:
we can create project using following command :
composer create-project --prefer-dist laravel/laravel Projectname
2. Create PostController :
now we should create new resource controller as PostController.
php artisan make:controller PostController --resource
app/Http/Controllers/PostController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use App\Post; class PostController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $search = \Request::get('search'); $posts = post::where('title','like','%'.$search.'%')->orderby('id')->paginate(5); return view('Posts.Index')->withPosts($posts); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('Posts.Create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate the data $this->validate($request, array( 'title' => 'required|max:255', 'body' => 'required' )); // store in the database $post = new Post; $post->title = $request->title; $post->body = $request->body; $post->save(); //Session::flash('success','The blog post was successfully save!'); return redirect()->route('posts.show' , $post->id); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $post = Post::find($id); return view('Posts.Show')->withPost($post); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $post = Post::find($id); return view('Posts.Edit')->withPost($post); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, array( 'title' => 'required|max:255', 'body' => 'required' )); // store in the database $post = Post::find($id); $post->title = $request->title; $post->body = $request->body; $post->save(); //Session::flash('success','The blog post was successfully save!'); return redirect()->route('posts.show' , $post->id); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $post = Post::find($id); $post->delete(); Session::flash('success','The Post was successfully deleted.'); return redirect()->route('posts.index'); } }
3. Create Post table and model :
Now we have to create one table which is Post table and one model so we can create table and model using following command :
php artisan make:model Post --migration
database/migration/CreatePostsTable

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
Now Go to app/Post
app/Post

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { // }
4. Add Route :
Route::resource('posts', 'PostController');
5. Create Layouts/app file :
Ok, now we have to create layouts directory and create app.blade.php file inside that folder.

resources/views/layouts/app.blade.php

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel CRUD operation</title> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"> </head> <body> <div class="container"> @yield('content') </div> </body> </html>
6. Create index.blade.php :
Ok, now we have to create posts directory and create index.blade.php file inside that folder.

resources/views/posts/index.blade.php

@extends('layouts.app') @section('title','| Show') @section('content') <style type="text/css"> .row { padding-top: 10px; } .icon { background: transparent; border: 0px; padding: 0; outline: 0; font-size: 20px; } .black { color: black; } .blue { color: blue; } .red { color: red; } .jumbotron { margin-bottom: 0px; background-color: skyblue; background-position: 0% 25%; background-size: cover; background-repeat: no-repeat; color: white; } .well{ margin-bottom: 0px; background-color: #ff7256; background-position: 0% 25%; background-size: cover; background-repeat: no-repeat; color: white; } </style> <script> $(document).ready(function(){ $('[data-toggle="tooltip"]').tooltip(); }); </script> <div class="row"> <div class="well"> <h2>My Posts <button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target="#myModal">Create New Post</button></h2> </div> <br> <div class="jumbotron"> <div class="col-md-offset-4 col-md-4 col-md-offset-4"> {!! Form::open(['method' => 'GET','url' =>'posts', 'class' =>'navbar-form navbar-left','role' =>'search']) !!} <div class="input-group custom-search-form"> <input type="text" name="search" class="form-control" placeholder="Search"> <span class="input-group-btn"> <button type="submit" class="btn btn-default"> <i class="glyphicon glyphicon-search icon"></i> </button> </span> </div> {!! Form::close() !!} </div> <table class="table table-bordered"> <tr> <th>#</th> <th>Title</th> <th>Body</th> <th>Created At</th> <th>Updated At</th> <th>Action</th> </tr> @foreach ($posts as $post) <tr> <td>{{ $post->id }}</td> <td>{{ $post->title }}</td> <td>{{ substr($post->body,0,50) }}{{ strlen($post->body) > 50 ?"..." : '' }}</td> <td>{{ $post->created_at }}</td> <td>{{ $post->updated_at }}</td> <td> {!! Form::open(['route' => ['posts.destroy',$post->id], 'method' => 'DELETE'])!!} <a href="{{ route('posts.edit', $post->id)}}" ><span class="glyphicon glyphicon-pencil black icon" data-toggle="tooltip" title="Edit Post"></span></a> &nbsp; &nbsp; <a href="{{ route('posts.show', $post->id)}}" ><span class="glyphicon glyphicon-eye-open icon blue" data-toggle="tooltip" title="View Post" data-placement="left"></span></a> &nbsp; &nbsp; <!-- <td align="right"><a href="{{ route('posts.destroy', $post->id)}}" class="btn btn-danger">Delete</a></td> --> {!! Form::button('<span class="glyphicon glyphicon-trash red " data-toggle="tooltip" title="Delete Post"></span>', ['type' => 'submit' ,'class' => 'icon' ]) !!} {!! Form::close() !!} </td> </tr> @endforeach </table> <div class="text-center"> {!! $posts->links(); !!} </div> </div> </div> @endsection <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title" style="text-align: center;">Create New Post</h4> </div> <div class="modal-body"> {!! Form::open(['route' => 'posts.store','data-parsley-validate' => '']) !!} {{ Form:: label('title','Title:')}} {{ Form::text('title',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: label('body','Body:')}} {{ Form:: textarea('body',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }} {!! Form::close() !!} </div> </div> </div> </div>
create second file inside posts folder

7. Create create.blade.php :
resources/views/posts/create.blade.php

@extends('layouts.app') @section('title','| Create New Post') <!-- @section('stylesheets') {!! Html::style('css/parsley.css')!!} @endsection --> @section('content') <style type="text/css"> .row { padding-top: 50px; } </style> <body style="background-image: url(bg.jpg);"> <div class="row"> <div class="col-md-offset-3 col-md-6"> <h1 style="text-align: center;">Create New Post</h1> <hr> {!! Form::open(['route' => 'posts.store','data-parsley-validate' => '']) !!} {{ Form:: label('title','Title:')}} {{ Form::text('title',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: label('body','Body:')}} {{ Form:: textarea('body',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }} {!! Form::close() !!} </div> </div><br> </body> @endsection <!-- @section('scripts') {!! Html::script('js/parsley.min.js')!!} @endsection -->

8. Create edit.blade.php :
resources/views/posts/edit.blade.php

@extends('layouts.app') @section('title','| Create New Post') @section('stylesheets') {!! Html::style('css/parsley.css')!!} @endsection @section('content') <style type="text/css"> .row { padding-top: 50px; } </style> <div class="row"> <div class="col-md-offset-3 col-md-6"> <h1 style="text-align: center;">Update Post</h1> <hr> {!! Form::model($post,['route' => ['posts.update',$post->id], 'data-parsley-validate' => '', 'method' => 'put']) !!} {{ Form:: label('title','Title:')}} {{ Form::text('title',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: label('body','Body:')}} {{ Form:: textarea('body',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: submit('Update Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }} {!! Form::close() !!} </div> </div><br> @endsection @section('scripts') {!! Html::script('js/parsley.min.js')!!} @endsection

9. Create show.blade.php :
resources/views/posts/show.blade.php

@extends('layouts.app') @section('title','| View Post') @section('content') <style type="text/css"> .row { padding-top: 50px; } </style> <div class="row"> <div class="jumbotron"> <a href="{{ route('posts.index')}}" class="btn btn-default">Back</a> <h1>{{ $post->title }}</h1> <p class="load" style="text-align: justify;">{{ $post->body }}</p> <table class="table"> <tr> <td>Created At:{{ $post->created_at }}</td> <td align="right">Updated At:{{ $post->updated_at }}</td> </tr> <tr> <td><a href="{{ route('posts.edit', $post->id)}}" class="btn btn-primary">Edit</a></td> <td align="right"> {!! Form::open(['route' => ['posts.destroy',$post->id], 'method' => 'DELETE'])!!} <!-- <td align="right"><a href="{{ route('posts.destroy', $post->id)}}" class="btn btn-danger">Delete</a></td> --> {!! Form::submit('Delete', ['class' => 'btn btn-danger ' ]) !!} {!! Form::close() !!} </td> </tr> </table> </div> </div> @endsection
Now you can run your project

0 comments:

Post a Comment