Form select box {!!Form::select()!!} Laravel - php

I'm using Laravel Collective Form builder in my view and i'm populating this select box from a table in my DB like this
I am having an issue with my values not matching up and my dropdown is also giving me the values as an array...
Here's what I have in my PostsController:-
public function edit(Post $post)
{
$categories = Category::all()->pluck('title', 'id')->toArray();
return view('posts.edit')->withPost($post)->withCategories($categories);
}
and here's my view edit.blade.php:-
{{ Form::label('category_id', 'Category :')}}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
So I need little help?
Here's the value issue I was talking about:
enter image description here
Here's the array issue I was talking about:
enter image description here

no need to use toArray() pluck method automatically create an array.
try this
$categories = Category::pluck('title', 'id');

First of all your code seems like not proper.
$categories = Category::all()->pluck('title', 'id')->toArray();
should be
$categories = Category::pluck('title', 'id')->toArray();

Firstly update you controller function. I think that will help:-
public function edit(Post $post)
{
$categories = Category::pluck('title', 'id')->toArray();
return view('posts.edit', [
'post' => $post,
'categories' => $categories
]);
}
Try this code and let me know need help.

Related

Laravel 8: How to add pagination after retrieving data from ManyToMany Relationship

I'm working on a forum project using Laravel 8, and in this project, I have made a page for getting all the questions with specific tag. Here is the the method to do that:
public function tag($name)
{
$tag = \App\Models\Tag::findOrFail($name);
return view('tag',[
'single' => $tag
]);
}
And then inside tag.blade.php:
#foreach($single->questions as $one)
...
#endforeach
But now I need to add pagination for this page, so if questions are more than a custom number, they can navigate to other pages.
In fact I had already added pagination to my other page which shows all the entire questions:
public function allQuestions()
{
$all = Question::latest()->paginate(20);
return view('questions.allquestions',[
'all' => $all
]);
}
But I don't know really how to add this feature to tag() method.
So if you know how to add this pagination, please let me know...
I would really appreciate any idea or suggestion from you guys.
Thanks in advance.
Theres 2 choices for this:
First Choice:
public function tag($name)
{
$tag = \App\Models\Tag::findOrFail($name);
$questions = $tag->question()->paginate(10);
return view('tag',[
'single' => $tag, 'questions' => $questions
]);
}
Then, on your view, you can just loop through the questions.
#foreach ($questions as $question)
... fill this
#endforeach
{!! $questions->render() !!}
Second Choice:
public function tag($name)
{
$tag = \App\Models\Tag::findOrFail($name);
$tag->setRelation('questions',$tag->questions()->paginate(10));
return view('tag',[
'single' => $tag
]);
}
Then, on your view, you can just loop through the questions.
#foreach ($single->questions as $question)
... fill this
#endforeach

Fill laravel select with eloquent model using pluck

I'm trying to fill a laravel (version 5.5) select with data from the eloquent model.
So I'm geting data like this:
$types = Type::pluck('name', 'id');
And returning to the view:
return view('something')->with('types', $types->all());
So here is my view select:
{{ Form::select('type', $types, NULL, ['id' => 'myselect', 'class' => 'form-control']) }}
Well, the problem is: each option is an object, like this:
{"id":1,"name":"test","created_at":"2017-12-29 18:09:45","updated_at":"2017-12-29 18:09:45"}
And what I want is: the value equal to the id and the name equal to the name
How can I do this?
Change this:
return view('something')->with('types', $types->all());
To:
return view('something')->with('types', $types);
try this :
$types = Type::pluck('name', 'id')->toArray();
return view('something', compact('types'));

laravel pagination and sort table

I'm using sort function and pagination but the problem when I use sort for any column works fine but when I click to the next page using pagination the sort change and have to click on the column again for sorting.
so how to keep sorting while using pagination?
<tr>
<th>
<div class="checkbox checkbox-replace">
<input type="checkbox" id="chk-3">
</div>
</th>
<th>#sortablelink('details','Details')</th>
<th>#sortablelink('postingdate','Date')</th>
<th>#sortablelink('description','Description')</th>
<th>#sortablelink('amount','Amount')</th>
<th>#sortablelink('type','Type')</th>
<th>#sortablelink('slip','Slip#')</th>
<th>#sortablelink('vendor_id','Vendor')</th>
<th>#sortablelink('category_id','Category')</th>
</tr>
public function index()
{
$checks = Checks::orderBy('id', 'asc')->get();
$checks= Checks::sortable()->paginate(5);
return view('home',compact('checks'));
}
You can append query string to your link by doing this :
{{ $users->appends(['sort' => 'votes'])->links() }}
replace sort by your sortable query, and votes by something like request()->sort
According to this page, you also can do your pagination like this:
//Controller
public function index()
{
$checks = Checks::orderBy('id')->paginate(5);
$links = $checks ->appends(['sort' => 'id'])->links();
return view('home', compact('checks', 'links'));
}
And in your view, you can just call your pagination links like this
{{ $links }}
Hope it helps. Cheers!
{{links()}} will still give a messed up sort when you paginate. This is the same as {!! link() !!} also and {{ $checks->links()}}
Rather, use the code below. It will correct the error.
//for the Controller
public function index()
{
$checks = Checks::sortable()->paginate(5);
return view('home', compact('checks'));
}
//for the blade template in laravel append this in div after the table
{!! $checks->appends(\Request::except('page'))->render() !!}][1]
you can use this line in your blade file
{!! $product->appends(\Request::except('page'))->render() !!}

Convert collection of models into an array with id as key

I am using Laravel HTML component to create a dropdown to list all the groups to which an user can belong.
The list of groups comes from a Groups table.
Currently in my controller my code looks like
$groups = array();
$groupModels = Group::all(['id', 'name']);
foreach ($groupModels as $groupModel) {
$groups[$groupModel->id] = $groupModel->name;
}
return view('myview', compact('groups'));
and in my view I have the following code to create the dropdown
{!! Form::select('group', $groups, null, ['class' => 'form-control']) !!}
This works, but I am trying to see if there is a way to avoid the foreach loop and directly convert the list of Models into an array. Is it possible?
Use pluck() method:
$groups = Group::pluck('name', 'id');
return view('myview', compact('groups'));

Laravel: how to populate a blade SELECT with values from a where statement

I understand you can send values to a select statement like this:
Controller:
$client = Client::lists('name', 'id');
return View::make('index', compact('client'));
And populate this in my view like so:
View:
{{ Form::select('client_id', $client, Input::old('client_id')) }}
But how do I populate only records from Clients where group_id = 1 for example.
I tried:
$client = Client::lists('name', 'id')->where('group_id', 1)->get();
and
$client = Client::lists('name', 'id')->where('group_id','=', 1)->get();
But it doesn't seem to work like that and gives me the error "Call to a member function where() on a non-object"
Any ideas on how to make it work?
Controller:
$client = Client::where('group_id', 1)->pluck('name', 'id');
View:
{!! Form::select('client_id', $client, Input::old('client_id'), ['class'=> 'form-control']) !!}
Result:
<select id="client_id" class="form-control" name="client_id">
<option value="1">John</option>
<option value="2">Karen</option>
</select>
The lists() must be called at last
$client = Client::where('group_id','=', 1)->lists('name','id');
I found an answer that worked for me:
Use fluent instead of eloquent, which will look something like this:
$client = DB::table('clients')->where('group_id', 1)->lists('name');
return View::make('index', compact('client'));
Then in your view just call it inside blade form tags like this:
{{ Form::select('client_id', $client, Input::old('client_id')) }}
#KyleK, thanks for trying to help.
This would work too
Client::where('group_id','=', 1)->lists('name');
Not sure if this is a typo or not, but you're not retrieiving properly,
This line should be...
$client = Client::lists('name', 'id')->where('group_id','=', 1)->get();
Also....
Sometimes when populating lists, you will get models, and its easy to pass them, but sometimes u get arrays (fluent, raw etc) , and in those cases you have to access manually, and build the form with HTML because you have to access it differently.
If you prefer to use Laravel's query builder (e.g. for performance reasons):
$clients = DB::table('clients')->get()->pluck('name', 'id')->toArray();
return view('index', compact('clients'));
or, with a select to pull only what's needed:
$clients = DB::table('clients')->select('name', 'id')->get()->pluck('name', 'id')->toArray();
return view('index', compact('clients'));
In your view:
{!! Form::label('client_id', 'Client') !!}
{!! Form::select('client_id', $clients, old('client_id')) !!}

Categories