devis98's avatar

Display Pdf File in show.blade.php view Laravel 8

Hello all,

Hope you're well.

I'm trying to show a PDF file in one of my page using tag, and I'm getting an 404 error.

On the index.blade.php page of my model everything is ok but on my show.blade.php I have error.

Below my code :

show.blade.php

@extends('layouts.app')

<title>Machine {{$machines->name}}</title>

@section('content')
    <div class="container">
        <h3 class="mt-2">Détails de la machine</h3>
        <h3 class="mt-2">{{$machines->name}}</h3>

        <div class="row">
            <div class="col-6">
                <img src="{{asset('machines/images/'.$machines->image)}}" height="500" width="500" >
            </div>
            <div class="col-6">
                <h4 class="mt-3">Description</h4>
                <p>{{$machines->description}}</p>
            </div>
        </div>

        <h3 class="my-4">Eclaté Technique</h3>

        <embed src="{{$machines->technical_details}}" height="500" width="1000">
    </div>
@endsection

index.blade.php

@extends('layouts.app')

<title> Les machines </title>

@section('content')
    <div class="container">
        <h1 class="mt-2"> Liste machine </h1>

        <a class="btn btn-dark mt-2" href="{{route('create.machine')}}"> Créer une machine</a>

        <div class="row row-cols-1 row-cols-md-3 g-4 mt-4">
            @foreach($machines as $machine)
                <div class="col">
                    <div class="card">
                        <img src="{{ asset('machines/images/'.$machine->image)}}" class="card-img-top" alt="Photo machine ">
                        <div class="card-body">
                            <h5 class="card-title">{{$machine->name}}</h5>
                            <p class="card-text">
                                <a class="btn btn-dark" href="{{url('/show_machine/'.$machine->id)}}"> Voir la machine </a>
                                <embed src="{{$machine->technical_details}}" height="500" width="1000">
                            </p>
                        </div>
                    </div>
                    @endforeach
                </div>
        </div>

    </div>

@endsection

MachinesController.php

<?php

namespace App\Http\Controllers;


use App\Models\Machines;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;

class MachinesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index()
    {
        $machines = DB::table('machines')->get();

        return view('machines.index', ['machines' => $machines]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return view('machines.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Application|RedirectResponse|Redirector
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'image' => 'required|mimes:png,jpg,jpeg,svg,webp|max:50000',
            'technical_details'=>'required|mimes:pdf,csv,xlsx|max:100000'
        ]);

        $input = $request->all();

        //Image Upload
        if ($image_machine = $request->file('image')) {
            $randomString = Str::random(5);
            $destinationPath = public_path('machines/images');
            $image = $randomString . "." . $image_machine->getClientOriginalExtension();
            $image_machine->move($destinationPath, $image);
            $input['image'] = "$image";
        }

        //File upload

        $fileName = time().'.'.$request->technical_details->extension();
        $path = ('machines/technical_details');
        $technical_details = $request->technical_details->move($path, $fileName);
        $input['technical_details']= $technical_details;

        $machine = Machines::create($input);

        $machine->save();

        return redirect()->route('index.machine');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|Response
     */
    public function show($id)
    {
        $machines = Machines::find($id);
        return view('machines.show',['machines' => $machines]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return Response
     */
    public function destroy($id)
    {
        Machines::destroy($id);
        return redirect()->back();
    }
}

Web.php

<?php

use App\Http\Controllers\MachinesController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

//Machines

Route::get('/machine', [MachinesController::class, 'index'])->name('index.machine');

Route::get('add_machine', [MachinesController::class, 'create'])->name('create.machine');

Route::post('add_machine', [MachinesController::class, 'store'])->name('store.machine');

Route::get('/show_machine/{id}', [MachinesController::class, 'show'])->name('show.machine');

Can you help me please ?

0 likes
0 replies

Please or to participate in this conversation.