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)
{
    //
}
}
						
$image = new Image(); $image = $request->image;these two lines summarizes your error, if you create an instance of the classImage()then overwrites with$request->imageor failed to access any method? most likely you will have to rename the variable$imageafter creating the class instance.– novic
Yes, the problem was that, the new Image was before the two lines below.
– Marcelo