I am trying to fetch all the users in my application so as to pick the user name or id in a select form, but I have not been able to achieve this after a long struggle. My MessageController that retrieves the messages is:
public function getMail(){
$message = Message::orderBy('created_at', 'desc')->get();
$pple = User::where('id', '!=', Auth::id())->pluck('name', 'id')->all();
var_dump($pple);
return view('mail', array('user' => $pple))->with('messages', $message, 'users',$pple );
}
My view that I want to render the users is:
<div>
<select id="recipient">
<option>{{$pple->name}}</option>
</select>
</div>
I have really tried doing this to help me pick up a user id or name to use in my messaging system.I would appreciate if someone helped me with this
You should get your users then pluck to get the result:
$messages = Message::orderBy('created_at', 'desc')->get();
$users = User::where('id', '!=', Auth::id())->pluck('name', 'id');
and return
return view('mail')->with(compact('messages', 'users'));
Then use it this way in your view
<select name="recipient" id="recipient">
#foreach($users as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
#enforeach
</select>
Don't forget to add a name to your select.
In your view, this should work
#foreach($pple as $userId => $userName)
<option value="{{ $userId }}">{{ $userName }}</option>
#endforeach
Related
Contacts:
id
name
Tags:
id
name
ContactTags:
contact_id
tag_id
In Contacts model:
public function tags()
{
return $this->belongsToMany(Tags::class, "contacts_tags", "contact_id", "tag_id");
}
So if I do
$contact = Contacts::findOrFail($id);
dd($contact->tags);
I successfully get the tags associated with the contact. But how can I get all tags and a flag indicating which one of those is associated?
I'm trying to prevent fetching all tags, loop them and with each iteration loop all contact_tags and check if tag_id matches. I want to display a list of checkboxes with all tags and check the ones that are in the relation.
This code can help you, but I'm using the SELECT multiple component. You can easily adapt it to use the CHECKBOX component.
Contacts model:
public function tags()
{
return $this->belongsToMany(Tags::class, "contacts_tags", "contact_id", "tag_id");
}
ContactController.php
public function edit(Contact $contact)
{
$tags = Tag::all();
return view('contacts.edit',compact('contact', 'tags'));
}
edit.blade.php
<div class="row">
<div class="col">
<div class="form-group">
<strong>Tags:</strong>
<select name="tags_id[]" multiple>
#foreach ($tags as $tag)
#if( $contact->tags->contains($tag) )
<option value="{{ $tag->id }}" selected>{{ $tag->name }}</option>
#else
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
#endif
#endforeach
</select>
</div>
</div>
</div>
Update in ContactController.php
public function update(Request $request, Post $contact)
{
$validatedData = $request->validate([
'tags_id' => ['array'],
]);
$contact->update($request->all());
$contact->tags()->sync($validatedData['tags_id']);
return redirect()->route('contact.index')->with('success', 'Contact successfully updated!');
}
The validation is just an example. The $validatedData has no use here, but it can be used to update the contact if you validate the other fields.
I am using laravel with infyomlab generator. I want to add data as dropdown from another table.
Here is my code
ProductController.php
$supplier = Supplier::all();
$unit = Unit::all();
$category = Category::all();
$products = $this->productRepository->all();
$product = DB::table('products')->select(
'suppliers.*',
'units.*',
'categories.*')
->join('suppliers','suppliers.id', '=', 'products.supplier_id')
->join('units','units.id', '=', 'products.unit_id')
->join('categories','categories.id', '=', 'products.category_id')
->get();
return view('products.index', compact('supplier', 'unit', 'category', 'product'))
->with('products', $products);
fields.blade.php
<select class="form-control" name="supplier_id" id="supplier_id">
<option value="">Select Supplier</option>
#foreach($suppliers as $supplier)
<option value="{{ $supplier->id }}">{{ $supplier->$supplier_name }}</option>
#endforeach
but when i press add new product it does not show the form for input it shows the following error
Undefined variable: suppliers (View: C:\xampp\htdocs\qa\resources\views\products\fields.blade.php)
try This
#foreach($products as $supplier)
<option value="{{ $supplier->id }}">{{ $supplier->$supplier_name }}</option>
#endforeach
As u returning it
It's just a typo:
Here, you are returning supplier not suppliers compact('supplier', 'unit', 'category', 'product')
So you need to perform in front end like:
#foreach($supplier as $s)
<option value="{{ $s->id }}">{{ $s->$supplier_name }}</option>
#endforech
You replace it by
$supplier = Supplier::all();
to
$suppliers = Supplier::all();
return view('products.index', compact('suppliers', 'unit', 'category', 'product','products'));
So i currently have a drop down list where it will output a list of hotels based on its price. I now want to further develop the drop down list so that it can output a list of hotels based on price AND distance. How would i exactly create a drop down list that will output a list of hotels based on 2 or more criteria.
SearchController.php
public function index(Request $request)
{
$distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
$prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');
$postsInRange = $request->has('distance')
? Post::where('distance', $request->distance)->get()
: [];
$postsInRange1 = $request->has('price')
? Post::where('price', $request->price)->get()
: [];
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $postsInRange,
'posts' => $postsInRange1,
]);
}
public function store(Request $request)
{
// This will return all request data to your screen.
return $request->all();
return view('Pages.search');
}
Search.blade.php
<select name="distance" id="distance" class="form-control input-lg dynamic" data-dependent="state">
<option value="">Distance</option>
#foreach($distances as $distance)
<option value="{{ $distance }}">{{ $distance }}</option>
#endforeach
</select>
<br>
<select name="price" id="price" class="form-control input-lg dynamic" data-dependent="state">
<option value="">Price</option>
#foreach($prices as $price)
<option value="{{ $price}}">{{ $price}}</option>
#endforeach
So to clarify the drop down list works but only outputs one of the choices selected.
I think what you need is query filter
$post = new Post;
if ($request->has('price')) {
$post->where('price', $request->price);
}
if ($request->has('distance')) {
$post->where('distance', $request->distance);
}
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $post->get()
]);
I assume that you need one list applying both filters.
I have two tables posts and category.
My controller:
public function index()
{
$posts = Post::orderBy('id', 'desc')->get(['category']);
$data = DB::table('posts')->paginate(5);
$category = DB::table('category')->pluck('cname','id');
return view('posts.index', [
'posts' => $posts,
'category'=> $category,
'posts' => $data
]);
}
My blade:
<select class="form-control" name="category" id="category_add">
<option value="">Select Category</option>
#foreach($category as $key=>$value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
Now my code shows a drop down data from the database...
What I need is: When I select the drop down category it should only shows the particular category of data in the view dynamically
Any one help
Did you mean after selecting a category from the dropdown there will be another section which will show data about that specific category?
One way to do it is through ajax request. I dont know if i got your question right or wrong. can u explain a bit if i got it wrong?
I'm having trouble to get all data (options) selected in multiple select dropdown. Here is the blade code:
<div class="form-group">
<select name="roles[]" class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="User roles" style="width: 100%;" tabindex="-1" aria-hidden="true">
#foreach ($roles as $role)
<option value="{{$role->name}}">{{$role->display_name}}</option>
#endforeach
</select>
</div>
That's how I get all "roles" listed, I have like seven of them in database and one should be able to select as much as one wants.
Here is the part in the controller:
$input = Input::all();
$roles[] = $input["roles"];
foreach ($roles as $role) {
echo $role; //this is just for testing purposes
}
But, only last one in that array is being showed. So if I select "admin, moderator, subscriber" it will only show "subscriber". Please help me, it's obvious I'm missing some small detail.
I'm sorry I can't test this answer right now. Try:
$roles = Input::get('roles');
foreach ($roles as $role) {
echo $role; //this is just for testing purposes
}
So the problem was double array I had both name of the select tag and var in the controller with "[]" indicating they are array, removing "[]" from $roles[] in controller solved it.
In case anyone needs this:
$input = Input::all();
$roles = $input["roles"]; // removed brackets
foreach ($roles as $role) {
echo $role; // this is just for testing purposes
}