Error sending form with if:Lse in the Standard

Asked

Viewed 149 times

0

I am making a small form with option to upload files with Windows and I ran into a problem at the time of submitting. The following problem occurs: When sending the uploaded form it sends and stores normally in the database, but without upload file it returns the error Call to a member function extension() on null. Can someone help me?

Routes:

Route::post('/gravar', 'ManifestationController@store');  

Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Manifestation;
use App\Models\State;
use App\Http\Requests\ManifestationFormRequest;
use RealRashid\SweetAlert\Facades\Alert;

class ManifestationController extends Controller
{

private $manifestation;

public function __construct(Manifestation $manifestation)
{
    $this->manifestation = $manifestation;
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $category = DB::table('categories')->pluck('id');
    $response = ['Email', 'Telefone', 'Fax', 'Não Informado'];
    $states = DB::table('states')->pluck('letter');

    return view('site.index', [
        'category'  => $category,
        'response'  => $response,
        'states'    => $states,
        ]);
}


/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(ManifestationFormRequest $request)
{
    //Nome do arquivo a ser armazenado
    $name = date('dmYHis');

    //armazena form em dataForm
    $dataForm = $request->except('_token');

    //Insere Formulário no Banco
    $insert = $this->manifestation->insert($dataForm);

    if($request->has('upload'))
    {
        //Pega extensão do arquivo original
        $extension = $request->upload->extension();
        //Armazena arquivo em Storage/app/uploads e renomeia.
        $upload = $request->upload->storeAs('uploads', $name.'.'.$extension);
    }

    Alert::success('Enviado...', 'Obrigado pela sua contribuição');
    return redirect()->route('index');
}
}

Form:

<!-- #formulario --> 
<form method="post" action="/gravar" enctype="multipart/form-data">
    {{csrf_field()}}

    <div class="form-row">
        <div class="form-group col-md-3">
            <label for="categorias">Escolha o tipo</label>
            <select id="categorias" class="form-control form-control-sm" name="FK_cat_id">
                @foreach ($category as $categories)
                    <option value="{{$categories}}">{{$categories}}</option>
                @endforeach
            </select>
        </div>
        <div class="form-group col-md-3">
            <label for="response">Receber resposta via:</label>
            <select class="form-control form-control-sm" name="response">
                @foreach ($response as $resposta)
                    <option value="{{$resposta}}">{{$resposta}}</option>
                @endforeach
            </select>
        </div>
        <div class="col-md-6 text-right">
            <input type="checkbox" name="anonimo" id="anonimo" onchange="setaCampos(this.checked)" />
            Deseja anonimato?
        </div>
    </div>

    @if (count($errors) > 0)
    <div id="ERROR_COPY" style="display: none;" class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{$error}}</li>    
            @endforeach
        </ul>
    </div>
    @endif
    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="name">Nome</label>
        <input type="text" id="name" style="text-transform:uppercase" name="name" class="form-control" placeholder="Nome completo" value="{{ old('name') }}">
        </div>
        <div class="form-group col-md-6">
            <label for="email">Email</label>
            <input type="email" name="email" class="form-control" id="email" placeholder="[email protected]" value="{{ old('email') }}">
        </div>
    </div>

    <div id="hideInf" >
        <div class="form-row">
            <div class="form-group col-md-6">
                    <label for="rg">RG</label>
                    <input type="text" name="rg" class="form-control" id="rg" value="{{ old('rg') }}">
            </div>
            <div class="form-group col-md-6">
                    <label for="cpf">CPF</label>
                    <input type="text" name="cpf" class="form-control" id="cpf" value="{{ old('cpf') }}">
            </div>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="address">Endereço</label>
            <input type="text" name="address" class="form-control" id="address" placeholder="Av, Rua, QD..." value="{{ old('address') }}">
        </div>
        <div class="form-group col-md-3">
            <label for="phone">Telefone</label>
            <input type="tel" name="phone" class="form-control" id="phone_with_ddd" placeholder="(xx) ____-____" value="{{ old('phone') }}">
        </div>
        <div class="form-group col-md-3">
            <label for="fax">Fax</label>
            <input type="tel" name="fax" class="form-control" id="fax_with_ddd" placeholder="(xx) ____-____" value="{{ old('fax') }}">
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="city">Cidade</label>
            <input type="text" name="city" class="form-control" id="city" placeholder="Cidade em que reside" value="{{ old('city') }}">
        </div>
        <div class="form-group col-md-4">
            <label for="state">Estado</label>
            <select id="state" name="state" class="form-control" value="{{ old('state') }}">
                    @foreach ($states as $state)
                        <option selected>{{$state}}</option>
                    @endforeach
            </select>
        </div>
        <div class="form-group col-md-2">
            <label for="cep">CEP</label>
            <input type="text" name="cep" class="form-control" id="cep" value="{{ old('cep') }}">
        </div>
    </div>

    <div class="form-row col-md-6 col-xs-6">
        <div class="form-group">
            <label for="file">Enviar Arquivo</label>
            <input type="file" name="upload" class="form-control-file" value="{{ old('upload') }}">
        </div>
    </div>

    <div class="form-group">
        <label for="manifestation">Escreva Sua Mensagem</label>
        <textarea name="manifestation" class="form-control" id="manifestation" rows="12">{{ old('manifestation') }}</textarea>
    </div>

    <button type="submit" class="btn btn-success">Enviar</button>
    <button type="reset" class="btn btn-danger">Limpar</button>
</form>

error line:

76 $Extension = $request->file('upload')->Extension();

  • which line the error?

  • 76 $extension = $request->file('upload')->extension();

1 answer

3


The method does not exist because you used

->extension()

the wrong way, the right way would be:

if ($request->hasFile('upload'))
{
    $ext = $request->upload->extension();
}

I might still use $request->upload->getClientOriginalExtension(), and looking at your API makes it easy to see all methods.

  • Hello @Virgilio, I did as you suggested, healed the error but it still does not enter the condition if you have file in the form. Any suggestions? Thanks! I’ve already edited the topic with the changes.

  • 1

    @John is hasFile is now fixed in the edit

  • 1

    Aaah ok, I’ll test back here.

  • thank you very much. All right here!

Browser other questions tagged

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