How to Create a Blog by using Laravel - (r)

Aug 12, 2023

@yield('content') Mini-Blog (c) 2023 [email protected]">[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous">

With this HTML code, it's possible to can use Bootstrap version 5.2.3 and Vite for combining JavaScript along with CSS assets. The webpage you build is a header, which includes a navbar and a footer, with scripts below. The body contains dynamic content can be rendered using different Blade files using the power of @yield('content').

postsdirectory postsdirectorydirectory is where you can find the Blade documents needed for the actions of read and creation.

  1. In the postsdirectory Create in the postsdirectory the Blade directory file with the name index.blade.php and add the code below:
@extends('layouts.app') @section('content') Blog list The Blog 1 - $post @endsection

The code is extended over the app.blade.php file on the layouts page. Once it's displayed in the browser, it will display the blog's content article and also the navigation bar and footer taken from the app.blade.phpfile in the layouts folder. In between the tag sections are the tags that you could pass content from the controller to render within the browser after you run the app.

  1. Make the route by using the routesdirectory. This will load the route automatically of it's RouteServiceProvider found in the App/Providers directory. The RouteServiceProvider will be the primary class that loads routes for the application.
  2. Inside the routes/web.php file, import PostController using use AppHttpControllersPostController.
  3. Then, set the route by adding Route::resource('posts', PostController::class); to the routes/web.php file.
  4. With your Vite development server, you can use the PHP artisan server to run the program in your terminal.
  5. With your browser, open http://127.0.0.1:8000/posts to see your new blog post list.

The page should look like this:

The blog application is displayed in the browser
The blog application can be shown within the browser.

In in the subsequent section we'll outline the methods of controllers to display all postings, creating posts after which we'll store the posts. Add their routes, and then create Blade files in the proper sections.

Design blog post Page

Write blog entries by entering the title, providing an explanation, and uploading photos. After that, you can display your blog posts in a chronological order.

  1. Within the app/Models directory, navigate to the app/Models directory, open Post.php file.
  2. Within the Post class, below it, use HasFactory; code block, insert the fillable protected value ['title' 'description image'];.

The software protects your model's attributes from being put in mass.

  1. In your app/Http/Controllers/PostController.php file, import the Post model using use AppModelsPost;.
  2. Replace the index and create controller methods that were previously developed within the PostController class using the following code:
// Show all posts public function index() $posts = Post::orderBy('created_at', 'desc')->get(); return view('posts.index', ['posts' => $posts]); // Create post public function create() return view('posts.create'); 

With index method, the index method that you have just developed using the index method that you created earlier This PHP application collects all post put them into chronological order, then saves posts in the posts variable. The return view shows the posts that have been inserted through index.blade.php. index.blade.php file as an instance variable within the posts/views directory. This creation method creates the create.blade.php file and puts it into the posts/views directory, if the user tries to make the post.

  1. Make the store controller using the following code (to save blog posts within databases). Include this code in the PostController class below index. Add this code to index. Add this code to index as well as develop controller method.
// Store post public function store(Request $request) // Validation $request("'title'" => 'required' 'description' => 'required image'> 'required

This storage method takes care of client queries regarding the data inside its body. This means that it will take a the request for argument. Then, you go through the fields used for creating posts. You then make an instance of an Post instance with it's model of the Post model. Information you type into fields will be transferred into the new instance after which it is saved. The site redirects users to an the index page, which displays a flash text which reads "Post created successfully."

Create Routes for Your Posts

To add the routes in the file web.php file:

  1. In the routings directory in the root directory for your project You can access the web.php. web.php file.
  2. Register the route for controller methods and replace the current code by this:
names([ 'index' => 'posts.index', 'create' => 'posts.create', 'store' => 'posts.store', 'show' => 'posts.show', ]);

The controller utilizes these routes to create the data objects, store and display the data objects you have created.

Make Blade Files

To create the views, return in this class PostController class:

  1. In the resources/views/posts directory, make a Blade file called create.blade.php and add the code below:
@extends('layouts.app') @section('content') Add Post @csrf @if ($errors->any()) @foreach ($errors->all() as $error) $error @endforeach @endif Title Description Add Image Save @endsection

In this code, create.blade.php inherits the contents of app.blade.php in the layouts directory using @extends('layouts.app'). It includes a heading, footer and navigation bar. When you've added to the Add Post text in the tag's h1 tag, you created forms using an method of post. This is the method that contains it's route('posts.store') action.

The code enctype="multipart/form-data" allows for image uploads, and csrf protects your form from cross-site attacks. In addition, the errors display as fields that are not valid as well as uses field attributes to make labels and inputs for the form.

  1. Change the code in the index.blade.php file with this code. This will display all blog posts:
@extends('layouts.app') @section('content') Add Post Mini post list @if ($message = Session::get('success')) $message @endif @if (count($posts) > 0) @foreach ($posts as $post) $post->title $post->description @endforeach @else No Posts found @endif @endsection

Then, you can click the "Add Post" button. When clicked, it creates the post, and also inserts any data into the content of the webpage. The If condition determines for data within the database. If it is it is true, the condition will be met. In the event that it fails, the display will read "No Posts found."

Design Your Pages

You can now make use of PHP artist server to display and publish blog posts. Open http://127.0.0.1:8000, and the page should look like this:

The blog application appears in the browser
The blog application appears within the browser.

If you add your blog to the site, it will appear in the following manner:

The blog application displays a post in the browser
Blog applications display blog posts within the browser

Deploy Your Laravel Blog on

  1. Create a .htaccess file.
  2. Upload the code into an archive.
  3. Establish an online database.
  4. Set up a project on My.
  5. Make and publish a blog.

Make an .htaccess file.

Within the root directory of the project create a new file titled .htaccess, and add the following code to the file:

RewriteEngine On RewriteRule ^(. *)$ public/$1 [L]

This code redirects the application's request to public/index.php in the installation.

You can send your code to the Repository

Make a repository of your creation and upload your code. It's possible to utilize GitHub, GitLab, or Bitbucket to place your code online as well as upload it onto My.

Create the database inside Your My Dashboard

Create a database of My:

  1. Select"Add Service" or the "Add Service button Select "Database" as the Database choice..
  2. Input the information about your database into the format below. Be sure, in order for your database installation to work it is possible to leave the Database username as the default value.
Creating a database in My
The creation of a database with My

The information includes the Database Name, Display Name Database Type, Version Database username, Data Center location and the dimension. This instance uses MariaDB to serve as the primary database and its dimension can be described in terms of Db3 (1CPU 4GB RAM, 10GB Disk Space, 65 USD per month). You are able to choose the type of database and the size that best suits your particular requirements.

  1. Click to to Continue.
  2. Review your monthly expenditures as well as the payment method. Then select Create database.

Create the project for My

In order to deploy your application My:

  1. Click the Dashboard panel.
  2. Simply click "Add Service and choose the Program like the following:
My dashboard when adding application service
My dashboard after adding the application service

My application will direct users to Apply the Application page.

  1. Under the Select branch card section, select the repository you have on GitHub, and then click the Add deploy to commit box.
  2. In the basic information section section, in the Basic details section, enter your name as an applicant and choose your data center for the application.
  3. Since Laravel has an App-specific key during deployment, in the environment variables card, you should include an App Key as Key 1. The key can be used as the app key defined within your local .env file or you can use the internet-based key generator that works with Laravel to generate one.
  4. Click to continue.
  5. Pick the appropriate building resource (CPU or RAM) to build your app. This example uses the standard build machine (1CPU and 4GB RAM) + 0.02USD per minute.
  6. Make sure to leave the Image of the container to be placed in place through hitting the radio button.
  7. Click to continue.
  8. When you are on the setting up your procedure page, you are able to change the pod's size as well as the type of instance, by choosing those boxes. This instance uses the defaults.
  9. Click to Continue.
  10. Then, press then the "Confirm the Payment Method" button to initiate your application's deployment. By clicking this, you will be taken to the details about your application page, where you'll have the ability monitor the progress of the deployment.

Design and launch Your Application

After the database and application are hosting, connect the database to your application, and then develop the application for it to be used.

To connect to the database you have, make use of the connections that are external to your hosted database. In the Infotab of your hosted database there is a list of external connections which are as follows:

External connections for your hosted database
External connections to connect your database that is hosted on the internet
  1. Within the application's Settingspage, navigate towards the environment variable card.
  2. Click Add an Environment Variable to create additional connections that connect to your database hosted by the value that is related to it. Utilize the same variables inside the .env file.
The environment variables for your hosted database
The environment variables for your database hosted on the internet

This image can be helpful when you think about the importance of marking the env variables you have edited manually for the purpose of separating the variables from others.

The app_urlis an URL of the web application that you host and DB_CONNECTION is mysql.

  1. Go to your application's settings page.
  2. In the Buildpackcard In the Buildpackcard, you can add PHP and Node.js in build pack. Because it's the PHP application, it is necessary for you to download it after the PHP build pack after Node.js.
  3. Click Now to Install to build your application.

You can then create a new procedure for migrating your database.

  1. Visit on the Processestab of your host's application's website.
  2. Select procedure to create from the process card.
  3. Input Migration in the form of a name background worker in the form of a kind, and the PHP artisan migration to start the process. It is possible to change the pod dimensions and number of instances to the defaults.
  4. Choose to continueto start the application. This will trigger a brand fresh build, and will redeploy the program.
  5. In your domainstab of your application click the link for the application. It will show that the application is at present running.
  6. Note that the blog application that is installed to My does not display blog entries. Create a new post by entering the name of your blog post and then adding a description. selecting an image.

Summary

Laravel can help you create a basic blog in a short time. The speed of page loading, sturdy controller design, as well as dependable security system make it easy to improve the performance of your application. Additionally, My lets you release and deliver your online applications swiftly and efficiently. My's pricing structure is built on usage, which means there are no additional charges.

Marcia Ramos

I'm the editororial team leader at . I'm a fan of open source and am a huge fan of coding. For the past 7 years of writing technical content as well as editing for tech companies, I enjoy collaborating with colleagues to produce short and precise pieces of content and improve methods.

Article was first seen on here