در حال بارگذاری ...

Creating Image Model0:00

All right, so we have our Laravel application stood up. Now we just need to add a few things to it. We're going to be uploading and storing images, so we'll need a model for that, and a migration in order to get the database structure set up. Let's open up our terminal. And we can go ahead and create that with php artisan make:model Image -m, and tack on the migration flag. That way we create the model and migration at the same time. And let's open up the migration. So for each Image model, we'll need a way to reference how to get that image out of

Defining Migration Fields0:33

And let's open up the migration. So for each Image model, we'll need a way to reference how to get that image out of our application. So we'll need some kind of unique name. So tableString name. And really that's all we need for this, but let's also save the extension and the size of it, which will be the actual amount of bytes the image is. That looks good, so let's go ahead and run the migration and get our database structure added in. php artisan migrate.

Configuring Model and Route1:08

added in. php artisan migrate. All right, now let's open up our model. And under the UseFactory, we're going to include this protected guarded attribute and set it to an empty array. That way when we start creating Image models, we can just pass in any data attributes that we want. All right, and in our routes/web.php file, let's get rid of this default route here. And instead we'll create a new one that uses a controller, ImageController class.

Building ImageController Index1:45

And instead we'll create a new one that uses a controller. ImageController class. And the index method. Let's include that at the top here. And create that controller. php artisan make:controller ImageController. And open it up. So for our public function index, let's return a view called images.index. And we'll go ahead and create that view now. Now this view will be the main basis of our application, so I'm just going to go ahead

Scaffolding Main View2:37

And we'll go ahead and create that view now. Now this view will be the main basis of our application, so I'm just going to go ahead and scaffold out something pretty simple here. For the title, we'll just use the title of the series, Image Uploading with View in Laravel. And I'm going to include a link to the CDN version of Tailwind. As well as a viewport. And then for the body, let's just create a div with some Tailwind classes. And a title, Image Uploader. And we'll give that some classes as well. Let's make it a pretty large text, bold, and centered.

Adding Gallery and Upload Routes3:47

And we'll give that some classes as well. Let's make it a pretty large text, bold, and centered. Alright let's see how this looks in the browser. Perfect. Now we'll need two more routes for this application to work. We'll need one for getting all of the images to display in the gallery, and one for posting to the application to save an image. So back in our routes/web.php file, let's create the images one, routeGetImages, ImageController class, and the method for this will just be called show. And we'll do routePostUpload, ImageController, and the method for this will be store.

Creating a model + migrationScaffolding a basic view with Tailwind

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟