Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : add new row in model table

I have a user model defined, and I'm trying to add a new user in my database using a form, what is the best and fastest way to do it, I have read something about model forms binding but I think it's just for updating not for adding new rows.

I'm new in Laravel and couldn't find some good tutorials, I must recognize that Laravel documentation is really poor, so any links to some good and well explained tutorials are welcomed. Thank you

like image 634
Tarik Mokafih Avatar asked Apr 07 '14 22:04

Tarik Mokafih


People also ask

How do I create a new row in Laravel?

You can create a new row as simple as User::create(Input::all()) when your $fillable is properly set in the model.

What is withTrashed in Laravel?

Laravel, “withTrashed()” linking a deleted relationship With Eloquent we can define the relation easily. If the user gets deleted, and on the User model we use the SoftDeletes trait, you can use withTrashed() method here.

What is fillable in Laravel?

The fillable property is used inside the model. It takes care of defining which fields are to be considered when the user will insert or update data. Only the fields marked as fillable are used in the mass assignment. This is done to avoid mass assignment data attacks when the user sends data from the HTTP request.


1 Answers

Assumed that, you have a User model (app/models/User.php) the one came with Laravel by default, which may look like this:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
    
    protected $table = 'users';
    protected $hidden = array('password');

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }
    
    public function getAuthPassword()
    {
        return $this->password;
    }

    public function getReminderEmail()
    {
        return $this->email;
    }
}

Now, from a controller (Basically) you may use somethng like this:

$user = new user;
$user->username= 'Me';
$user->email = '[email protected]';
// add more fields (all fields that users table contains without id)
$user->save();

There are other ways, for example:

$userData = array('username' => 'Me', 'email' => '[email protected]');
User::create($userData);

Or this:

User::create(Input::except('_token'));

This requires you to use a property in your User model like this:

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $fillable = array('username', 'email');

    // Or this,  (Read the documentation first before you use it/mass assignment)
    protected $guarded = array();

}

Since, you are still new to Laravel you may use first example and read about Mass Assignment, then you may use the second one if you want.

Update:

In your controller, you may use Input::get('formfieldname') to get the submitted data, for example:

$username = Input::get($username);

So, you can use these data like this:

$user = new User;
$user->username= $username;

Or directly you can use:

$user->email = Input::get($email);
$user->save();

In the form, you have to set the form action, where you'll submit the form and in this case you have to declare a route, for example:

Route::post('user/add', array('as' => 'user.add', 'uses' => 'UserController@addUser'));

Then in your controller you have to create the method addUser, like this:

class UserController extends addUser {
    
    // other methods
    
    public function addUser()
    {
        $user = new user;
        $user->username = Input::get('username');
        $user->email = Input::get($email);
        $user->save();
    }
}

In your form you may use this:

Form::open(array('route' => 'user.add'))

Read the documentation properly, you can do it easily.

like image 163
The Alpha Avatar answered Oct 25 '22 18:10

The Alpha