Form Model Binding in Laravel 4 - laravel

I am building an edit details view and the basic purpose is to update the values entered previously. I read that the best way to do this would be using Form Model Binding. Nevertheless, this seems very risky as the interaction is happening directly on the model (and validation is taking place in controller.
What is your input on this? Is it the best approach to do edit data form?

Model form binding doesn't mean that it will automatically update the existing model with the data once it's posted -- you still need to manually write that logic. It's simply a way for when using laravels Form Builder i.e. Form::text('username') to attempt to automatically map the 'username' field and use it's value in the text. That's it. No data is automatically updated so simply using model form-binding isn't 'risky'
Instead of doing
Email: {{ Form::text('email', user.email) }}
Username: {{ Form::text('username', user.username) }}
You can simplify this by doing:
{{ Form::model(user) }}
Email: {{ Form::text('email') }}
Username: {{ Form::text('username') }}

Related

How to pass Vue.js variable to Laravel Blade route helper

For example, I have a link to an other post like this in my template
My post
I want to be able to use the post object within the route helper to use the id.
Is there any syntax for this use case?
Or is there an other way of doing this?
Actually I had similar problems to solve this kind of cases. Your question related with Vue exactly, but the method below you can use for your case as well. Anyway you can't execute JS and PHP at the same time, cuz they're working at different sides. But as I also liked to have all routes with their aliases, I thought this approach.. You can imitate something like this:
Route::get('/', 'PostController#all')->name('all'); // all posts page
Route::get('post', 'PostController#all')->name('all_page'); // THIS IS THE THING (one additional route), WHICH WILL TAKE RESPONSIBILITY ON CASE, WHEN post_id WILL BE EMPTY
Route::get('post/{post_id}', 'PostController#post')->name('post');
This method will allow you to use the 1st and 3rd routes as normally, and as mixed too in the different places on your app like this:
{{ route('post.all') }}
{{ route('post', ['post' => $post_id]) }}
{{ route('post') }}/#{{ post.id }}
In scripts you can implement the approach like this:
let someUrl = "{{ route('post') }}/" + postObj.id;
In the view you can implement the method like this:
My post #{{ post.id }}

Form model binding in Laravelcollective, a default checked checkbox never shows unchecked

Laravel 5.5, Laravelcollective 5.4
On my edit page the checkbox is always showing checked, regardless of state in database. (Have confirmed it's working as intended without default checked.)
I use the same form fields for my create route, and would like the default to be checked.
{{ Form::model($client, ['route' => ['clients.update', $client->id], 'method' => 'patch']) }}
{{ Form::checkbox('active', 'Yes', true) }}
{{ Form::close() }}
According to the documentation on form model binding
So, the priority looks like this:
Session Flash Data (Old Input)
Explicitly Passed Value
Model Attribute Data
Note that the explicitly Passed Value seems to not be a default fallback but the actual value that will be used if provided (regardless of model). Therefore that seems to be the intended behaviour.
Update: As discussed in the comments, a solution to allow fallback values in the case where there's no model set is to use:
{{ Form::checkbox('active', 'Yes', isset($client)?null:true) }}

How to send authenticate user laravel to vue props?

i want to send authenticate user data to props vue. then i tried this:
:user="{{ Auth::user() }}" but got an errors.
Did you try to convert the user object to JSON
:user="{{ json_encode(Auth::user()) }}"
Actually, there is one tini tiny mistake here! You are using {{ Auth::user() }} which is JSON or Array! If you want to store User's ID in user Attribute, you have to use {{ Auth::user()->id }}.
If you want to Authenticate user first, you can use if Condition! For example,
#if(Auth::user())
// Your Markup Code
#endif
Let me know if it helps!
Use It like this
<post-comment user="{{ Auth::user() }}"></post-comment>
Using : is reserved for data binding for properties from data object aka Vue App Model / State.
I guess you are using this into Laravel Blade.
Dynamic Props - https://v2.vuejs.org/v2/guide/components.html#Dynamic-Props
However your way would works only If Auth::user() method return the Boolean (true/false).Otherwise It would throw an error in Browser Console.

In a Form Model Binding form, how do I access the variable without using a helper?

I am building a form using Form Model Binding via the Laravel Collective HTML package. The documentation (https://laravelcollective.com/docs/5.2/html#form-model-binding) boasts how the model's value is conveniently available with the value being set in the priority order of:
Session Flash Data (Old Input)
Explicitly Passed Value
Model Attribute Data
This is super useful because if a user has changed multiple fields, but 1 of them fails validation, you don't want all the other fields being reset to original value when they get thrown back to the form page with a helpful Message Bag of errors to give them a chance to correct their invalid input.
It's all very well using the Form::label, Form::text and Form::select helpers to leverage this lovely feature but what if you just want to access that convenient variable directly to do something a bit left-field?
Here's what I have tried...
{{ Form::model($user, array('route' => array('user.update', $user->id))) }}
{{ Form::label('first_name', 'First Name:', array('class' => 'address')) }}
{{ Form::text('first_name') }}
{{ Form::value('first_name') }} <-- No
{{ Form::session('first_name') }} <-- Nope
{{ Form::input('first_name') }} <-- Still no
{{ Form::attribute('first_name') }} <-- Absolutely not
{{ Form::close() }}
Help me, what is the method I am looking for?
The documentation hints at the ability to get the thing I want here: https://laravelcollective.com/docs/5.2/html#custom-macros where it says it's "easy" to define your own custom Form class helpers called "macros" but it doesn't actually tell you where you would write the code for this or where in the code you can look if you want to poke around and learn more about the FormBuilder class?
The method for doing this is Form::getValueAttribute('first_name') and the location for poking around to learn more is /vendor/laravelcollective/html/src/FormBuilder.php

How to get submitted data in the same form in laravel

I am using laravel 5.0.
I have a form in my project. When I submit a form it return with error. But not getting the posted data.
I have used {{ old('name') }}. It is working with perfect. But I want to make a condition with the post data. If I use
#if( {{ old('name') }} == "praveen" )
it shows error.
I have seen Request::get('name') method one time. But I cannot remember that correctly. Please give me a solution.
Do not print in your condition. Just this:
#if(old('name') == "praveen")
// code here
#endif
to get submitted data in the same form you should use
return Redirect::back()->with('name');

Resources