21447

Can eloquent ignore irrelevant data in Laravel 4

Question:

I have a form that accepts data which will be used to create two new database table entries. The form takes both the users details and their address. The user details will be stored using the User::create(Input::all()) method into the users table, and the address details will be stored using the Address::create(Input::all()) method into the addresses table of the database.

The issue I'm currently having is that Eloquent is complaining that street, city, country etc do not exist on the users table. This is true, that data is to be used for the address side of things.

Is there any way to have eloquent ignore irrelevant data in the Input::all() array when it's passed to the create methods?

P.s. I'm aware that mass-assignment isn't a good idea, I'm only using it here to simplify my question.

Answer1:

Sure enough you can use $fillable array in your model to declare fields allowed for mass-assignment. I believe this is the most sufficient solution in your case.

class User extends Eloquent { protected $fillable = [ 'first_name', 'last_name', 'email' ]; }

Answer2:

Have you tried looking at Input::only('field1','field2',...);, or even Input::except('field3')? They should be able to accomplish what you are looking for.

Source: <a href="http://laravel.com/docs/requests" rel="nofollow">http://laravel.com/docs/requests</a>

Answer3:

You'll have to unguard that model using these <a href="http://laravel.com/docs/eloquent#mass-assignment" rel="nofollow">http://laravel.com/docs/eloquent#mass-assignment</a> and then manually unset those values before you execute save(). I highly recommend using a form object or something similar to complete this kind of service for you outside of your model since it's safer and usually clearer to intended behavior.

Answer4:

@cheelahim is correct, When passing an array to Model::create(), all extra values that aren't in Model::fillable will be ignored.

I would however, <strong>STRONGLY RECOMMEND</strong> that you do not pass Input::all() to a model. You really should be validating and verifying the data before throwing it into a model.

Recommend

  • Cannot merge on an Eloquent collection in Laravel
  • Laravel updating eloquent event: getting the data
  • how can i do UIView animateWithDuration in viewDidLoad? ios7
  • With knitr, preserve chunk options when purling chunks into separate files
  • Create new eloquent instance with relationships
  • C++ std::set comparator
  • Prolog Query - Trying to understand how this result happens
  • Align Excel cell to center VB - xlCenter is not declared
  • @Autowired for @ModelAttribute
  • Does the failbit effect the call ignore on cin?
  • Django: ORDER BY DESC on FloatField puts null values top?
  • Angular2 ag-grid datasource not working
  • Generate a unique string based on a pair of strings
  • What causes the runtime difference in this trivial fortran code?
  • OSX - always hide certain files
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Laravel: Getting Session ID oddly truncates when using foreach
  • How to assign byte[] as a pointer in C#
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Tamper-proof configuration files in .NET?
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Do I need to reset a Perl hash index?
  • Xcode 4 NSLog Macro link in Xcode 3
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • javascript inside java/jsp code
  • WinForms: two way TextBox problem
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Cannot Parse HTML Data Using Android / JSOUP
  • Benchmarking RAM performance - UWP and C#
  • C# - Getting references of reference
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?