I'm trying to connect companies that are used by specific user role. A user with a specific role can "work" for multiple companies. A company can "employ" multiple users. I have 5 tables (users, role_user, roles, companies and company_user)
Models relations:
App\User:
public function roles()
{
return $this
->belongsToMany('App\Role')
->withTimestamps();
}
public function companies()
{
return $this
->belongsToMany('App\Company')
->withTimestamps();
}
App\Role:
public function users()
{
return $this
->belongsToMany('App\User')
->withTimestamps();
}
App\Companies:
public function users()
{
return $this->belongsToMany('App\User'); // with user_role ??
}
Companies Controller
public function edit(Request $request, $id) {
$company = Company::findOrFail($id);
$users = User::where('role_id',4)->pluck('username')->all(); // no role_id column
$users = User::pluck('username','id')->all(); // returns all users
return view('companies.edit', compact(['company','users']));
}
public function update(Request $request, $id) {
/* TODO */
}
Edit view
{!! Form::select('users[]', $users, null, ['class' => 'form-control', 'multiple' => 'multiple']) !!}
I want to asign users whith a specific user role to these companies. Is there a way to setup the relation or perhaps a scope?
Bonus question :) Is there a simple way to display concatinated value in a dropdown? First name + Last name instead of username?
Perhaps not the tidiest solution but it seems to work :)
Model
public function scopeOfRole($query, $role) {
return $query->
join('role_user', 'role_user.user_id', '=', 'users.id')->
join('roles', 'roles.id', '=', 'role_user.role_id')->
select(
DB::raw('GROUP_CONCAT(users.first_name," ",users.last_name) as username'),
'users.id')->
where('roles.name', $role)->
groupBy('users.id');
}
Controler
public function edit(Request $request, $id) {
$company = Company::findOrFail($id);
$users = User::OfRole('my role')->pluck('username','users.id')->all();
return view('companies.edit', compact(['company','users']));
}
edit view
{!! Form::select('users[]', $users, $company->users()->pluck('users.id'), ['class' => 'form-control', 'multiple' => 'multiple']) !!}
Related
I am trying to get all the order details that are linked to and order which is linked to a booking.
Let's say I pass the booking id and wanna retrieve all the order details linked to it. but the orderOrderDetials in the view returns an empty array even if there is record in the database.
Booking Model
protected $fillable = [
'room_id',
'invoice_id',
'order_id',
'status',
'start_time',
'end_time',
];
public function invoice()
{
return $this->belongsTo(Invoice::class, 'invoice_id');
}
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
Order Model
protected $fillable = [
'booking_id',
'serial_number',
'subtotal',
];
public function orderOrderDetails()
{
return $this->hasMany(OrderDetail::class, 'order_id', 'id');
}
public function booking()
{
return $this->belongsTo(Booking::class, 'booking_id');
}
Order Details Model
protected $fillable = [
'order_id',
'order_item_id',
'quantity',
'amount',
];
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
public function order_item()
{
return $this->belongsTo(Product::class, 'order_item_id');
}
Home Controller
class HomeController
{
public function index()
{
$bookings = Booking::where('status', "in_progress")->orWhere('status', "unpaid")->get(); \\ shows 5 objects when dd($bookings)
return view('home', compact('bookings'));
}
}
Home View
#if ($bookings !== NULL)
#foreach ($bookings as $booking)
#foreach ($booking->order->orderOrderDetails as $orderDetail) \\$booking->order->orderOrderDetails returns an empty array even tho there are records in the database
<p>{{ $orderDetail->item->name ?? ''}}</p>
<p>{{ $orderDetail->item->price ?? ''}}</p>
<p>{{ $orderDetail->item->quantity ?? ''}}</p>
#endforeach
#endforeach
#endif
Order Details Table
Orders Table
Bookings Table
I want to populate the whole Edit form, but Select Box does not grab the value.
First time working with an eloquent relationship. I'm trying to access the Category method yet I get this error:
Property [categoryId] does not exist on this collection instance.
And I installed composer require laravelcollective/html package.
{!! Form::select('categoryId',
\Modules\Category\Entities\Category::pluck('title', 'id')->toArray(),
old('categoryId', $faq->categories->categoryId) , [
'class' => 'form-control',
'id' => 'categoryId',
]) !!}
Category.php
class Category extends Model
{
protected $guarded = [];
}
Faq.php
class Faq extends Model
{
protected $fillable = ['subject', 'body', 'userId'];
public function scopeSearch($query, $term)
{
return $query->when($term, function ($query, $term) {
return $query->where('subject', 'like', "%{$term}%");
});
}
public function user()
{
return $this->belongsTo(User::class, 'userId');
}
public function categories()
{
return $this->belongsToMany(Category::class, 'faq_category', 'faqId', 'categoryId');
}
}
I have a Situation Like This:
User Model:
public function role() {
return $this->hasOne('App\model\Roles' , 'id' ,'role');
}
public function userMetaData() {
return $this->hasOne('App\model\UserMetaData' , 'user_id' ,'id');
}
public function userBet() {
return $this->hasMany('App\model\UserBet' , 'user_id' , 'id');
}
public function userComission() {
return $this->hasMany('App\model\UserComission' , 'user_id' , 'id');
}
public function userPartnership() {
return $this->hasMany('App\model\UserPartneShip' , 'user_id' , 'id');
}
// Self Call
public function parentData() {
return $this->hasOne('App\User','id','parent_id');
}
Controller
$userData = User::with(['userMetaData','userBet','userComission','userPartnership','role','parentData'])
->where('id',$id)
->get();
Now The Point Is In role i am getting the roles Of the User and In the parentData i am getting the creator of the user(parent) from the same user table by self calling now that parent also has a role
My Question Is How can i get that role object inside the parentData Object?
Thanks!
First of all.. the relationship you've set is wrong it should be belongsTo
public function role() {
return $this->belongsTo('App\model\Roles' ,'role', 'id');
}
public function parentData() {
return $this->belongsTo('App\User','parent_id','id');
}
Now as you want role object inside the parentData set with as below.
$userData = User::with(['userMetaData','userBet','userComission','userPartnership','parentData.role'])
->where('id',$id)
->get();
I'm studying event message board. I can display table data by every Users own post. however I would like to display All post too. I wrote this as $tasksall but it didn't work. Could someone teach me what is wrong?
AController.php
public function index()
{
$tasks = Auth::user()
->tasks()
->orderBy('is_complete')
->orderByDesc('created_at')
->paginate(5);
$tasksall =
->tasks()
->orderBy('is_complete')
->orderByDesc('created_at')
->paginate(5);
return view('tasks', [
'tasks' => $tasks, 'tasksall' => $tasksall
]);
}
Task.php (model)
class Task extends Model
{
protected $casts = [
'is_complete' => 'boolean',
];
protected $fillable = [
'title',
'is_complete',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
AController.php I add this code
public function person()
{
return $this->belongsTo('App\Models\Task');
}
public function getData()
{
return $this->id . ':'/ $this->person->name.')';
}
index.blade.php I add this code
{{ $task2->getData() }}
You can just write a query to get all the task using eloquent to get all the tasks.
$tasksall = Task::all();
Have a look at this link.
Also for you case I think the problem is you are getting task from the User model so you $task will contain only task related to that particular user as you have a belongsTo relation of task with user.
For Your case to get name of User from task.
//Task model
class Task {
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Then you can query like this in your controller.
$task = Task::find($id);
$name = $task->user->name;
I am new to Laravel. I am trying to create a website called froto in this people hire vehicles for goods transportation I am successful in collecting data from users and display data to admin with middleware.but now I want to display data to user i.e. to generate order summary to user immediately after they hired a vehicle which I am not able to do
This is my controller store method and show method
public function store(PostsCreateRequest $request)
{
//
if ($user = Auth::user()) {
$input = $request->all();
$user = Auth::user();
$user->posts()->create($input);
return redirect('admin.post.show');
}
return redirect('/login');
}
public function show($id)
{
//
$users = User::all();
$posts = Post::all();
$posts = $post->where("user_id", "=", $user->id)->get();
return view('admin.post.show', compact('posts'));
}
My routes
Route::group(['middleware'=>'admin'], function(){
Route::resource('admin/users', 'AdminUsersController');
Route::resource('admin/post', 'AdminPostsController', ['only' => [
'index',
]]);});Route::resource('admin/post', 'AdminPostController', ['only' => [
'create', 'store', 'update', 'destroy', 'show']]);
My show.blade.php
#extends('layouts.app')
#section('content')
<h1>order summary</h1>
#foreach ($posts as $post) <h1> {{$post->user->name}}</h1>... #endforeach
#stop
please help me to generate an order summary when user click on hire a vehicle button thankshire a vehicle page:
Here are screenshots of my projecthome page:
App/User-- model
public function posts(){
return $this->hasMany('App\Post', 'user_id', 'id');
}
App/Post -- model
public function user(){
return $this->belongsTo('App\User', 'user_id', 'id');
}
public function show($id){
$user = User::findOrFail($id);
$posts = $user->posts;
return view('admin.post.show', compact('posts'));
}
I hope this helps.