how to update two data in two different tables ?

Published 2 months ago by wardaddy

I started learning laravel a week ago, and I am still confused by how to update data and my english is not good

I have two tables and I want to update two data on different tables at a time, what can i do, can someone tell me?

users table

  • id --> "1"
  • type_id --> "3"
  • name --> "john"
  • email --> "[email protected]"
  • password --> "123"

channels table

  • id --> "1"
  • user_id --> "null"
  • name --> "channel #1"

*type_id --> 1 = admin, 2 = dj, 3 = user

this is what i need

if the user clicks on the "take channel" button, I want to update the type_id in the users table from "3" to "2", which means the user has changed to a dj, and also fill user_id in the channel table with the id of the user who clicked the button, can anyone tell me how?

I'm not sure of my code, but here's the code that comes to my mind

UserController.php

public function becomeDJ()
{
    $channel = App\Channel::find('id');
    $channel->user_id = Auth::user()->id;
    $channel->save();

    $user = App\User::find('type_id');
    $user->type_id = '2';
    $user->save();

    return view('dashboard.editChannel');
}

becomeDJ.blade.php

@foreach ($channels as $channel)
    <div class="col s12 m4 l3">
        <div class="card grey lighten-4">
            <div class="card-content p-3">
                <h5 class="truncate mt-0 mb-0">{{ $channel->name }}</h5>
                <div class="d-flex justify-content-between">
                    <div>
                        <p>DJ : Username</p>
                        <small>ID Channel : {{ $channel->id }}</small>
                    </div>
                    <a href="/become-dj/{{ $channel->id }}/process" class="btn-flat"><i class="material-icons">flag</i></a>
                </div>
            </div>
        </div>
    </div>
@endforeach

routes/web.php

Route::get('/become-dj/{channel}/process', '[email protected]');
Best Answer (As Selected By wardaddy)
ftrillo

I see

$user = Auth::user(); // This will get you the user that is currently logged in
$user->type_id = '2';
$user->save();

$channel = Channel::find($id); // $id is the id of the channel. It probably comes from the request
$channel->user_id = $user->id;
$channel->save();

That should work. If you want an user other than the authenticated one, you can just user find(), like I did with the channel.

Also, you probably want to save both models inside a transaction, so that if something goes wrong between the two queries, you don't get corrupted data.

That would look like this:

$user = Auth::user();
$user->type_id = '2';

$channel = Channel::find($id);
$channel->user_id = $user->id;

DB::transaction(function() use  ($user, $channel) {
    $user->save();
    $channel->save();
});

I hope this cleared things up a bit for you.

ftrillo

Explain what are you trying to do, clearly please. Because it doesn't make sense to me.

public function becomeDJ()
{
    // Here you find a channel whose primary key value is 'id'.  That's weird.
    $channel = App\Channel::find('id');

    $channel->user_id = Auth::user()->id;
    $channel->save();

    // Here you find an user whose primary key value is 'type_id'. Also weird.
    $user = App\User::find('type_id');
    $user->type_id = '2';
    $user->save();

    // Here you are returning a view. It would make more sense to redirect to the edit route. Or redirect back to wherever the user making the request came from.
    return view('dashboard.editChannel');
}
wardaddy

@ftrillo can you tell me what I can do?

this is what I am trying to do

if the user clicks on the "take channel" button,

<a href="/become-dj/{{ $channel->id }}/process" class="btn-flat"><i class="material-icons">flag</i></a>

I want to update the type_id in the users table from "3" to "2", which means the user has changed to a dj,

$user = App\User::find('type_id'); //trying to retrieve the id from the user who clicked the button
$user->type_id = '2'; //change the id from "3" to "2", which means the user has changed to a dj
$user->save(); //and save it to the database

and also fill user_id in the channel table with the id of the user who clicked the button

$channel = App\Channel::find(id);  //trying to retrieve the id from the channel selected by the user

$channel->user_id = Auth::user()->id; //updating the user_id on the channel table with the id of the user who clicked on the button
$channel->save(); //and save it to the database
ftrillo

I see

$user = Auth::user(); // This will get you the user that is currently logged in
$user->type_id = '2';
$user->save();

$channel = Channel::find($id); // $id is the id of the channel. It probably comes from the request
$channel->user_id = $user->id;
$channel->save();

That should work. If you want an user other than the authenticated one, you can just user find(), like I did with the channel.

Also, you probably want to save both models inside a transaction, so that if something goes wrong between the two queries, you don't get corrupted data.

That would look like this:

$user = Auth::user();
$user->type_id = '2';

$channel = Channel::find($id);
$channel->user_id = $user->id;

DB::transaction(function() use  ($user, $channel) {
    $user->save();
    $channel->save();
});

I hope this cleared things up a bit for you.

wardaddy

@ftrillo this works like a charm, thanks a lot for helping me

ftrillo

@wardaddy I'm glad it worked. Don't forget to mark this as solved.

Please sign in or create an account to participate in this conversation.