Posts

Adding Images to Posts and Subweddits using Storage

Image
I've left this part of the project until the posts and subweddits were completed to avoid over-complicating things, as I know its a case of a simple migration and a bit of fiddling in the controller from this point. Both subweddits and posts will have some sort of file upload, in the form of an image. Subweddits will have their logo, which is mandatory, and posts can have an optional image upload. We must first make a migration to add_logo_to_subweddit and add_img_to_post. Besides the names of the fields, the migration looks the same. These are both the frontend and backend of the new file upload. These files could be anything, so require both clientside and serverside validation, but these will be attempted later on, I'll focus on the functionality first. Once the data is passed to the controller, there must be a way to save the data, for that we need to import 'Storage' [do full path], and set the path of the image to the logo/img/field. so save being confused, I'...

Creating, Editing and Deleting Posts

Image
Now for the meat of the project, creating posts with content and file storage and upload. Like the same as adding subweddit functionality, we need a model, controller, factory and migration.  The design of the Posts table is as follows: A post will have a title, body, an id and a foreign key user_id, set as the creator of the post, who will be perform edit and delete actions on the post. We can now fill the database table with mock results it was at this point I forgot to add subweddit_id (the subweddit the post belongs to) to the post table! time to make a migration after adapting the factory, I was able to add a mock row in the database Changing the subweddit_id of the post using phpMyAdmin allowed me to simulate a post from the first subweddit so I can test a show() method in SuubwedditController At this point I had this in my SubwedditController: Notice - getFirstOrFail() - method call - this will find the first post where the subweddit_id is equal to the subweddits id, then co...

Creating and Viewing a Subweddit

Image
  To begin making a subweddit, we must make 4 things -  A Subweddit model A make_subweddit_table migration A Subweddit factory (a useful laravel technique for filling a database table with fake data A subweddit Controller, to control the data All 3 can be made by the command : php artisan make:model Subweddit -fmc (the suffix -fmc does the magic, making the factory, migration and controller) For the miagration - this is the layout as it stands: Apart from the ordinary id and timestamps, the subweddit table contains the subweddit name, bio and mod_id, which, by default, is the user who created the subweddit, they will be able to delete the subweddit, and in the future remove posts as well as comments from posts.   Now we are able to fill in the factory to create some fake data. after altering SubwedditFactory.php to look like so and attempting to use the factory by running  factory(App\Models\Subweddit)->create();  I encountered a problem  PHP Fatal error...

Creating a login system with Jetstream & Livewire

Image
For this project I will be using Jetstream and Livewire to create a killer login system. This process couldn't be simpler, only requiring 3 lines of code. composer require laravel/jetstream php artisan jetstream:install livewire npm install && npm run dev and voila! a working authentication system! but let's make some personal tweaks.. Perfect... When signing up to Reddit.com, you are asked for your name AND  username, which is not available out the box with livewire, so we must adapt! Firstly, I added a new field in the Livewire's register.blade.php form to take in 'username'. The issue now being that Reddit does not allow spaces in their usernames. A quick bit of validation in Fortify's 'CreateNewUser' action using the argument 'alpha_dash' means that usernames may be alpha-numeric and also contain dashes and underscores. And make sure to persist these changes below! At this point, the form appeared to be displaying correctly, and valid...

Welcome to Weddit

Image
Welcome! In this blog I will be attempting to create a clone of Reddit.com using the PHP framework - Laravel.  This blog will follow my progress, analysis of code and include any problems encountered along the way, as well as any solutions (If I find any!).  Disclaimer: Weddit is purely a display of technical ability and as such is not fully GDPR compliant. Furthermore, this application will not be used for commercial or revenue purposes. The main features of this application will include: Authentication Subweddits (communities containing subweddit-specific posts) Posts (including authorization, validation, CRUD, media upload, likes, comments) Timeline Admin access Guest access For simplicity - I will ignore some features of reddit.com such as the 'hot' feature - this features takes in to account the popularity of a post coupled with its submission date. Progress can be tracked on my GitHub account here - https://github.com/ConvertToInt Sit back and enjoy the (not so smoo...