Laravel collective select list with default selected value - laravel

I try to populate Laravel forms select list with some data and default selected value like this
#foreach($pesron->state as $state)
{{ Form::select('state[]', \App\City::pluck('Description', 'id'), $state->city->Description,['class' => 'form-control'])!!}
#endforeach
I want $state->city->Description value will be selected by default in select list. Is it possible? I use 5.7.15 version

Looks like you're using Laravel Collective's form helpers.
Take a look here https://laravelcollective.com/docs/5.4/html#drop-down-lists
echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], 'S');

Related

How to change url in pagination?

In my Laravel 5.7 app I have news page
http://mysite/all_news
with pagination implementation as:
$newsList = PageContent
::select(\DB::raw(' page_contents.*, users.username'))
->getByPageType( 'N' )
->getByPublished( true )
->orderBy('created_at', 'desc')
->join(\DB::raw('users '), \DB::raw('users.id'), '=', \DB::raw('page_contents.creator_id'))
->paginate( 4 , null, null, $page)
->onEachSide();
and route defined as :
Route::get('all_news', array(
'as' => 'all_news',
'uses' => 'PageController#all_news'
));
and in view I show pagination :
{{ $newsList->appends([])->links() }}
But rendered links in pagination looks like(and how to render them ?):
http://mysite/all_news?=2
How to make urls look like
http://mysite/all_news/2
?
Thanks!
Take a look at this:
https://laravel.com/docs/5.7/pagination#displaying-pagination-results
Specifically:
"Customizing The Paginator URI":
$users->withPath('custom/url');
The withPath method allows you to customize the URI used by the
paginator when generating links. For example, if you want the
paginator to generate links like
http://example.com/custom/url?page=N, you should pass custom/url to
the withPath method

Laravel 5.3 Best practices - load lists from other models to populate dropdowns

i'm new to Laravel and i'm using InfyOm laravel generator to create an app.
I came to an issue and would like to know what is the best practice to do this:
I have a model "Mission". When creating a mission, it need to have a client id and agent id associated with it.
In my view, i want to display 2 dropdown lists, one containing all the active clients (id + name) and the other containing all the active agents (id + name).
my controller
public function create()
{
return view('missions.create');
}
my view blade
{!! Form::select('client_id', ?????? , null, ['class' => 'form-control']) !!}
Thanks for your suggestions
Most convenient way is to use pluck() method to build a list from a collection, for example:
public function create()
{
return view('missions.create', [
'users' => User::pluck('name', 'id')
]);
}
Then use $list variable:
{!! Form::select('client_id', $users , null, ['class' => 'form-control']) !!}

Accessor parameter is giving me null in Laravel 5.3

I am working on one to many relationship between tasks and Projects, i.e. a task can only belong to a single project, and I have used laravel's Accessor to get the selected project in my view in drop down:
my code is as follow:
public function getAssignUserAttribute($value)
{
dd($value); // gives me null
// if $value have id of user I want to get that user from db
}
and my view contains the drop down is:
{!! Form::select('assign_user', $assign_user, null, ['class' => 'form-control select2', 'id' => 'assign_user']) !!}
I have accessed all the users from database into TasksController to the view as:
$assign_user = User::pluck('title', 'id');
return view('tasks.edit', compact('task', 'assign_user'));
But I get all users selected, while I only want to have the selected user in my drop down.
Can someone guide me to the right path.
Thank you
finally I solved the issue by myself I have edit the Accessor as follow:
public function getAssignUserAttribute()
{
return [0 => $this->attributes['assign_user'] ];
}
Since I required an array so I assigned the current user to the array index 0 and return, in that case the view selected the returned user in drop down :)
This may help someone :)

database fetch value show in dropdown using laravel 4

hello I am in big trouble please help me about database fetch value show in drop down using laravel 4 my code is below
<?php //echo "<pre>"; print_r($user);exit; ?>
{{ Form::select($user, $user ) }}
in print_r($user); showing all database value fetch from database but i want to show only value = id , user name field I try for each but not working any idea how to show value=id , user name filed
and result showing in to the drop down like this
<select name="[{"id":"1","username":"skaka","name":"sandip","avatar_id":"1","gender":"1","dob":"1989-10-20","grade":"A","school":"kbpv","created_at":"2014-07-22 08:45:59","updated_at":"2014-07-22 08:48:04","parent_email":""},{"id":"2","username":"ttt","name":"test2","avatar_id":"2","gender":"2","dob":"1989-10-20","grade":"A","school":"kbpv1","created_at":"2014-07-22 08:58:25","updated_at":"2014-07-22 08:58:25","parent_email":""}]">
In order to use {{Form::select()}} the easiest way to achieve this is by calling your data from the database with lists() method.
So
$users = User::lists('username', 'id');
Now you have an array with data like this:
$users = [1 => 'skaka', 2 => 'ttt', ....];
So you are ready to use efficiently Form::select() by doing:
{{Form::select('dropdown_name', $users)}}

Laravel Foreign Keys Drop-down List

I have 2 tables:
CUSTOMERS(id, full_name, company_id)
COMPANIES(id, company_name)
I already created the relation between the two tables, and it's working fine because I can display company name in the customers view like this: $customer->company->company_name
I'm now having issues with the customer create and edit views. I'd like to have the company_name as a drop-down (Form Select) in create and edit views. Then insert the company id to the CUSTOMERS table.
You need to supply Form::select with companies as an array('id'=>'name'):
// Controller, repo or wherever you want it:
$companies = Company::lists('company_name','id');
// $companies passed to the view, then in the create view:
{{ Form::select('company_id', $companies, null, $options) }}
// edit view:
{{ Form::model($customer, array('route' => array('YourCustomerUpdateRoute', $customer->id))) }}
...
{{ Form::select('company_id', $companies, null, $options) }}
// form model binding autopopulates the form, so correct option will be selected
After submitting the form validate the input, check if provided company_id exists on the companies table and save the customer, that's all.
The Jarek Tkaczyk answer is excellent. However if you want make a default value for create form and avoid pre-select the first element in the $companies array, you can do something like this in your controller:
$companies = Company::lists('company_name','id');
$companies = array('0' => 'Select Company') + $companies;
Then pass $companies array to the view as Jarek Tkaczyk said.
Note:
I did
$companies = array('0' => 'Select Company') + $companies;
in order to preserve the array keys.

Resources