Laravel - parse_url() expects Parameter 1 to be string, array Given

Asked

Viewed 1,047 times

-1

I’m encountering an error while trying to register data in the database.

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

Productocontroller.php

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

product-formulary.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 this way, using create. I’ve never had a problem. You’re pointing something at me $components = parse_url($uri); in the Request.php 363 file

  • 1

    But do not register in the bank using the variable $request...

  • 1

    Sorry my friend. My mistake.

3 answers

2

I wasn’t going through the model. Now it worked.

Product.php

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

Productocontroller.php

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

public function cadastrar(Request $request)
{
    $dados = $request->except('_token');
    $conf = $this->produto->create($dados);
}
  • Ah yes, now $this->produto->create makes some sense (instead of $request::create) :)

  • Yes, my friend! I’m new to Laravel, I’m getting acquainted with him and this MVC issue. I’ll read the documentation. Thank you so much for your help.

2

There are several ways to save in the bank. One of them can be like this 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();
 }
  • I’ll read the documentation to know the difference between create and save. Thanks for the help.

  • Regardless of the difference this way does not record in the bank no?

  • 1

    Save yes. I’ve seen save in some tutorials, but I didn’t remember. I’ll use it.

  • When you use a form with little data you can use save. More when using a form with a lot of data on the user side, it’s good to use create because of csrf attacks. And the input that contains the token, does not worry that it does not go into the database. It only serves for authentication scheme to bar the csrf attack.

  • Ah, got it!!! The form I posted above only had 3 input, but it will have much more. I already know which one to use. Thanks

1

Are you sure you know what these methods use?

The Request::create is a method to request a URL as if "was a browser calling", an example of a call via POST:

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

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

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

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

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

This will rertonar one array

In other words, it’s a separate call and there’s no point in passing a array (which is the return of except) as if it were a string of the route, as documented https://laravel.com/docs/5.5/requests#retrieving-input

Apart from the fact that none of these methods have any relation as database or models of Laravel, they only do is to take the values of the Inputs and the create only creates a new request.

Browser other questions tagged

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