Laravel - parse_url () expects parameter 1 to be string, array given

-1

I'm encountering an error when trying to register data in the database.

ErrorException (E_WARNING)
parse_url() expects parameter 1 to be string, array given

ProductController.php

public function cadastrar(Request $request)
{
    $dados = $request->except('_token');
    $result = $request::create($dados);
}

product-form.blade.php

<form method="post" action="{{route('cadastrar')}}">
    {!! csrf_field() !!}

    <input type="text" name="produto">
    <input type="text" name="descricao">
    <input type="text" name="modelo">
    <input type="submit" value="Ok">

</form>

I've always done it that way, using create. I've never had problems. Is it pointing to something in $components = parse_url($uri); in the Request.php file 363

    
asked by anonymous 13.10.2017 / 16:33

3 answers

2

I was not going through the model. Now it worked.

Product.php

class Produto extends Model
{
    protected $fillable = ['produto', 'descricao', 'modelo'];
}

ProductController.php

public function __construct(Produto $produto)
{
    $this->produto = $produto;
}

public function cadastrar(Request $request)
{
    $dados = $request->except('_token');
    $conf = $this->produto->create($dados);
}
    
13.10.2017 / 16:56
2

There are several ways to save to the bank. Some of them can be that way too.

 use App\namespace\Produto;
 public function cadastrar(Request $request)
 {
   $dados = new Produto;
   $dados->produto = $request-> produto;
   $dados->descricao = $request-> descricao;
   $datos->modelo = $request-> modelo;
   $dados->save();
 }
    
13.10.2017 / 17:04
1

Are you sure you know how to use these methods?

Request::create is a method for requesting a URL as if it were a browser calling , an example of a POST call:

$request = Request::create('minha/rota/existente', 'POST', [
             'foo' => 'bar'
           ]);

$response = Route::dispatch($request);

var_dump($response);//Pega o resultado da reposta

While the $request->except() returns a group of input values, except for a specific one, the correct use would be:

$request->except(['_token']);

This will revert to a array

In other words, it is a separate call and there is no point in passing a array (which is the return of the exception) as if it were a string of the route, according to the documentation link

Since none of these methods are related to Laravel's database or models, they only take the value of the INPUTs and the create only creates a new request.

    
13.10.2017 / 16:54