I passed my tags and on my form I have a multi-select. Although when I do tags[] nothing gets passed in. If I do just tag then only one of them submits.
public function create()
{
$categories = Category::pluck('name', 'id');
$tags = Tag::pluck('name', 'id');
return view('posts.create')->withCategories($categories)->withTags($tags);
}
{{ Form::label('tags', "Select Tag")}}
{{ Form::select('tags[]', $tags, null, ['class' => 'selectpicker', 'multiple', 'data-live-search="true"', 'data-actions-box="true"', 'data-width="100%', 'show-menu-arrow']) }}
It could be a glitch in the current version Laravel / Form Builder. If you do the old fashion select way it works as it should.
public function create()
{
$categories = Category::pluck('name', 'id');
$tags = Tag::all();
return view('posts.create')->withCategories($categories)->withTags($tags);
}
<select name="tags[]" class="selectpicker show-tick" multiple data-live-search="true" data-actions-box="true" show-menu-arrow>
#foreach ($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
#endforeach
</select>
Related
I have two tables with a many to many relation (Project and Center, the pivot table is ProjectCenter).
These are my models:
Project:
class Project extends Model {
public function centers()
{
return $this->belongsToMany('App\Models\Center', 'ProjectCenter', 'IDProject', 'IDCenter');
}
public function getCenterListAttribute()
{
return $this->centers->lists('IDCenter')->all();
}
}
Center:
class Center extends Model {
public function projects()
{
return $this->belongsToMany('App\Models\Project', 'ProjectCenter', 'IDCenter', 'IDProject');
}
}
Controller -> edit:
public function edit($id)
{
$project = Project::find($id);
$centerList = Center::lists('Name', 'IDCenter')->toArray();
return view('project/add', array('centerList' => $centerList))->with('project', $project);
}
And the view:
{!! Form::label('centers_list', 'Center*') !!}
{!! Form::select('centers_list[]',
$centerList,
null,
array(
'class' => 'form-control ',
'required' => 'required',
'multiple' => true,
'data-placeholder' =>
'Select a center'
)
) !!}
But I can not select the data already stored previously.
For example: the project 8 (IDProject) has two centers (1 and 2) but the data is not populated in the multiple select:
What am I doing wrong?
You all time get same result $centerList = Center::lists('Name', 'IDCenter')->toArray(); but you must be get product centers using query with model.
$project = Project::with("centers:Name,IDCenter")->find($id);
$centerList = $project->centers->pluck('Name', 'IDCenter')->toArray();
I already solve the problem using a foreach to select the related centers:
<select multiple="multiple" name="centers[]" id="centers" class="form-control select2" required="required" data-placeholder="Select a center">
#if($centerList)
#foreach($centerList as $key => $center)
<option value="{{$key}}" {{ (collect($selectedCenters)->contains($key)) ? "selected='selected'" : '' }} >{{$center}}</option>
#endforeach
#endif
</select>
I have a problem, blade not find the variable: editor.
This is the function of my Controller.
public function HomeText()
{
$data = [];
$data['editor'] = Editore::get();
return view('home')->with($data);
}
And these are the instructions in the file blade.php:
<select class="form-control select_editore">
#foreach ($editor as $editore)
<option>
{{ $editore->id_editore, $editore->nome_editore }}
</option>
#endforeach
</select>
What is the error?
I hope that you help me!
I'm newbie with Laravel,
I want to understand where I'm wrong.
Change it to this if you want to use an array:
return view('home', $data);
If you want to use ->with(), do this:
->with('editor', $data['editor'])
Change this:
public function HomeText()
{
$data = [];
$data['editor'] = Editore::get();
return view('home')->with($data);
}
to this:
public function HomeText()
{
$editor = Editore::all();
return view('home',compact('editor'));
}
and in your blade file
change
<option>{{ $editore->id_editore, $editore->nome_editore }}</option>
to
<option>{{ $editore->id_editore}}, {{$editore->nome_editore }}</option>
you may want to change the way you pass the variable to this
public function HomeText()
{
$data = [];
$data['editor'] = Editore::get(); // or all()
return view('home', $data);
}
then you may now use $editor in your blade ..
In your way of coding you can’t call $editor directly.
You have to use, $data[‘editor’] as $editore
<select class="form-control select_editore">
#foreach ($data[‘editor’] as $editore)
<option>
{{ $editore->id_editore, $editore->nome_editore }}
</option>
#endforeach
When I edited post I want to remove cancelled categories. I use sync method but its not working..
Edit Blade
<select class="form-control m-select2" id="m_select2_3" name="categories[]" multiple="multiple">
<optgroup label="Kategoriler">
#foreach($categories as $category)
<option value="{{$category->id}}" {{ in_array($category->id,$categoriesPost) ? 'selected' : '' }}>{{$category->category_name}}
</option>
#endforeach
</optgroup>
</select>
And Controller
public function update(StoreBlogPost $request, $id){
$post = Post::find($id);
$post->title = $request->title;
$post->caption = $request->caption;
$post->content = $request->input('content');
$post->save();
$post->categories()->sync($request->categories, false);
return redirect()->route('posts.index');
}
Any advice ?
You should set the second parameter of sync to true.
From the code,
/**
* Sync the intermediate tables with a list of IDs or collection of models.
*
* #param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids
* #param bool $detaching
* #return array
*/
public function sync($ids, $detaching = true)
{
....
}
It means, the ids that didn't present in the $ids will be detached.
this is my PermissionController.php
foreach (Route::getRoutes() as $route)
{
$action = $route->getAction();
if (array_key_exists('controller', $action))
{
$controllers[] = $action['controller'];
}
}
return view('permission.create')->withControllers($controllers);
and my permission/create/php is like this :
{!! Form::select('class_name[]', $controllers, null, ['class' => 'form-control', 'multiple']) !!}
here in controller, it put the name of all controllers in $controller and send it to the blade but when i select some of them, the request gives me their keys instead of value
public function store(Request $request)
{
return $request->get('class_name');
}
if I select the first one and third one the output is for example :{"1","3"}
but I want their value like this : {"UserController.php", "TestController.php"}
use this code instead Forms::Select or create your custom List with Laravel built-in List Function :
<select name="class_name[]" multiple>
#foreach($controllers as $class)
<option value="{{$class}}" >{{$class}}</option>
#endforeach
</select>
or simply replace the
$controllers[] = $action['controller'];
with
$controllers[$action['controller']] = $action['controller'];
I'm trying to pass a variable from foreach to my view. So I can access this using in my select form. Because I have two tables M:M relationship between departments and user. I need to get all the department_name where the user_id belong. For me able to send a data via department_name Here what I did please take a look.
DB Diagram:
department_user
As you can see here user_id is the id of the user and document_id is where the users belong.
Model:
Department:
public function users()
{
return $this->belongsToMany('\App\Models\User', 'department_user');
}
User:
public function departments()
{
return $this->belongsToMany('App\Models\Department', 'department_user');
}
Controller:
public function getDocuments()
{
$departmentRecipient = DB::table('departments')->get();
foreach ($departmentRecipient as $department)
{
$department->users = DB::table('department_user')
->where('department_id', '=', $department->id)
->pluck('user_id');
}
return view('document.create')->with('department', $department);
}
I'm getting all the users_id when I die and dump my variable departmentRecipient.
View:
<div class = "form-group">
<label for = "recipient" class = "control-label">Recipient:</label>
<select name = "recipient[]" multiple class = "form-control select2-multi" id = "myUserList">
#foreach ($department as $list)
<option value = "{{ $list->user_id }}">{{ $list->department_name }}</option>
#endforeach
</select>
</div>
I wanted to foreach the $department in my Controller to my select form. But it always says.
Trying to get property of non-object (View: C:\Users\JohnFrancis\LaravelFrancis\resources\views\document\create.blade.php)
Goal:
Use the following loop to iterate through the department users and add them to pivot table.
foreach($request->department as $departmentId)
{
foreach(Department::find($departmentId->id)->users()->get() as $user1) //find the users belonging to the current department
{
$document->sentToUsers()->sync([ $user1->id => ['sender_id' => $user->id]],false );
}
}
Also remove the following code form your getDocuments() as it is redundant:
foreach ($departmentRecipient as $department)
{
$department->users = DB::table('department_user')
->where('department_id', '=', $department->id)
->pluck('user_id');
}
I don't see user_id property in your dumped value of $departmentRecipient object, that is why you are getting the error you mentioned. However, there is a users array inside of $departmentRecipient object, which you made inside your foreach loop. You are plucking every user_id which are in individual department and setting in a property named users of $departmentRecipient object, and so you are getting an array inside users property. Here I have a solution for you,
public function getDocuments()
{
$departmentRecipient = DB::table('departments')->get();
$departmentUsers = array();
foreach ($departmentRecipient as $department)
{
$users = DB::table('department_user')
->where('department_id', '=', $department->id)
->pluck('user_id');
foreach ($users as $userId) {
$departmentUsers[$userId] = $department->department_name;
}
}
return view('document.create')->with('department', $department)->with('departmentUsers', $departmentUsers);
}
and inside of your view loop through the variable $departmentUsers, like this,
#foreach ($departmentUsers as $userId => $departmentName)
<option value = "{{ $userId }}">{{ $departmentName }}</option>
#endforeach
This will work but as your department contains multiple users so you will get individual department name multiple time in your select2 dropdown. If you share more of what is your goal by select2 then may be I can help you to solve your problem in other way.
Moreover if you are interested to use of Eloquent then you can get rid of lots of foreach looping.
In your case you can have multiple users against each department. So to make it work correctly with your forearch code. You need to make sure you are getting one user record against each depart. So modify following line of code in controller.
$department->users = DB::table('department_user')->where('department_id', '=', $department->id)->pluck('user_id');
But you want to display all users of department then you have to change foreach loop code into view.
Try This Code
App/Department
public function users()
{
return $this->belongsToMany('App\Entities\User', 'department_user', 'user_id', 'department_id');
}
App/User
public function departments()
{
return $this->belongsToMany('App\Models\Department', 'department_user');
}
Controller
use App/User;
public function getDocuments($userId,User $User)
{
$getSpecificUser = $User->with('departments')->find($userid);
return view('document.create')->compact('getSpecificUser');
}
View
#foreach ($getSpecificUser as $getUser)
#if(empty($getUser->departments) === false)
#foreach ($getUser->departments as $departments)
<option value = "{{ $getUser->id }}">{{ $departments->department_name }}</option>
#endforeach
#endif
#endforeach