Laravel 6

utilisation de l'API medilibrary sur lavarel6

Avatar de Pat2010
Pat2010

Bonsoir , je veux utiliser medialibrary pour telecharger et afficher les images pour mes publications. Quand je compile , j'obtiens ce résultat :

Facade\Ignition\Exceptions\ViewException
Undefined variable: product (View: /home/olivier/prototype/resources/views/welcome.blade.php)

voici mon modele product :

<?php

namespace App;

use Spatie\MediaLibrary\Models\Media;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class Product extends Model implements HasMedia
{

    use HasMediaTrait;

    protected $guarded = [];

    public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumb')
              ->width(200)
              ->height(200)
              ->sharpen(10);

        $this->addMediaConversion('square')
              ->width(412)
              ->height(412)
              ->sharpen(10);
    }
}

productcontroller:

<?php

namespace App\Http\Controllers;

use App\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       return view('index');
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //Populate model
        $product = new Product($request->except(['image']));
        
        $product->save();

        //Store Image
        if($request->hasFile('image') && $request->file('image')->isValid()){
            $product->addMediaFromRequest('image')->toMediaCollection('images');
        }
        

        return redirect("/products/{$product->id}")->with('success', 'New Gift Added !');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function show(Product $product)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function edit(Product $product)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Product $product)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function destroy(Product $product)
    {
        //
    }
}

vue :

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Product Details</div>

                <div class="card-body">
                     Product Name:  {{$product->name}} <br/><br/>

                     Product Description : {{$product->description}} <br/><br/>


                     Image: <img src="{{$product->getFirstMediaUrl('images', 'thumb')}}" />
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

vue de formulaire :

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Product</div>

                <div class="card-body">
                     <form method="POST" action="/products" enctype="multipart/form-data">
                        @csrf
                        <div class="form-group">
                            <label>Product Name</label>
                            <input name="name" type="text" class="form-control" id="name" placeholder="Enter Product Name" value="{{old('name')}}">
                        </div>
                        <div class="form-group">
                            <label>Product Description</label>
                            <textarea name="description" class="form-control" id="description">{{old('description')}}</textarea>
                        </div>
                        <div class="form-group">
                            <label for="exampleFormControlFile1">Product Image</label>
                            <input name="image" type="file" class="form-control-file" id="image">
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-primary" value="Submit"/>
                        </div>
                     </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Avatar de bestmomo
bestmomo

Salut

Apparemment il manque la variable product dans la vue welcome. Elle est comment cette vue et appelée d'où ?

Avatar de Pat2010
Pat2010

Merci j'ai pu trouver l'erreur , il devrait juste declarer la variable product dans la fonction index () du controleur

Avatar de Pat2010
Pat2010

mais j'ai une autre question ?

Avatar de CinquièmeDimension
CinquièmeDimension

Salut,

Une autre question ?

Vous ne pouvez pas répondre à ce sujet.