I modified the register controller AND the user table to have a role attribute as a string:
Schema::table('users', function (Blueprint $table) {
$table->string('role')->nullable()->index();
});
and i have created an enums.php in config folder like this:
return [
'role_types' => [
'ADMIN' => "Admin",
'TEACHER' => "Teacher",
'STUDENT' => "Student",
]];
on the register user view (which is scaffold by default with bootstrap)
i made select box to accommodate the selection:
<select class="form-control" id="role" name='role'>
#foreach (Config::get('enums.role_types') as $role)
<option value="{{ $role }}">{{$role}}</option>
#endforeach
</select>
But up on registering the user, the value is NULL.. what am i doing wrong ?
When using Model::create() you have to set your model's fillable array:
protected $fillable = [
'email',
'name',
'role',
...
];
Laravel does not let you mass assign a row using data it cannot trust. Check the docs: https://laravel.com/docs/5.3/eloquent
Related
What I'm trying to achieve:
User can assign multiple supermarkets to a product, and supermarkets can be assigned to multiple products. When registering a product, I want the user to be able to select from check boxes which are populated from a database.
Product.php
class Product extends Model
{
//
protected $fillable = [
'name',
'summary',
'category_id',
'author_id',
'supermarket_id',
'gf_rank_id',
'average_price',
'photo_id',
'online_store_path',
'ingredients',
];
public function supermarket() {
return $this->belongsToMany('App\Supermarket');
}
}
Supermarket.php
class Supermarket extends Model
{
protected $fillable = [
'name',
'url',
];
public function products() {
return $this->belongsToMany('App\Products');
}
}
ProductsController.php
public function create()
{
$categories = Category::pluck('name', 'id')->all();
$supermarkets = Supermarket::pluck('name', 'id')->all();
return view('admin.products.create', compact(['categories', 'supermarkets']));
}
create.blade.php
#foreach ($supermarkets as $supermarket)
<div class="col-sm-3">
{!! Form::checkbox('supermarket_id[]', $supermarket->id) !!}
{!! Form::label('supermarkets', $supermarket) !!}
</div>
#endforeach
To explain what has been said in the comments:
You are not supplying an object in your $supermarkets data because you are using pluck('name','id'). You are supplying an associative array:
[
1 => 'supermarket A',
2 => 'supermarket B'
]
So you need to change your display code to:
#foreach ($supermarkets as $id => $name)
<div class="col-sm-3">
{!! Form::checkbox('supermarket_id[]', $id) !!}
{!! Form::label('supermarkets', $name) !!}
</div>
#endforeach
I want to display the role of data in a text box based on the selected user options. for example A user, role as project manager. then when selecting a user, the text box will display the role manager. but the problem is when I choose the first user, he displays the correct data in the text box. but when changing user choices, the text box does not change, only displays data based on the first choice. What is wrong.?
I want to display the role in the text box based on the user chosen
this my view
<div class="form-group">
<label>Name *</label>
<select class="form-control select2" style="width: 100%;" name="user_id" id="user_id">
<option selected="selected" value="user_id">Select One</option>
#foreach($users as $id => $user)
<option value="{{$id}}" data-price="{{$user_roles->name}}">{{$user}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Role *</label>
<input type="text" name="name" id="name" class="form-control" autocomplete="off" readonly>
</div>
script
<script type="text/javascript">
$('#user_id').on('change', function(){
var price = $(this).children('option:selected').data('price');
$('#name').val(price);
});
</script>
controller
$projects = Project::pluck('project_name', 'id');
$users = User::pluck('name', 'id');
$user_roles = auth()->user()->role;
return view ('teams.create', compact('projects', 'users', 'user_roles'));
model user
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Presence;
use App\Models\Project;
use App\Productivity;
use App\Sick_leave;
use App\Annual_leave;
use App\Models\Team;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'role_id',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function presence()
{
return $this->hasOne(Presence::class, 'astrowatch', 'user_id', 'presence_id');
}
public function role()
{
return $this->belongsTo(Role::class, 'role_id');
}
public function permission()
{
return $this->hasMany(Permission::class);
}
public function teams()
{
return $this->belongsToMany(Team::class, 'user_teams');
}
public function spent_time()
{
return $this->belongsToMany(Spent_time::class, 'astrowatch', 'user_id', 'spent_time_id');
}
public function projects()
{
return $this->belongsToMany(Project::Class, 'user_projects');
}
}
Change your JS to this:
$(function() {
$('#user_id').on('change', function(){
var price = $(this).data('price');
$('#name').val(price);
});
});
-- EDIT--
Change this
Controller:
$users = User::all();
View:
#foreach($users as $user)
<option value="{{$user->id}}" data-price="{{$user->role->name}}">
{{$user->name}}
</option>
#endforeach
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 am trying to learn laravel 5, so I strated by creating some simple forms.
My form contains (id, title,..) and radio button
<label for="importance">Importance</label>
<input type="radio" name="Importance"
<?php if (isset($importance) && $importance=="tres_important") echo "checked";?>
value="tres_important">très important
<input type="radio" name="importance"
<?php if (isset($importance) && $importance=="important") echo "checked";?>
value="important">important
Now, I need to know what I should add to my migration file, to make it work "create_projets_table"
public function up()
{
Schema::create('projets', function (Blueprint $table) {
$table->increments('id',true);
$table->string('title');
$table->date('dateDebut');
$table->timestamps();
});
}
Thank you in advance
UPDATES :
Based on the comments bellow, I made some changes, but still not working, it seems like something went wrong with "submit.blade.php"
submit.blade.php :
<div class="form-group">
<label class="radio-inline">
<input type="radio" id="tres_important" name="importance" value="tres_important">Très important</label>
<label class="radio-inline">
<input type="radio" id="important" name="importance" value="important">Important</label>
</div>
and this is my migration "create_projets_table.php" :
public function up()
{
Schema::create('projets', function (Blueprint $table) {
$table->increments('id',true);
$table->string('title');
$table->date('dateDebut');
$table->date('dateFin');
$table->float('cout');
$table->integer('importance');
$table->timestamps();
});
}
This route.php :
Route::post('/submit', function(Request $request) {
$validator = Validator::make($request->all(), [
'title' => 'required|max:255',
'annee_realisation' => 'required|max:255',
'cout' => 'required|max:255',
'importance' => 'required',
]);
if ($validator->fails()) {
return back()
->withInput()
->withErrors($validator);
}
$projet = new \App\Projet;
$projet->title = $request->title;
$projet->annee_realisation = $request->annee_realisation;
$projet->cout = $request->cout;
$projet->importance = $request->importance;
$projet->save();
return redirect('/');
});
And this my controller "Project_Controller" :
class Project_Controller extends Controller
{
//
$this->validate(request(), [
'importance' => 'required'
]);
}
I got the following error :
Undefined variable : importance
enter image description here
You can use laravel form methods, like below
Form::radio('name', 'value', true);
Example:
<label class="radio inline">
{!! Form ::radio('importance','tres_important',($importance == 'tres_important' ? true : false)) !!}
Très important
</label>
<label class="radio inline">
{!! Form ::radio('importance','important',($importance == 'important' ? true : false)) !!}
Important
</label>
Read Documentation https://laravelcollective.com/docs/5.0/html#checkboxes-and-radio-buttons
I don't see anywhere in your code you have set the value of "$importance", can you show the code where you set that value?.
Your checking to see if its set which is fine, but then also checking its value.
if (isset($importance) && $importance=="tres_important"
if it is not set you cant chcek it to see what the value is, which is what it looks like your trying to do in the code i can see. Thats where your undefined error is coming from. If you can post full code it will be easier to track down
I have a menu in my Laravel application which lists tags. I want to be able to add more tags and change their order, but I don't want to affect the underlying database ids for each tag. Because that would break any data I have already associated with them. So I thought I add an extra column to my database called 'display_order' like so:
Database:
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->integer('display_order');
$table->string('name');
$table->timestamps();
});
Now I want to create a view to let the user change the value of the display_order field. Initially I show a page which prints each tag and its display_order and allows the user to update the value in a text field:
Controller:
public function showTagsOrder()
{
$tags = Tag::all()->sortBy('display_order');
return view('menu.tags', compact('tags'));
}
Route:
Route::get('/menu/tags', ['as' => 'menu_tags', 'uses' => 'MenuOrderController#showTagsOrder']);
View:
{!! Form::open(['action' => 'MenuOrderController#updateTagsOrder']) !!}
#foreach($tags->chunk(18) as $chunk)
<div class="col">
#foreach($chunk as $tag)
<p><label>{!! $tag->name !!}</label><input type="text" name="tag_array[{!! $tag->id !!}]" value="{!! $tag->display_order !!}" class="numeric" />
#endforeach
</div>
#endforeach
<p>{!! Form::submit('Submit') !!}
{!! Form::close() !!}
When the form is submitted I then have the following function in my controller:
public function updateTagsOrder(TagsMenuRequest $request)
{
$i = 1;
foreach($request->tag_array as $tag_value)
{
$tag = new Tag;
$tag = Tag::find($i);
$tag->display_order = $tag_value;
$tag->save();
$i++;
}
flash()->overlay('The order of tags in the menu has been udpated!', 'Congratulations');
return redirect('/');
}
But this doesn't work. How would I rewrite this so I assigned the correct value for display_order to each tag. I'd be very grateful for your help.
foreach($request->tag_array as $tag_id => $tag_value)
{
$tag = Tag::find($tag_id);
$tag->display_order = $tag_value;
$tag->save();
}