Laravel update type error

Asked

Viewed 978 times

1

Is appearing the error:

Type error: Argument 1 passed to Illuminate Database Eloquent Model::update() must be of the type array, Object Given, called in /var/www/html/Laravel/app/Http/Controllers/Propertycontroller.php on line 89

Controller:

<?php

namespace Laravel\Http\Controllers;

use Laravel\Property;
use Illuminate\Http\Request;
use Laravel\Http\Controllers\Controller;

class PropertyController extends Controller
{

public function __construct()
{
    $this->middleware('auth');

}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $properties = Property::all();
    return view('/property/index')->with('properties', $properties);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('/property/create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{   
    $property = new Property;
    $property->nome = $request->nome;
    $property->proprietario = $request->proprietario;
    $property->valor = $request->valor;
    $property->save();
    return redirect()->action('PropertyController@create');
}

/**
 * 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)
{
    $property = Property::find($id);
    return view('/property/edit')->with('property', $property);
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $property = Property::find($id);
    $property->update($request);
    return redirect()->action('PropertyController@index');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $property = Property::find($id);
    $property->delete();
    return redirect()->action('PropertyController@index');
}
}

Route:

<?php

Route::get('/', function () {
return view('auth/login');
});

Route::get('property/index', 'PropertyController@index'); 
Route::get('property/create', 'PropertyController@create');
Route::post('property/store', 'PropertyController@store');
Route::get('property/show', 'PropertyController@show');
Route::get('property/edit/{id}', 'PropertyController@edit');
Route::post('property/update/{id}', 'PropertyController@update');
Route::get('property/destroy/{id}', 'PropertyController@destroy');

Form:

@extends('app')

@section('content')


<form method="post" action="/property/update/{{$property->id}}"       class="container">

<input type="hidden" name="_token" value="{{ csrf_token() }}">

<div class="form-group">
<label for="exampleInputEmail1">Nome</label>
<input type="text" class="form-control" name="nome" value=" {{$property->nome}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Proprietário</label>
<input type="text" class="form-control" name="proprietario" value="{{$property->proprietario}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Valor</label>
<input type="text" class="form-control" name="valor" value=" {{$property->valor}}">
</div>

<button type="submit" class="btn btn-default">Alterar</button>
</form>
@endsection

1 answer

1


The problem of your code, which was being passed a class Request, but the method requires a array with values failed to call $request->all() and failed to configure your model with fillable which is the array of fields that can be updated or created (create).

In the there are ways to make updates to the table:

Model Property

class Property
{
    protected $primaryKey = 'id';
    protected $fillable = array('valor','proprietario','nome');
}

1) Manner:

public function update(Request $request, $id)
{
    $property = Property::find($id);
    if ($property)
    {
        $property->fill($request->except(['_token'])); 
        $property->save();
    }
    return redirect()->action('PropertyController@index');
}

in that item the model needs to be configured Mass Assignment, to know the relation of fields that can be updated with this technique (fill)

protected $fillable = ['valor','proprietario','nome'];

Observing: in that way the method save() it is mandatory to be called to save the data in the table.


2) Way:

public function update(Request $request, $id)
{
    $property = Property::find($id);
    if ($property) 
    {
        $property->update($request->except(['_token']));
    }
    return redirect()->action('PropertyController@index');
}

3) Way:

public function update(Request $request, $id)
{
    $property = Property::find($id);
    if ($property)
    {
        $property->valor = $request->get('valor');
        $property->proprietario = $request->get('proprietario');
        $property->nome = $request->get('nome');
        $property->save();
    }
    return redirect()->action('PropertyController@index');
}
  • Now you’re giving msg: Massassignmentexception in Model.php line 445: _token

  • @Marcelo then use like this $request->except(['_token']) I’ve already done the editing

  • @Marcelo if given error and more fields add the in the array $request->except(['_token','outro nome']); and you set up $fillable of your model?

  • Using the second way returned: name

  • @Marcelo I did not understand, next put in your question your model and also the structure of your table please?

  • My model is empty.

  • 1

    It was the model, I put protected $fillable = array('name', 'proprietary', 'value'); and it worked.

  • @Marcelo never use model no configuration, made the edit in reply.

Show 3 more comments

Browser other questions tagged

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