user3100193
user3100193

Reputation: 561

store and update in Laravel - which Request to use?

I'm new to Laravel and I have problems with storing and updating a Model.

Here is my store method

 public function store(Request $request)
{

    $input = Request::all();

    Klijent::create($input);

    return redirect('klijenti');

}

And I have to include use Request; to make it work.

Here is my update method

    public function update(Request $request, $id)
{
    //

    $klijent = Klijent::find($id);

    $klijent->update($request->all());

    return redirect('klijenti/'. $id);

}

And I have to include use Illuminate\Http\Request; to make it work.

But if I don't use first one I get this error when using store method:

Non-static method Illuminate\Http\Request::all() should not be called statically, assuming $this from incompatible context

If I don't use second one, I get this error when I use update method:

Call to undefined method Illuminate\Support\Facades\Request::all()

If I use both of them I get this error:

Cannot use Illuminate\Http\Request as Request because the name is already in use

Upvotes: 2

Views: 19646

Answers (1)

wogsland
wogsland

Reputation: 9508

You need to make the call to non-static methods like

$input = $request->all();

in your first function. The second error is because Illuminate\Support\Facades\Request does not have an all method to call. The third error is a namespace conflict, because you cannot have two classes with the same name in PHP.

Upvotes: 9

Related Questions