Skip to content

Commit 728fd38

Browse files
Add column user_id to logs
1 parent e2ac702 commit 728fd38

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddUserToLog extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('youtube_logs', function (Blueprint $table) {
17+
$table->foreignId('user_id')->nullable()->after('id');
18+
19+
$table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('CASCADE');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::table('youtube_logs', function (Blueprint $table) {
31+
$table->dropConstrainedForeignId('user_id');
32+
});
33+
}
34+
}

src/Controllers/ApiController.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Http\Request;
1111
use Illuminate\Routing\Controller;
1212
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Facades\Auth;
1314
use Illuminate\Support\Facades\File;
1415
use Illuminate\Support\Facades\Storage;
1516
use Illuminate\Support\Facades\Validator;
@@ -106,7 +107,9 @@ public function convert(Request $request)
106107
$log->title = $video->getTitle();
107108
$log->duration = $video->getDuration();
108109
$log->format = $format;
109-
//$log->user()->associate(Auth::user()) todo ?
110+
111+
if(config('youtube-api.auth') !== null)
112+
$log->user()->associate(Auth::user());
110113

111114
$log->save();
112115
}

src/Models/Log.php

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MichaelBelgium\YoutubeAPI\Models;
44

5+
use Exception;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Log extends Model
@@ -11,4 +12,12 @@ class Log extends Model
1112
public $timestamps = ["created_at"];
1213

1314
const UPDATED_AT = null;
15+
16+
public function user()
17+
{
18+
if(config('auth.providers.users.model') !== null)
19+
return $this->belongsTo(config('auth.providers.users.model'));
20+
else
21+
throw new Exception('No user provider model defined.');
22+
}
1423
}

0 commit comments

Comments
 (0)