How to add an image upload in the form in laravel

Posted 2 weeks ago by michaelabraham

So i already made a form that upload a title and a content(in text editor) in laravel to a database, how do i include an image upload before content in the form to be posted in the same table in database and show it in the view, i know in ck editor you can also upload image, but i also want to edit how the image is shown for other view, i have made and additional column in the table called thumbnail, and routes for the image upload function but i haven't made the method function

this is my controller BlogController.php for the form and view

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Blog;

class BlogController extends Controller
{
    public function index()
    {
        $blogs = Blog::get();
        return view('post.post_textarea',[
            'blogs' => $blogs
        ]);
    }

    public function store(Request $request)
    {
        Blog::create([
            'name' => $request->name,
            'message' => $request->message
        ]);
        return redirect()->back();
    }

    public function getFullPost($blog_id) {
    $blogs = Blog::where('id', '=', $blog_id)->get();
    return view('post.read')->with(compact('blogs'));
    }
}

this is the view for the from post_textarea.blade.php

<section class="games-single-page">
        <div class="container">

            <div class="card">
                <div class="card-body">
                    <form enctype="multipart/form-data" method="POST">
                        {{csrf_field()}}
                        <div class="form-group">
                            <label style="color: black;" >Judul</label>
                            <input type="text" class="form-control" name="name">
                        </div>
                        <div class="form-group">
                            <label style="color: black;" >Thumbnail</label>
                            <br>
                            <input type="file" name="select_file">
                        </div>
                        <div class="form-group">
                            <label style="color: black;" >Isi</label>
                            <br>
                                <textarea class="form-control" name="message" id="" rows="10"></textarea>
                        </div>
                        <div class="form-group">
                            <button class="pull-right site-btn" type="submit" >Upload<img src="../public/asset/img/icons/double-arrow.png" alt="#"/></button>
                        </div>                     
                    </form>
                </div>
            </div>
        </div>
    </section>

this is the view for the full post read.blade.php

<section class="games-single-page">
        <div class="container">
            <div class="row">
                <div class="col-xl-9 col-lg-8 col-md-7 game-single-content">
                    @foreach ($blogs as $blog)
                    <div class="gs-meta">{{ Carbon\Carbon::parse($blog->created_at)->format('d-m-Y')  }} /  di <a href="">Games</a></div>
                    <h2 class="gs-title">{{ $blog->name }}</h2>
                    <p>{!! $blog->message !!}</p>
                    @endforeach
                </div>
            </div>
        </div>
    </section>

this is the model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
    protected $fillable = ['name','thumbnail','message'];
}

and this is the routes

Route::get('/posting','[email protected]')->name('blog');
Route::post('/posting','[email protected]')->name('blog.store');
Route::post('/profile','[email protected]')->name('blog.thumb');
Route::post('/posting','[email protected]')->name('blog.store');

Please sign in or create an account to participate in this conversation.