We are going to create a new Fresh Project "Myapp" Enter this Command on CMD. After entering this is Command It takes some time depends on your internet speed. After creating a new Laravel Project Now move to the first Step. We Need Model, Migration, and Controller to Start CRUD operation in Laravel 5.6. First, go to the Project path and enter Following Command in CMD. After that its generate 3 new files. app-> Blog.php. database->migrations-> create_blogs_table.php app->http->controller-> BlogController.php. Open create_blogs_table.php and add some column title and body as follows. Before the migration, you should connect with your database. so open your .env file and add database, username and password field. (In my case My database name is "newblog" username is "root" and password is blank ) Now open CMD and enter a Command to migrate. if you got some error like follows If not go to Step 3 Edit your app->providers->AppServiceProvider.php file Create a View Page For insert into the database. After adding this is code may be Show Error "Class 'form' not found" so go to this link Now Open BlogController.php and Edit Now Test it, Hope It Will Work. All a button to show all list of the blog on the view page. Now Add Some code on "BlogController.php" to fetch all record of Blog. Create a new page in View Folder "View.blade.php" and add some code to display data in the table format. See the Result: Add A Edit Button on "view.blade.php" Page. First Create an Edit View to Edit Our blog post "edit.blade.php". Now add some code to fetch Data in View page BlogController.php. And Add Some code to update data when you Click on "Update Post" button. Now Add Some to delete Selected Blog post from the database. Hope this post helps you if you have any dought ask in comment box. Thanks.composer create-project laravel/laravel Myapp
Insert Data Into database in laravel 5.6
Step 1: Create Model, Controller, and Migration
php artisan make:model Blog -mcr
Step 2: Create a Database Table
public function up()
{
Schema::create('blogs', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->timestamps();
});
}
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=newblog
DB_USERNAME=root
DB_PASSWORD=
php artisan migrate
In Connection.php line 647:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQ
L: alter table `users` add unique `users_email_unique`(`email`))
In Connection.php line 449:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
Step 3: Create a form
{!! Form::open(array('route' => 'blog.store', 'method'=>'post')) !!}
<div class="container">
<div class="row">
<div class="col-md-12">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Blog Body</label>
<textarea class="form-control" name="body"rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{!! Form::close() !!}
</div>
</div>
</div>
public function store(Request $request)
{
$this->validate($request,array(
'title' => 'required|max:255',
'body' =>'required',
));
$blog = new Blog;
$blog->title = $request->title;
$blog->body=$request->body;
$blog->save();
}
Read data from Database
Step 1
<a href="{!! route('blog.index') !!}" class="btn btn-success">All Blog Post</a>
public function index()
{
$blog= Blog::all();
return view('view')->withBlog($blog);
}
<div class="container">
<div class="col-md-2"></div>
<div class="col-md-8">
<table class="table">
<thead>
<tr>
<th>title</th>
<th>Blog</th>
</tr>
</thead>
<tbody>
@foreach ($blog as $key => $value)
<tr>
<td>{{$value->title}}</td>
<td>{{$value->body}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="col-md-2"></div>
</div>
Edit and Updates data
in laravel 5.6
Step 1: Create an Edit view
<a href="{{route('blog.edit', $value->id) }} " class="btn btn-sm btn-warning">Edit</a>
<div class="container">
<div class="row">
<div class="col-md-12">
{!! Form::model($blog,['route'=>['blog.update',$blog->id],'method'=>'PUT']) !!}
{{Form::label('title','Title:')}}
{{Form::text('title',null,["class"=> 'form-control input-lg'])}}
{{Form::label('body','Body:')}}
{{Form::textarea('body',null,["class"=> 'form-control input-lg'])}}
{!! Form::submit( 'Publish', ['class' => 'btn btn-success','name' => 'publish'])!!}
{!! Form::close() !!}
<hr>
</div>
</div>
</div>
public function edit($blog)
{
$blog = Blog::find($blog);
return view('edit')->withBlog($blog);
}
public function update(Request $request, $blog)
{
$this->validate($request,array(
'title' => 'required|max:255',
'body' =>'required',
));
$blog = Blog::find($blog);
$blog->title = $request->title;
$blog->body=$request->body;
$blog->save();
return view('/welcome');
}
Delete data in laravel 5.6
Step 1: Add a Delete Button in View.blade.php page
{!! Form::open(['route'=>['blog.destroy', $value->id],'method'=>'DELETE'] ) !!}
{!! Form::submit('Delete',['class'=>'btn btn-danger btn-sm']) !!}
{!!Form::close() !!}
public function destroy( $blog)
{
$blog = Blog::find($blog);
$blog->delete();
return view('/welcome');
}
About the author
Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...
You must be logged in to post a comment.