L
L
lynnikvadim2015-12-01 17:02:53
Laravel
lynnikvadim, 2015-12-01 17:02:53

How can you change part of the data in the resulting model?

If you write code like this:
Organization::create($request->all());
in the controller.
Can I somehow change part of one value?
For example, there is a name field, and add ";" to it at the end.
and then to send on records in a DB.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Muhammad, 2015-12-01
@lynnikvadim

$organization = Organization::create($request->all());

$organization->field .= ';';
$organization->save();

and even better:
$data = $request->all();
$data['field'] .= ';';

$organization = Organization::create($data);

PS: don't forget about validation

T
Tesla, 2015-12-02
@Tesla

You can use the Eloquent mutator to keep the controller clean.

F
Flasher, 2015-12-03
@Flasher

$post = new Post($request->all());
Don't forget about $fillable in the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question