1
|
<?php
|
2
|
|
3
|
namespace App\Http\Controllers;
|
4
|
|
5
|
use App\ArtefactUser;
|
6
|
use Illuminate\Support\Facades\Auth;
|
7
|
use App\User;
|
8
|
use App\Artefact;
|
9
|
use App\Http\Controllers\Image;
|
10
|
|
11
|
class FavoriteArtefactsController extends Controller
|
12
|
{
|
13
|
public function __construct()
|
14
|
{
|
15
|
$this->middleware('auth');
|
16
|
}
|
17
|
|
18
|
/**
|
19
|
* Display a listing of the resource.
|
20
|
*
|
21
|
* @return \Illuminate\Http\Response
|
22
|
*/
|
23
|
public function index()
|
24
|
{
|
25
|
if(isset(Auth::user()->id))
|
26
|
{
|
27
|
$userId = Auth::user()->id;
|
28
|
$list = ArtefactUser::where('user_id', $userId)->get();
|
29
|
$finalData = array();
|
30
|
foreach($list as $item)
|
31
|
{
|
32
|
$tmp = Artefact::where('id', $item->artefact_id)->get();
|
33
|
$tmp['likes'] = Artefact::find($item->artefact_id)->users()->count();
|
34
|
array_push($finalData, $tmp);
|
35
|
}
|
36
|
|
37
|
$data = array(
|
38
|
'title' => 'Favorite artefacts',
|
39
|
'user' => User::find($userId),
|
40
|
'artefacts' => $finalData
|
41
|
);
|
42
|
return view('favartefacts.index') -> with($data);
|
43
|
}
|
44
|
else
|
45
|
{
|
46
|
$data = array(
|
47
|
'title' => 'Welcome to the MERLOT page',
|
48
|
);
|
49
|
//return view('index', compact('title'));
|
50
|
return view('pages.index') -> with($data);
|
51
|
}
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Display the specified resource.
|
56
|
*
|
57
|
* @param int $id
|
58
|
* @return \Illuminate\Http\Response
|
59
|
*/
|
60
|
public function show($id)
|
61
|
{
|
62
|
$list = ArtefactUser::where('user_id', $id)->get();
|
63
|
$finalData = array();
|
64
|
foreach($list as $item)
|
65
|
{
|
66
|
$tmp = Artefact::where('id', $item->artefact_id)->get();
|
67
|
$tmp['likes'] = Artefact::find($item->artefact_id)->users()->count();
|
68
|
array_push($finalData, $tmp);
|
69
|
}
|
70
|
|
71
|
$data = array(
|
72
|
'title' => 'Favorite artefacts',
|
73
|
'id' => $id,
|
74
|
'user' => User::find($id),
|
75
|
'userId' => Auth::user()->id,
|
76
|
'artefacts' => $finalData
|
77
|
);
|
78
|
return view('favartefacts.index') -> with($data);
|
79
|
}
|
80
|
|
81
|
}
|