I already joined two table and view data.
Here is my controller:
$data = DB::table('classrooms')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')->get();
return View::make('std_classes')->with('data',$data);
And Here is my view
<h4>{{$data->class_name}}</h4>
<h5>Section:{{$data->section}} </h5>
It's working nice. But my problem is in my url {{ URL::to('class/'.$data->id) }} i need here classroom id. But it's print joinclass id.
How can i solved this problem?
You need to manually select them out as you've 2 fields with the same name id.
$data = DB::table('classrooms')
->select('classrooms.id as classrooms_id', 'joinclass.*')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')->get();
return View::make('std_classes')->with('data',$data);
Related
I am working on admin panel ... where i get all products from database and show in table ... the problem is i want to get each product categorey from "category" table and get stock of each product from "products attribute" table ... i am create a join but this join collapse product id , name stock or prize from another table... Thanks.
public function viewProducts(Request $request){
$products = Product::get();
$products = Product::join('categories','categories.id','products.category_id')
->join('products_attributes','products_attributes.product_id',
'products.id')
->get();
$products = json_decode(json_encode($products));
//echo "<pre>"; print_r($products); die;
return view('admin.products.view_products')->with(compact('products'));
}
$products = Product::join('categories','categories.id','products.category_id')
->join('products_attributes','products_attributes.product_id','products.id')
->get(['categories.category_name as category_name', 'products_attributes.stock as product_stock', 'products.*']);
Try this:
Product::join('categories','categories.id','products.category_id')
->join('products_attributes','products_attributes.product_id','products.id')
->select(['categories.category_name as category_name', 'products_attributes.stock as product_stock', 'products.*'])
->get()->toArray();
I am trying to make a datatable, but I am having trouble querying the database. What I want to do is select one instance of each user on the datatable and I want to grab a count of all the products created by that user.
Products Table
id user_id title
1 2 tv
2 2 car
3 2 book
4 3 glasses
So user(2) has 3 products
So user(3) has 1 products
Here is the code I been working on
$users = DB::table('users')
->join('products', 'users.id', '=', 'products.user_id')
->select('users.*', 'products.id', 'url')
->groupBy('products.id', 'url')
->get();
You need to select the user details, together with a count of the products, grouped by the user id, as follows:
$users = DB::table('users')
->selectRaw('users.*, COUNT(products.id) AS products')
->join('products', 'users.id', '=', 'products.user_id')
->groupBy('users.id')
->get();
You can do this really simply with eloquent
Say you have your User model and Product model.
In your database structure, for your products table, you need to include a column called user_id as an integer data type.
Then You declare this relationship on the user model
public function products()
{
return $this->hasMany(Product::class);
}
then the inverse of the relation on the product's model
public function user()
{
return $this->belongsTo(User::class);
}
Now you can simply do the following
//This returns all users with products (eager loading pointes out by fubar)
$users = User::with('products')->get();
//Now you could pass $users to your view and loop through like so
#foreach($users as $user)
<li>{{ $user->name }} has {{ $user->products->count() }} </li>
#endforeach
for a specific user, you can simply
$user = User::find($id);
$userProductCount = $user->products->count();
eloquent really does make everything so clean and beautiful
Using Laravel eloquent how do I make a query like this:
select * from branches where user_id =(select id from users where name ='sara' )
Assuming that you have a user relationship in your Branch model you could use whereHas:
$branches = Branch::whereHas('user', function ($query) {
$query->where('name', 'sara');
})->get();
Update
If you're using v8.57.0 or above, you can now use the whereRelation() method instead:
Branch::whereRelation('user', 'name', 'sara')->get();
$id = Users::select('id')->where('name','sara')->first();
$barnches = branches::where('id',$id)->get();
Here Users and branches are models , first is using for 1 row and get for many rows
I would split it into two queries. First getting the id, then getting the list. Expecting your models to be called "User" and "Branches"
$user = User::where('name', 'sara');
$id = $user->id;
$branches = Branch::where('id', $id);
This site may help you Link
Try this.
$name = 'sara';
$results = BranchModel::whereIn("user_id", function ($query) use ($name) {
$query->select("id")
->from((new UserModel)->getTable())
->where("name", $name);
})->get();
You can use this:
$users = User::whereName("sara")->get()->pluck('id');
Branch::whereIn('user_id',$users)->get();
i have three table in mysql:
1-users
table of users
2-projects
table of project
3-project_user
there is id and project_id and user_id for relation
there is two model : user and project
the relation between these table are belongsToMany
when a project create maybe one project define for two person
NOW how can i show the project of each person?
Assuming you properly defined your relationships, do
$users = User::with('projects')->get();
//show each user projects
foreach($users as $user) {
echo $user->projects;
}
//Getting project users
$projects = Project::with('users')->get();
This will give you projects list, in each list you can see which user has access or not.
Updates
Taking project with specified user_id
$projects = Project::with(['users' => function ($query) use ($user_id) {
$query->where('user_id', '=', $user_id);
}])->get();
New code
Or try to to get the user
$user = User::with('projects')->where('id', $user_id)->first();
Or via projects, constraining by user_id
$projects = Project::with('users')->whereHas('users', function ($query) use ($user_id) {
$query->where($user_id);
})->get();
the way i solve it:
//save user_id
$user_id = $request->id;
//select in project_user where user_id = $user_id
$project_user = DB::table('project_user')->where('user_id', '=', $user_id)->get();
//create an empty array
$p_id = array();
//fill empty array with project_id
foreach($project_user as $pro) {
$p_id[] = $pro->project_id;
}
//select code and id in project table where id = one of that [$p_id] array
$data = Project::select('code','id')->whereIn('id', $p_id)->get();
//return your data in json
return response()->json($data);
As far as I understood your question, you want to query Project as per user_id. Here is how you do it.
Project::with('users')->whereHas('users', function ($query) use ($user_id_array) {
$query->whereIn('id',$user_id_array);
})->get();
This will give you projects whose user_id are from $user_id_array. Let me know if you need any further help.
I have a form to mark employees' attendance that HR fill in which is looped over every current employee. I need it to show the current values where attendance is marked in the database, otherwise it should be blank.
In my controller I query the existing results:
$results = Attendance::where('Sdate', '=', date("Y-m-d", strtotime($TheDate))) ->get();
Then loop through them to get the employee details:
foreach($results as $result)
{
$contractors = DB::table('contractors') ->where('contractors.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $employees = DB::table('current_employees') ->where('current_employees.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $showEmployees = array_merge($contractors, $employees);
}
This should exclude all employees who have a record saved in attendance for that date, however it doesn't seem to be looping correctly. It will exclude some results, but not all. If I return the results variable I get the correct list of records so I know that part is working correctly.
What I'm looking to achieve in my view is something like:
#foreach($attendance as $results)
Show form where there's an existing record for this date and department
#endforeach
#foreach($employees as $employee)
Show form for all employees in this department (but should exclude results where there is a record in attendance)
#endforeach
The problem with your code is you are saving the result in a variable and not in an array.
Your solution would be to store the data in an array
foreach($results as $result)
{
$contractors[] = DB::table('contractors') ->where('contractors.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $employees = DB::table('current_employees') ->where('current_employees.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $showEmployees = array_merge($contractors, $employees);
}
Try printing contractors array and see what happens.I hope this works
This was answered for me on Laracasts.
What I needed to do was create a list of the variable to check against (employee number)
$PRNs = Attendance::where('Sdate', '=', date("Y-m-d", strtotime($TheDate)))->lists('PRN');
And then use Laravel's whereNotIn, checking against the list.
$contractors = DB::table('contractors')
->where('contractors.AreaPosition', '=', $department)
->whereNotIn('PRN', $PRNs)
->get();