Beginer with sql, I'm working on laravel and want to change the values of a table that the user who is connected is linked
I have 3 tables:
User(id) foreign key in Exams(user_id)
and
Exams(id) foreign key in Verification(exam_id)
How to change the verifications of only the user connected ? he doing it by himself so I think we wil need
Auth::user()
DB::table('verifications')->where([
['confirmation', 'non'],
['modifier', 'oui'],
])->update(['confirmation' => "oui"]);
If you aren't using laravel relationships then use Joins.
DB::table('verifications')
->leftJoin('exams', 'verification.exam_id', '=', 'exams.id')
->leftJoin('users', 'exams.user_id', '=', 'user.id')
->where([
['verifications.confirmation', 'non'],
['verifications.modifier', 'oui'],
['users.id', Auth::user()->id]
])
->update(['verificationsconfirmation' => "oui"]);
You need querying relationship existence. But first you must have relations between models.
Example:
If you need even more power, you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries. These methods allow you to add customized constraints to a relationship constraint, such as checking the content of a comment:
// Retrieve all posts with at least one comment containing words like foo%
$posts = App\Post::whereHas('comments', function ($query) {
$query->where('content', 'like', 'foo%');
})->get();
More: https://laravel.com/docs/5.6/eloquent-relationships#querying-relationship-existence
You seems getting the exams id so add it in the update query: Give a try like below:
DB::table('verifications')->with($exams)->->whereIn('exam_id', $exams->pluck('id') )->where([ ['confirmation', 'non'], ['modifier', 'oui'], ])->update(['confirmation' => "oui"]);
Update Answer: you already have "$exams" variable and so you don't need to join any table further: Try this:
DB::table('verifications')->whereIn('verification.exam_id', $exams->pluck('id') )->where([ ['confirmation', 'non'], ['modifier', 'oui'], ])->update(['confirmation' => "oui"]);