Error Method save does not exist in Laravel

Asked

Viewed 477 times

0

Good afternoon guys. I have an application that uses Polymorphic Relations according to Laravel’s doc.

https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-Relations

I have the error below when trying to save the mode. The error occurs in the second save() of the store method.

BadMethodCallException in Macroable.php line 74:
Method save does not exist.

View:

<div class="container">
<div class="panel panel-primary">
    <div class="panel-heading">Modalidades</div>
    <div class="panel-body">
        <form method="post" action="/modality/store" enctype="multipart/form-data">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class="form-group">
                <label for="exampleInputEmail1">Nome</label>
                <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Modalidade" name="name">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">Descrição</label>
                <textarea class="form-control" placeholder="Descrição" name="description"></textarea>
            </div>
            <div class="form-group">
                <label for="exampleInputFile">Upload de imagens</label>
                <input type="file" name="image" id="exampleInputFile">
                <p class="help-block">Para selecionar multiplas imagens segure Crtl e escolha as imagens</p>
            </div>
            <button type="submit" class="btn btn-primary">Adicionar modalidade</button>
        </form>
    </div>
</div>

Controller:

namespace Powerzone\Http\Controllers;

use Illuminate\Http\Request;
use Powerzone\Modality;
use Powerzone\Image;

class ModalityController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $modalities = Modality::all();
    return view('/dashboard/modality/index')->with('modalities', $modalities);
}

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

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $input = $request->all();
    $modality = new Modality($input);
    $modality->save();

    $image = new Image();
    $image = $request->image;
    $image->name = $image->getClientOriginalName();
    $image->imageable_id = $modality->id;
    $image->imageable_type = Modality::class;
    $path = $request->file('image')->store('public');
    $image->save();
}

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

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

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

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}
}
  • 1

    $image = new Image(); $image = $request->image; these two lines summarizes your error, if you create an instance of the class Image() then overwrites with $request->image or failed to access any method? most likely you will have to rename the variable $image after creating the class instance.

  • Yes, the problem was that, the new Image was before the two lines below.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.