Laravel form model binding for many to many select input - php

I have a many to many relationship between clients and assets.
For my edit client form I have multi-select box with which the user can select several assets to attach to a client. The form is using model-binding so it automatically populates the fields with the existing client data.
The model binding works for all fields in the form except the multi-select. Here is a snippet from my view:
{{ Form::model($client, ['route' => ['clients.update', $client->id], 'class' => '', 'method' => 'put']) }}
{{ Form::label('name', 'Name', $label_attributes) }}
{{ Form::text('name', null, array('class'=>'form-control')) }}
{{ Form::label('assets', 'Client Benchmarks (Select multiple)', $label_attributes) }}
{{ Form::select('assets[]', $assets, null, array('multiple' => true, 'class' => 'form-control')); }}
When I submit the form the relationship saves the data successfully via the sync method in my controller (update method):
$client = Client::find($id);
$client->name = Input::get('name');
$assets = Input::has('assets') ? Input::get('assets') : array();
$client->assets()->sync($assets);
$client->save();
Also, if I output the client assets directly they are accessible in the model collection in my form view:
<?php print_r($client->assets); ?>
How can I get the form to populate the select box with the existing selections?

echo Form::select('assets[]', $assets, array(1,2), array('multiple' => true));
As far as I know it won't set all selected values by default for you even when using form
model binding
This is what the native formBuilder method looks like:
public function select($name, $list = array(), $selected = null, $options = array())

Related

Update specific table field on another Table

I need to update specific one id / row on a separate field on another table. how is the logic to that? so far i have this pseudo code sql query.
table name: settings
current table: aircrafts
UPDATE settings SET description id = 4
i need to update id 4 on my database in database settings
how can i integrate this code into laravel syntax?
assuming i have this controller
public function editAirReg(Request $request, $id)
{
$descr = $request->input('descr_id');
DB::update('update settings set description = ,'$descr', where id = 4',[$name,$id]);
return redirect('/admin/aircrafts')->with('success', 'Settings Updated');
}
and also this view
{!! Form::open(['action' => 'Admin\AircraftsController#getdata', 'method' => 'POST']) !!}
<input type="hidden" class="form-control" name="settings" value="{{$aircraft->air_line}}">
{{Form::submit('BIND', ['class'=>'btn btn-primary btn-block btn-lg', 'name'=>'submit'])}}
{!! Form::close() !!}
First of all, getData() doesn't make sense if you're updating something in mysql. It should be called store() or save or whatever you want to call so that the function name is appropriate.
Let's change the form so that it can pass the id to the controller.
{ Form::open(array('action' => 'Admin\AircraftsController#getdata', $id)) }}
{{ Form::hidden('settings', $aircraft->air_line) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
What I did is that change your input to the laravel syntax. Also make sure you pass the id from form to controller. take a look at my example. look where I put $id field.
Now in the controller, this is what you do.
$setting = Setting::findOrFail($id); // you must have a model or you can use DB facade
$setting->description = $request->settings; //name that you called it in form
$setting->save();

Laravel 5.5 correct way for a CRUD form select

So I have this CRUD where I use the same form to create and edit entries.
I need in several form selects, when creating (no data present for that particular field yet) my select to show the placeholder, but when editing, my select to show whatever is stored on database for that particular id filed. So I have:
Controller:
...
public function create()
{
$house = houses::pluck('name', 'id');
//$thisclient = null;
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
return view('prospects.create', compact('house', 'clients', 'reps'));
}
...
public function edit($id)
{
$house = houses::pluck('name', 'id');
//$thisclient = user::whereId($id)->first();
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
$prospect = Prospect::findOrFail($id);
return view('prospects.edit', compact('prospect', 'house', 'clients', 'reps'));
}
and my view form:
Working for create:
{!!Form::select('client_id', $clients, null, ['class' => 'form-control', 'placeholder' => 'Please Select'] ) !!}
Working for edit:
{!! Form::select('client_id', $clients, $prospect->client_id, ['class' => 'form-control'] ) !!}
I'm having 2 troubles here, if I have null as my selected field, it won't bring the selected data on edit, if I have $prospect->client_id , it will return a error on create as there's no data yet.
I tried to solve this by creating a variable $thishouse on controller and passing it to view on return view('prospects.create', compact('house', 'thisclient','clients', 'reps')); and view Form::select('client_id', $clients, $thisclient, ['class' => 'form-control'] ) !!} but seems a bit dirty whne having several form selects...
The second trouble is if I leave a placeholder on Edit, it will show the placeholder, not $prospect->client_id itself.
What's the best and simplest way to achieve all of this and use the same form for create and edit?
Thanks
You can use Form::open and Form::model to create and edit. As an example, you can set in your view:
#if(isset($prospect))
{!! Form::model($prospect, ['action' => ['ProspectController#update', $prospect->id], 'method' => 'patch']) !!}
#else
{!! Form::open(array('action' => 'ProspectController#store', 'method' => 'POST')) !!}
#endif
And then you can create the select like this:
{!! Form::select('client_id', $clients, old('client_id'), ['class' => 'form-control'] ) !!}
So, when you are editing, Laravel will select the attribute from the variable on model function.
And since you are using Laravel 5.5, you could also use #isset instruction.

User Submitted Posts Store function using Laravel

So in addition to my prior topic (thank you guys for helping) routes with compact
I am now facing troubles with my store function, I am getting the error : "Trying to get property of non-object.
This is my store function in my Controller
public function store(Request $request)
{
// validate the data
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
// store in the database
$userpost = new Usp;
$userpost->title = $request->title;
$userpost->body = $request->body;
$userpost->save();
Session::flash('success', 'The blog post was successfully saved!');
return redirect()->route('admin.userposts.show', $userpost->id);
}
This is the view to create userpost(to make it more clear, p.s. the same form with different route ofcourse, works for my simple posts but not for my user submitted posts)
#extends('index')
#section('index-stylesheets')
{!! Html::style('css/parsley.css') !!}
#endsection
#section('content')
<h1>Create New User Post</h1>
<hr>
{!! Form::open(array('route' => 'admin.userposts.store', 'data-parsley-validate' => '')) !!}
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('body', "Post Body:") }}
{{ Form::textarea('body', null, array('class' => 'form-control', 'required' => '')) }}
{{ Form::submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }}
{!! Form::close() !!}
#endsection
#section('index-scripts')
{!! Html::script('js/parsley.min.js') !!}
#endsection
Method of showing the post:
public function show($id)
{
$userpost = Usp::find($id);
return view('admin.userposts.show', compact('userpost'));
}
So the fact is that the problem was not the store method but the routes.
Route::get('/userposts/{id}', 'UserPostsController#show')->name('admin.userposts.show');
Route::get('/userposts/create', 'UserPostsController#create')->name('admin.userposts.create');
Route::post('/userposts/store', 'UserPostsController#store')->name('admin.userposts.store');
When registering the routes in that order, when laravel will iterate over your routes, it will first encounter the show one, and it will therefore take "create" as the id. Therefore, it will go into the show method and it won't find any post that matches, the post being null, you get the error.
So, there are two ways of fixing this.
The first one (the easiest, works in all cases, maybe not the best) is to put the create route before the show route.
The second one, the best in my opinion, is to add a condition to the id (doesn't work in the case of a slug). As the ids are only integers, you get :
Route::get('/userposts/{id}', 'UserPostsController#show')->name('admin.userposts.show')->where('id', '[0-9]+');
Therefore, create won't match the regular expression and it won't go in the show method.
For "resource creations" (storing in database), I wouldn't use a "field-by-field" method.
Instead, I'd do something like this :
$userpost = Usp::create($request->only('title', 'body'));
I feel this is more talkative.
But, it won't work, laravel protects* us against such things. To make it work, you have two options.
(The best option in my opinion)
In your model, add a protected variable called $fillable with all your columns that you allow to mass assign*. In this case you would put :
protected $fillable = ['name'];
(The option if you are sure of what are you doing)
In your model, you can say, hey, I know what I'm doing here, just let me do my stuff without guarding me. In this case you would put :
protected $guarded = [];
Notes :
$request->only('field1', ...) gives you an array of the fields that you want with the fields name as keys, in this case it gives you ['field1' => $request->field1]. In your case it will give you ['title' => $request->title, 'body' => $request->body].
Mass assignment is when you give an array to the model and it puts all attributes to the fields of the array. More informations here https://laravel.com/docs/5.4/eloquent#mass-assignment
When I mean laravel protects us against those things, it does't really protect us because it isn't a bad practice (instead, I find it more readable), but because it does allow you to make mistakes (for exemple, setting fields that don't exist).

Select default value if no old input is set

I am using form model binding and have the following select input :
{!! Form::select('user_id', $users, old('user_id') ?: Auth::id(), ['class' => 'form-control select2 users']) !!}
I would like to accomplish the following:
In the creation form: select the option where the user_id equals the authenticated user's ID, but if there was old input in the session, select that option instead.
In the edit form: always select the option that is stored in the model.
old('user_id') ?: Auth::id() doesn't seem to work when editing, because it always selects the option of the authenticated user and not the one that is stored in the model.
I believe you already have this line,
{!! Form::Model($users, ['route' => ['admin.user.update', $user->id], 'method' => 'PUT']) !!}
then your code should like this:
{!! Form::select('user_id', $users, isset($selectedUser) ? $selectedUser : null) !!}
at Create function do not pass $selectedUser to view but pass the id in your session,and in your edit function you should pass $selectedUser to view ,which is Auth::user()->id

Laravel 4 display list of data into form selection

How do I display a list of books in laravel form builder selection?
BookController.php
$book_names = Book::all();
return View::make('books')->with('book_names', $book_names);
At the moment I only know how do manually input data:
{{ Form::select('book_name', array(
'book1' => 'book1',
'book2' => 'book2',
'book3' => 'book3')
}}
I want to do something like this:
{{ Form::select('book_name', array(
#foreach($book_names as $book_name)
$book_name->name => $book_name->name,
#endforeach
}}
But obviously It won't work..
Meet the lists() method. It allows you to create an array from one or two (key and value) properties of a collection:
$book_names = Book::lists('name');
return View::make('books')->with('book_names', $book_names);
And then simply pass that array:
{{ Form::select('book_name', $book_names) }}

Categories