1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
use App\Snippet; |
7
|
|
|
|
8
|
|
|
class SnippetsController extends Controller |
9
|
|
|
{ |
10
|
|
|
public function __construct() |
11
|
|
|
{ |
12
|
|
|
$this->middleware('auth'); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* List all of the snippets. |
17
|
|
|
* |
18
|
|
|
* @return \Response |
19
|
|
|
*/ |
20
|
|
|
public function index() |
21
|
|
|
{ |
22
|
|
|
$snippets = Snippet::with('user')->latest()->paginate(5); |
23
|
|
|
$topUsers = DB::table('snippets_votes') |
24
|
|
|
->join('snippets', 'snippets.id', '=', 'snippets_votes.snippet_id') |
25
|
|
|
->join('users', 'users.id', '=', 'snippets.user_id') |
26
|
|
|
->select(DB::raw('count(*) as votes, name, username')) |
27
|
|
|
->groupBy('snippets.user_id') |
28
|
|
|
->orderBy('votes', 'desc') |
29
|
|
|
->limit(10) |
30
|
|
|
->get(); |
31
|
|
|
|
32
|
|
|
return view('snippets.index', compact('snippets', 'topUsers')); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Show a page to create a new snippet. |
37
|
|
|
* |
38
|
|
|
* @param Snippet $snippet |
39
|
|
|
* @return \Response |
40
|
|
|
*/ |
41
|
|
|
public function create(Snippet $snippet) |
42
|
|
|
{ |
43
|
|
|
return view('snippets.create', compact('snippet')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Show a single snippet. |
48
|
|
|
* |
49
|
|
|
* @param Snippet $snippet |
50
|
|
|
* @return \Response |
51
|
|
|
*/ |
52
|
|
|
public function show(Snippet $snippet) |
53
|
|
|
{ |
54
|
|
|
$snippet->load('forks'); |
55
|
|
|
|
56
|
|
|
return view('snippets.show', compact('snippet')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Store a new snippet in the database. |
61
|
|
|
* |
62
|
|
|
* @return \RedirectResponse |
63
|
|
|
*/ |
64
|
|
|
public function store() |
65
|
|
|
{ |
66
|
|
|
$this->validate(request(), [ |
67
|
|
|
'title' => 'required', |
68
|
|
|
'body' => 'required', |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$data = request()->only(['title', 'body', 'forked_id']); |
72
|
|
|
$data['user_id'] = auth()->user()->id; |
|
|
|
|
73
|
|
|
|
74
|
|
|
Snippet::create($data); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return redirect()->home(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: