Eloquent Laravel 4.2 setting Null at equal values

Asked

Viewed 95 times

0

I’m having trouble with the ORM Eloquent where I give update in a table and a column of that table is the same as mine input he leaves null.

Example:

Table

name    idade
joão    10

using the command

$user->update($input);

being $user the users table model, if I upgrade the age to 10 again it leaves age as null, but using the command

DB::table('users')->where('id', $id)->update($input);

works properly.

Controller

<?php

use Illuminate\Database\Eloquent\ModelNotFoundException;
use \Carbon\Carbon;

class Users extends BaseController implements InterfaceController {

    use \Traits\UserTrait;

    public
        $rules = [
            ‘idade’ => 'numeric'
        ],

    public $model;

    public function __construct(Users $model)
    {
        $this->model = $model;
     }

    public function update($id) {
        $user = $this->model->findOrFail($id);

        $input = Input::all();

        $user->update($input);
    }
}
  • Put also your controller, also put what is $input? I didn’t understand the first part since it worked the second

  • I put the example of the controller. I need the first function because of the type of answer. The first command returns all the data of the updated object, the second returns only 1(success) or 0(error).

  • It makes no sense. If I exchange $user->update($input) for DB::table('users')->Where('id', $id)->update($input) it works normal. I didn’t change anything in the users' model. I needed to create the update method to change what it does after the update. In other cases it works normal.

  • I understood I made a mess, what returns: $input = Input::all();???

  • Input::all() would be the array that was sent by the POST method via front-end form using ajax.

  • I need to know what the values are var_dump in Input::all()?

  • array (size=2) 'name' => string 'John Test' (length=10) 'age' => string '10' (length=2)

  • Post your model?

Show 3 more comments

1 answer

0

I was really confused when you posted a method that is already implemented on ORM Eloquent and does not work. Now observing well you did not follow the naming of names that the Laravel leaves as an example, you created a Controller without using the final name, example:

  • Userscontroller
  • Carscontroller
  • Peoplecontroller

this is a way to standardize names and codes referring to a certain area, it is not a mandatory rule, but it is a good practice to follow what the Framework leaves as an example in your documentation and clicking here it is clear that

So whatever happened to your code giving problem, you created a model Users and a controller Users, that is, is shocking names and so should this with problems in recording.

The ideal of your code is to rename your controller for UsersController:

<?php

use Illuminate\Database\Eloquent\ModelNotFoundException;
use \Carbon\Carbon;

class UsersController extends BaseController implements InterfaceController 
{
    use \Traits\UserTrait;

    public $rules = ['idade' => 'numeric'];

    private $model;

    public function __construct(Users $model)
    {
        $this->model = $model;
    }

    public function update($id) 
    {
        $user = $this->model->findOrFail($id);
        $input = Input::all();
        $user->update($input);
    }
}

although even so, I don’t have your model to know what is the best way to exemplify for you, so the tip is, when you ask a question, put everything that is involved, model, controller, what was sent from viewfor the controller, etc..

Browser other questions tagged

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