{"id":272,"date":"2023-07-26T17:44:43","date_gmt":"2023-07-26T17:44:43","guid":{"rendered":"https:\/\/semicollar.com\/?p=272"},"modified":"2023-07-26T17:44:44","modified_gmt":"2023-07-26T17:44:44","slug":"a-beginners-guide-to-building-a-simple-crud-application-with-laravel-develop-your-own-crud-application","status":"publish","type":"post","link":"https:\/\/semicollar.com\/index.php\/2023\/07\/26\/a-beginners-guide-to-building-a-simple-crud-application-with-laravel-develop-your-own-crud-application\/","title":{"rendered":"A Beginner&#8217;s Guide to Building a Simple CRUD Application with Laravel &#8211; Develop your own CRUD application"},"content":{"rendered":"\n<p>In this approachable lesson, we will learn how to create a straightforward CRUD (Create, Read, Update, Delete) application from scratch and go into the realm of Laravel, a PHP framework. The majority of online applications are built on the CRUD model, which makes it simple for users to interact with data. You&#8217;ll have a firm grasp of Laravel&#8217;s fundamental concepts by the end of this blog post and be able to easily develop your own CRUD application.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Setup and Installation<\/h2>\n\n\n\n<p>Before we dive into the tutorial, make sure you have the following set up on your local machine<\/p>\n\n\n\n<ul>\n<li>PHP installed (version 7.4 or higher is recommended)<\/li>\n\n\n\n<li>Composer (PHP package manager)<\/li>\n\n\n\n<li>A web server (e.g., Apache or Nginx)<\/li>\n\n\n\n<li>Laravel installed (you can install it via Composer)<\/li>\n<\/ul>\n\n\n\n<p>Assuming you have Laravel installed, open your terminal and create a new Laravel project using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer create-project --prefer-dist laravel\/laravel crud-app<\/code><\/pre>\n\n\n\n<p>This will create a new Laravel project named &#8220;crud-app&#8221; in a folder with the same name. Move into the project directory using <code>cd crud-app<\/code>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Database Configuration<\/h2>\n\n\n\n<p>Next, configure your database credentials in the <code>.env<\/code> file located at the root of your Laravel project. Update the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DB_CONNECTION=mysql\nDB_HOST=your_database_host\nDB_PORT=your_database_port\nDB_DATABASE=your_database_name\nDB_USERNAME=your_database_username\nDB_PASSWORD=your_database_password\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Creating the Database Table<\/h2>\n\n\n\n<p>Let&#8217;s create a new migration to set up our database table. Run the following command in your terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:migration create_items_table --create=items<\/code><\/pre>\n\n\n\n<p>This will generate a new migration file in the <code>database\/migrations<\/code> folder. Open the file and define the table schema. For instance, let&#8217;s create an &#8220;items&#8221; table with an &#8220;id,&#8221; &#8220;name,&#8221; &#8220;description,&#8221; and &#8220;price&#8221; columns:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function up()\n{\n    Schema::create('items', function (Blueprint $table) {\n        $table->id();\n        $table->string('name');\n        $table->text('description');\n        $table->decimal('price', 8, 2);\n        $table->timestamps();\n    });\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Running the Migration<\/h2>\n\n\n\n<p>Run the migration to create the &#8220;items&#8221; table in the database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan migrate<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Creating the Model<\/h2>\n\n\n\n<p>Next, let&#8217;s create a model for our &#8220;Item&#8221; table. Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:model Item<\/code><\/pre>\n\n\n\n<p>This will generate an &#8220;Item&#8221; model in the <code>app<\/code> folder.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Building the Routes<\/h2>\n\n\n\n<p>In Laravel, routes define how URLs should be handled. Open the <code>routes\/web.php<\/code> file and add the following routes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Http\\Controllers\\ItemController;\n\nRoute::get('\/items', &#91;ItemController::class, 'index'])->name('items.index');\nRoute::get('\/items\/create', &#91;ItemController::class, 'create'])->name('items.create');\nRoute::post('\/items', &#91;ItemController::class, 'store'])->name('items.store');\nRoute::get('\/items\/{item}', &#91;ItemController::class, 'show'])->name('items.show');\nRoute::get('\/items\/{item}\/edit', &#91;ItemController::class, 'edit'])->name('items.edit');\nRoute::put('\/items\/{item}', &#91;ItemController::class, 'update'])->name('items.update');\nRoute::delete('\/items\/{item}', &#91;ItemController::class, 'destroy'])->name('items.destroy');\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 7: Creating the Controller<\/h2>\n\n\n\n<p>Now, we need a controller to handle the logic for our CRUD operations. Run the following command to generate the controller:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:controller ItemController --resource<\/code><\/pre>\n\n\n\n<p>This will create an <code>ItemController<\/code> with the necessary CRUD methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 8: Implementing the CRUD Methods<\/h2>\n\n\n\n<p>In the <code>ItemController<\/code> file (<code>app\/Http\/Controllers\/ItemController.php<\/code>), implement the CRUD methods:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Item;\nuse Illuminate\\Http\\Request;\n\npublic function index()\n{\n    $items = Item::all();\n    return view('items.index', compact('items'));\n}\n\npublic function create()\n{\n    return view('items.create');\n}\n\npublic function store(Request $request)\n{\n    Item::create($request->all());\n    return redirect()->route('items.index')->with('success', 'Item created successfully!');\n}\n\npublic function show(Item $item)\n{\n    return view('items.show', compact('item'));\n}\n\npublic function edit(Item $item)\n{\n    return view('items.edit', compact('item'));\n}\n\npublic function update(Request $request, Item $item)\n{\n    $item->update($request->all());\n    return redirect()->route('items.index')->with('success', 'Item updated successfully!');\n}\n\npublic function destroy(Item $item)\n{\n    $item->delete();\n    return redirect()->route('items.index')->with('success', 'Item deleted successfully!');\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 9: Creating the Views<\/h2>\n\n\n\n<p>Create a view in the resources &#8216;app.blade.php&#8217;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    &lt;title>My Laravel App&lt;\/title>\n\n    &lt;!-- Add your CSS stylesheets here, for example: -->\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.5.2\/css\/bootstrap.min.css\">\n\n    &lt;!-- Add your custom CSS file if you have one -->\n    &lt;link rel=\"stylesheet\" href=\"{{ asset('css\/custom.css') }}\">\n&lt;\/head>\n&lt;body>\n    &lt;nav class=\"navbar navbar-expand-lg navbar-dark bg-dark\">\n        &lt;div class=\"container\">\n            &lt;a class=\"navbar-brand\" href=\"{{ route('items.index') }}\">My Laravel App&lt;\/a>\n            &lt;button class=\"navbar-toggler\" type=\"button\" data-toggle=\"collapse\" data-target=\"#navbarNav\" aria-controls=\"navbarNav\" aria-expanded=\"false\" aria-label=\"Toggle navigation\">\n                &lt;span class=\"navbar-toggler-icon\">&lt;\/span>\n            &lt;\/button>\n            &lt;div class=\"collapse navbar-collapse\" id=\"navbarNav\">\n                &lt;ul class=\"navbar-nav ml-auto\">\n                    &lt;li class=\"nav-item\">\n                        &lt;a class=\"nav-link\" href=\"{{ route('items.index') }}\">All Items&lt;\/a>\n                    &lt;\/li>\n                    &lt;li class=\"nav-item\">\n                        &lt;a class=\"nav-link\" href=\"{{ route('items.create') }}\">Add Item&lt;\/a>\n                    &lt;\/li>\n                &lt;\/ul>\n            &lt;\/div>\n        &lt;\/div>\n    &lt;\/nav>\n\n    &lt;div class=\"container mt-4\">\n        @yield('content')\n    &lt;\/div>\n\n    &lt;!-- Add your JavaScript libraries here, for example: -->\n    &lt;script src=\"https:\/\/code.jquery.com\/jquery-3.5.1.slim.min.js\">&lt;\/script>\n    &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/@popperjs\/core@2.9.1\/dist\/umd\/popper.min.js\">&lt;\/script>\n    &lt;script src=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.5.2\/js\/bootstrap.min.js\">&lt;\/script>\n\n    &lt;!-- Add your custom JS file if you have one -->\n    &lt;script src=\"{{ asset('js\/custom.js') }}\">&lt;\/script>\n&lt;\/body>\n&lt;\/html>\n<\/code><\/pre>\n\n\n\n<p><code>resources\/views\/items\/index.blade.php<\/code> (Display all items):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@extends('layouts.app')\n\n@section('content')\n    &lt;div class=\"container\">\n        &lt;h1>All Items&lt;\/h1>\n        &lt;table class=\"table\">\n            &lt;thead>\n                &lt;tr>\n                    &lt;th>ID&lt;\/th>\n                    &lt;th>Name&lt;\/th>\n                    &lt;th>Description&lt;\/th>\n                    &lt;th>Price&lt;\/th>\n                    &lt;th>Action&lt;\/th>\n                &lt;\/tr>\n            &lt;\/thead>\n            &lt;tbody>\n                @foreach($items as $item)\n                    &lt;tr>\n                        &lt;td>{{ $item->id }}&lt;\/td>\n                        &lt;td>{{ $item->name }}&lt;\/td>\n                        &lt;td>{{ $item->description }}&lt;\/td>\n                        &lt;td>{{ $item->price }}&lt;\/td>\n                        &lt;td>\n                            &lt;a href=\"{{ route('items.show', $item->id) }}\" class=\"btn btn-primary\">View&lt;\/a>\n                            &lt;a href=\"{{ route('items.edit', $item->id) }}\" class=\"btn btn-info\">Edit&lt;\/a>\n                            &lt;form action=\"{{ route('items.destroy', $item->id) }}\" method=\"POST\" style=\"display: inline-block;\">\n                                @csrf\n                                @method('DELETE')\n                                &lt;button type=\"submit\" class=\"btn btn-danger\" onclick=\"return confirm('Are you sure?')\">Delete&lt;\/button>\n                            &lt;\/form>\n                        &lt;\/td>\n                    &lt;\/tr>\n                @endforeach\n            &lt;\/tbody>\n        &lt;\/table>\n    &lt;\/div>\n@endsection\n<\/code><\/pre>\n\n\n\n<p><code>resources\/views\/items\/create.blade.php<\/code> (Form to create a new item):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@extends('layouts.app')\n\n@section('content')\n    &lt;div class=\"container\">\n        &lt;h1>Create New Item&lt;\/h1>\n        &lt;form action=\"{{ route('items.store') }}\" method=\"POST\">\n            @csrf\n            &lt;div class=\"form-group\">\n                &lt;label for=\"name\">Name&lt;\/label>\n                &lt;input type=\"text\" name=\"name\" id=\"name\" class=\"form-control\" required>\n            &lt;\/div>\n            &lt;div class=\"form-group\">\n                &lt;label for=\"description\">Description&lt;\/label>\n                &lt;textarea name=\"description\" id=\"description\" class=\"form-control\" rows=\"3\" required>&lt;\/textarea>\n            &lt;\/div>\n            &lt;div class=\"form-group\">\n                &lt;label for=\"price\">Price&lt;\/label>\n                &lt;input type=\"number\" name=\"price\" id=\"price\" class=\"form-control\" required>\n            &lt;\/div>\n            &lt;button type=\"submit\" class=\"btn btn-primary\">Create&lt;\/button>\n        &lt;\/form>\n    &lt;\/div>\n@endsection\n<\/code><\/pre>\n\n\n\n<p><code>resources\/views\/items\/show.blade.php<\/code> (Show details of a single item):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@extends('layouts.app')\n\n@section('content')\n    &lt;div class=\"container\">\n        &lt;h1>Item Details&lt;\/h1>\n        &lt;p>&lt;strong>ID:&lt;\/strong> {{ $item->id }}&lt;\/p>\n        &lt;p>&lt;strong>Name:&lt;\/strong> {{ $item->name }}&lt;\/p>\n        &lt;p>&lt;strong>Description:&lt;\/strong> {{ $item->description }}&lt;\/p>\n        &lt;p>&lt;strong>Price:&lt;\/strong> {{ $item->price }}&lt;\/p>\n        &lt;a href=\"{{ route('items.edit', $item->id) }}\" class=\"btn btn-info\">Edit&lt;\/a>\n        &lt;form action=\"{{ route('items.destroy', $item->id) }}\" method=\"POST\" style=\"display: inline-block;\">\n            @csrf\n            @method('DELETE')\n            &lt;button type=\"submit\" class=\"btn btn-danger\" onclick=\"return confirm('Are you sure?')\">Delete&lt;\/button>\n        &lt;\/form>\n    &lt;\/div>\n@endsection\n<\/code><\/pre>\n\n\n\n<p><code>resources\/views\/items\/edit.blade.php<\/code> (Edit an existing item):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@extends('layouts.app')\n\n@section('content')\n    &lt;div class=\"container\">\n        &lt;h1>Edit Item&lt;\/h1>\n        &lt;form action=\"{{ route('items.update', $item->id) }}\" method=\"POST\">\n            @csrf\n            @method('PUT')\n            &lt;div class=\"form-group\">\n                &lt;label for=\"name\">Name&lt;\/label>\n                &lt;input type=\"text\" name=\"name\" id=\"name\" class=\"form-control\" value=\"{{ $item->name }}\" required>\n            &lt;\/div>\n            &lt;div class=\"form-group\">\n                &lt;label for=\"description\">Description&lt;\/label>\n                &lt;textarea name=\"description\" id=\"description\" class=\"form-control\" rows=\"3\" required>{{ $item->description }}&lt;\/textarea>\n            &lt;\/div>\n            &lt;div class=\"form-group\">\n                &lt;label for=\"price\">Price&lt;\/label>\n                &lt;input type=\"number\" name=\"price\" id=\"price\" class=\"form-control\" value=\"{{ $item->price }}\" required>\n            &lt;\/div>\n            &lt;button type=\"submit\" class=\"btn btn-primary\">Update&lt;\/button>\n        &lt;\/form>\n    &lt;\/div>\n@endsection\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion:<\/h2>\n\n\n\n<p>Congratulations! You&#8217;ve successfully built a basic CRUD application using Laravel. You&#8217;ve learned how to set up the database, create migrations, models, controllers, and views to perform CRUD operations. With this foundation, you can continue exploring Laravel&#8217;s vast capabilities and build more complex web applications. <strong>Happy coding!<\/strong> \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this approachable lesson, we will learn how to create a straightforward CRUD (Create, Read, Update, Delete) application from scratch and go into the realm <a href=\"https:\/\/semicollar.com\/index.php\/2023\/07\/26\/a-beginners-guide-to-building-a-simple-crud-application-with-laravel-develop-your-own-crud-application\/\" class=\"read-more-link\">[Read More&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":274,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[112],"tags":[10,114,113,115],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/posts\/272"}],"collection":[{"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":3,"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions\/276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/media\/274"}],"wp:attachment":[{"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/semicollar.com\/index.php\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}