Hi Guys,
Nowadays, I can study you the way to use spatie medialibrary in laravel 8 application,we are able to show example of laravel spatie medialibrary.you can easliy use spatie media library laravel eight.this package use to add a image or an avatar. in this academic we will show you an smooth manner to feature it, using Spatie’s Media Library bundle.
In this post, let’s move over the well-known laravel medialibrary package deal evolved by way of Spatie. This bundle deal can accomplice all sorts of documents together with your Eloquent models.
Here, I'm able to give you full example for spatie media library laravel eight as bellow.
Step 1 : Install Laravel 8In the first step, we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.
composer create-project --prefer-dist laravel/laravel postStep 2 : Database Configuration
>In second step, we will make database Configuration for example database name, username, password etc. So lets open .env file and fill all deatils like as bellow:
.envDB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name(post) DB_USERNAME=here database username(root) DB_PASSWORD=here database password(root)Step 3 : Install Spatie Medialibrary
In this step, we need laravel/Spatie Medialibrary package. you can install Spatie Medialibrary package using bellow command so let's run bellow command
composer require "spatie/laravel-medialibrary:^9.6.0"
After the package is installed, run the following command to copy the migration file from to package directory to your project directory and also to run the fresh migration command.
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
php artisan migrateStep 4: Create Model and Migration
here this step, we will create one model and migration name post. Use the below following command and create it
php artisan make:model post -m
Next,Open post migration file and put the below code. here following path of migration file
Path: /database/migrations/2020_05_27_095534_create_posts_table.php<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
Next, go to app/post.php and open post model file and put the below code. here following path of model fille
Path:/app/Models/Post.php<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; class Post extends Model implements HasMedia { use HasFactory,InteractsWithMedia; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'title', 'body', ]; }Step 5: Create Route
Create two routes one for show form and the second route send data to the server:
here following path of route fille
Path:/routes/web.php<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('post',[PostController::class,'index'])->name('post'); Route::get('post/create',[PostController::class,'create'])->name('post.create'); Route::post('post/store',[PostController::class,'store'])->name('post.store');Step 6:Create Controller
In this step,we will create a controller. Use the below command for generate controller
php artisan make:controller PostController
Here this step,we will create two methods inside the controller first index method is used to display post form and second store method is used to store data in the mysql database and image upload Medialibrary to storge folder
here following path of Controller fille.
Path:/app/Http/Controllers/postController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $posts = Post::latest()->get(); return view('post.index',compact('posts')); } /** * Write code on Method * * @return response() */ public function create() { return view('post.create'); } /** * Write code on Method * * @return response() */ public function store(Request $request) { $input = $request->all(); $post = Post::create($input); if($request->hasFile('image') && $request->file('image')->isValid()){ $post->addMediaFromRequest('image')->toMediaCollection('images'); } return redirect()->route('post'); } }Step 7:Create a blade view
In this step, we will create two blade file name post/index.blade.php and post/create.blade.php.
here following path of index.blade fille
Path:/resources/views/post/index.blade.php<html> <head> <title>Laravel Spatie Medialibrary Example</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" /> </head> <body class="bg-dark"> <div class="container"> <div class="row"> <div class="col-md-8 offset-2"> <div class="card mt-5"> <div class="card-header"> <div class="row"> <div class="col-md-10"> <h5>Laravel 8 Spatie Medialibrary Example</h5> </div> <div class="col-md-2 text-center"> <a href="{{ route('post.create') }}" class="btn btn-success btn-sm">Create</a> </div> </div> </div> <div class="card-body"> <table class="table table-bordered"> <thead> <tr> <th>No</th> <th>Title</th> <th width="25%">Image</th> </tr> </thead> <tbody> @foreach($posts as $key=>$post) <tr> <td>{{ ++$key }}</td> <td>{{ $post->title }}</td> <td><img src="{{$post->getFirstMediaUrl('images', 'thumb')}}" / width="100%"></td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> </div> </body> </html>
Next following path create a create.blade fille
Path:/resources/views/post/create.blade.php<!DOCTYPE html> <html> <head> <title>Laravel Spatie Medialibrary Example</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" /> </head> <body class="bg-dark"> <div class="container"> <div class="row"> <div class="col-md-8 offset-2"> <div class="card mt-5"> <div class="card-header"> <div class="row"> <div class="col-md-10"> <h5>Laravel 8 Spatie Medialibrary Example</h5> </div> <div class="col-md-2 text-center"> <a href="{{ route('post') }}" class="btn btn-info btn-sm">Back</a> </div> </div> </div> <div class="card-body"> <form action="{{ route('post.store') }}" method="post" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-md-12 mb-3"> <div class="form-group"> <label for="">Title:</label> <input type="" name="title" class="form-control" placeholder="Enter Title"> </div> </div> </div> <div class="row"> <div class="col-md-12 mb-3"> <label for="">Body:</label> <textarea name="body" id="" class="form-control" rows="3"></textarea> </div> </div> <div class="row"> <div class="col-md-12 mb-3"> <label for="">Image:</label> <input type="file" name="image" class="form-control"> </div> </div> <div class="row"> <div class="col-md-12 mb-3 text-center"> <button class="btn btn-success btn-block">Submit</button> </div> </div> </form> </div> </div> </div> </div> </div> </body> </html>
After Note that to view the files in the browser you need to make the files public that is stored in the storage directory, you need to run the following command on your project root in terminal/command-line.
php artisan storage:linknext change .env file in APP_URL path
...... APP_URL=http://localhost:9000 ......
Now, we will use the php artisan serve command.
php artisan serve --port=9000
Now we are ready to run our example so run bellow command to quick run.
http://localhost:9000/post
It will help you...
0 Comments