Type error in update Laravel

1

Error is appearing:

  

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
    
asked by anonymous 07.11.2016 / 00:25

1 answer

1

The problem with your code, which was being passed a Request class, but , the method requires a array with missing values calling $request->all() and you have not configured 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: / p>

Model Property

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

1) Way:

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 this item model needs to be configured Mass Assignment , to know the list of fields that can be updated with this technique ( fill )

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

Note: In this way the save() method is required 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');
}
    
07.11.2016 / 00:45