Code Monkey home page Code Monkey logo

Comments (5)

alexeymezenin avatar alexeymezenin commented on July 30, 2024 1

Thank you for the recommendation. $request->all() is safe if you're using validation and the $fillable property properly.

In newer versions of the Laravel framework you can use $request->validate(), I'll add it to the repository a bit later.

from laravel-best-practices.

gpedro avatar gpedro commented on July 30, 2024

well, idk if i getting a mistake or something. if i'm wrong, please tell me where i getting wrong ... please checkout my demo project. he is a collaborative news submission.

setup: composer install && php artisan migrate --seed

paths:

  • home - GET /
  • news form - GET /news/create

steps to reproduce:

  • go to home and look the news inserted by the factory.
  • go to news form and submit a news
  • back to home and confirm is the news created on last step is not listed.
  • go to news form and fill all fields and inspect element and add a field named published with value 1 and submit
  • back to home and confirm is the news created on last step is listed.

notes:

  • published field should be in fillable because we can have a feature to publish.

from laravel-best-practices.

gpedro avatar gpedro commented on July 30, 2024

an possible solution is set value manually on controller to override. but if i have a default value on database i can just pass null.

#before
News::create($request->all());

#after
$data = $request->all();
$data['published'] = false;
News::create($data);

/ping @alexeymezenin

from laravel-best-practices.

gpedro avatar gpedro commented on July 30, 2024

/ping @alexeymezenin

from laravel-best-practices.

jyhsu2000 avatar jyhsu2000 commented on July 30, 2024

Just remove published from $fillable is okay.
$fillable is for mass assignment.
Setting values of non-fillable properties in this way still works.

$news->published = true;
$news->save();

from laravel-best-practices.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.