I have many to many relationship. In postman I pass find_id and status.
My query is like: UPDATE finds_user SET status = TRUE WHERE find_id = 2 AND user_id = 1.
I do it now like that:
DB::table('finds_user')
->where('find_id', '=', $request->find_id)
->where('user_id', '=', $user->id)
->update(['status' => $request->status]);
How I can do it with Eloquent instead of DB Query Builder?
Thank you a lot!
we have tow condition for update:
1- find_id = 2
2- user_id = 1.
suppose the relation name in the user model called 'finds'
the query will look like this:
User::find(1)->finds()->newPivotQuery()->where('find_id',2)->update(['status'=>true])
and if you have a model for the pivot table with name like "FindUser":
FindUser::where('user_id',$user->id)->where('find_id',2)->update(['status'=>true]);
Related
This question already has answers here:
Laravel eloquent selecting most recent row from joined table
(3 answers)
Closed 3 years ago.
I have two tables for example users and orders.
I have to join users with orders On users.id = orders.user_id - which is quite fine.
User table has one to many relation in orders table.
I have some conditions also, like orders.pay_type = 'xyz', 'orders.date = yesterday' , 'user.status = active' ..etc.
My problem is that I need to join my user table to the latest row of orders table correspond to that user_id.
Or need to fetch the latest details of that users orders table details along with user table data.
I already tried
->orderBy('orders.date')
->groupBy('orders.user_id')
->get();
but it has no result in o/p.
$data= users::leftJoin('orders', function($join) {
$join->on('orders.user_id', '=', 'users.id')
->on('orders.id', '=', DB::raw("(SELECT max(id) from orders WHERE orders.user_id = users.id)"));
})
->select(*)
This resolve my issue.
If you don't want to change anything in model you can go with this method as well, which is simple query builder method.
DB::table('orders')->leftJoin('users', 'orders.user_id', 'users.id')
->groupBy('orders.user_id')
->orderBy('orders.date', 'DESC')
->get();
Assuming, You might have "orders" function in User.php model
You can also go for below method. (Similar to the answer already given but for array of users instead of one user.)
$users = User::all();
foreach ($users as $user) {
$latest_order = $user->orders()->where('pay_type', 'xyz')->orderBy('date', 'DESC')->first(); // Option 1
$latest_order = $user->orders()->where('pay_type', 'xyz')->latest('date')->first(); // Option 2
}
You can use the following Relation in the User Model
public function latest_order(){
return $this->hasOne('Order')->orderBy('orders.date', 'desc')->limit(1);
}
Update
Since you can not change the Model, you can use the following code to fetch the latest Order for each User.
$user = User::first(); //get first User
$user->orders()->latest('date')->first();
In my application, there's two tables at the moment: users and employees. The latter is simply a profile table for the boilerplate users table that ships with Laravel.
I have two foreign keys inside of employees, one is user_id that points to id on users table. So basically, the profile data of the registered user. The second foreign key in employees is manager_id. That is the id of the person who is the manager of an employee. Some users are managers and have a blank manager_id field.
I want to retrieve the row from my Employee model where the user_id matches the manager_idand then send them all to my view, where they will see the first_name and last_name of the manager. At the moment I see the manager ID instead of their name.
Sample LeftJoin =>
DB::connection('testing')->table('table1 as t1')
->select('t1.column, t2.column')
->leftJoin('table2 as t2','t2.client_id','=','t1.id')
->get();
Try some thing like this:
$users = DB::table('users t1')
->join('users t2', 't1.id', '=', 't2.user_id')
->select('your column list')
->get();
// here we are creating 2 aliases for users table which make it self join
In your Employees model add the following method:
public function manager()
{
return $this->hasOne('App\User', 'id', 'manager_id');
}
You can then get the name of the Manager like this:
$employee = \App\Employee::find(1);
$manager = $employee->manager()->first();
$manager_name = $manager->name;
I am doing project in laravel.
Database Table 1
user_verification
------------------------------------------
'id', 'user_id', 'verified', 'verified_by'
Database Table 2
user
-----------------------------
'id', 'name', 'email', 'pass'
I want to get data as all users which are verified('verified' = 1) and name of user('verified_by' is user id from user table) who does verification.
My controller method is like,
public function verifiedProviders(){
//$user_data = sql query goes here...
return view('display',compact('user_data '));
}
for example:,
I want to display in blade file as follow
name:abc,
email:abc#gmail.com,
verified by: name (id present in *verified_by* field) from *user* table.
I am not getting how to get and display this data in laravel's blade file. How to use relationships eloquent method or database query. Thanks in advance.
User::select('name', 'email', 'u2.name as verified_by_name')
->join('user_verification', 'user_verification.user_id', '=', 'user.id')
->join('user u2', 'user_verification.verified_by', '=', 'u2.id')
->all(); // I guess
Of course you may want to limit your query to return only the first 25 results and display them, etc.
References:
https://laravel.com/docs/5.2/queries#joins
https://laravel.com/docs/5.2/queries#selects
I have a table Registrationrequests where I have course_id and user_id and some other field.
$users_id = Registrationrequest::where('course_id', $query_course_user)->where('registered', 1)->get();
From the above query it gives me an array of result. But I need to take the details of these user_id from another table Users. I'm using Laravel. Table models are Registrationrequest and User
How can I get the user details from the above select result? I'm not that good in Joins. Any advice?
Use Eloquent's whereHas method:
$courseId = Request::get('course_id');
$users = User::whereHas('registrationRequests', function($query) use ($courseId)
{
$query->where('course_id', $courseId)->where('registered', 1);
});
This assumes you have set up the proper relationship in your User model. If not, add this method to your user model:
public function registrationRequests()
{
return $this->hasMany('Registrationrequest');
}
i have some table,one of them is user table that i want fetch id from this table by searching username and second one is prize_info table that i want to fetch user_id from here by searching a prize,after all i want fetch just prize_info.user_id and user.id.what is the best way for this search part in laravel4?? here is my codes
$search = '%'.Input::get('keywords').'%';
$pages = DB::table('user')
->select('user.id','user.username')
->where('username', 'LIKE', $search)->get();
$blogitems = DB::table('prize_info')
->select('prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
->where('prize_name', 'LIKE', $search)->get();
what is your idea about above codes?????
and i check the result by this line but it does not work!
$results = $pages->union($blogitems)->take(30)->get();
please help me!best regard
I would advise you used Laravel's Eloquent ORM Relationships to do this. You can easily fetch related record using the available relationships;
One-to-One
One-to-Many
BelongsTo
BelongsToMany
HasMany
HasOne
Polymorphic Tables (using pivot tables)
You can use this:
$search = '%'.Input::get('keywords').'%'
$results = DB::table('user')
->select('user.id','user.username','prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
->leftJoin('prize_info','user.id','prize_info.user_id')
->take(30)
->get();