Created
June 6, 2020 13:37
-
-
Save JethroPasco/3b732a58a3361edbe21dae69effefb97 to your computer and use it in GitHub Desktop.
Laravel Controller View Responsables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers; | |
| use App\Task; | |
| use App\Http\Responses; | |
| use App\Http\Controllers\Controller; | |
| class DashboardController extends Controller | |
| { | |
| public function __invoke($id) | |
| { | |
| $tasks = Task::all(); | |
| return new DashboardResponse($tasks); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Responses; | |
| use Illuminate\Contracts\Support\Responsable; | |
| class DashboardResponse implements Responsable | |
| { | |
| protected $tasks; | |
| public function __construct($tasks) | |
| { | |
| $this->$tasks = $tasks; | |
| } | |
| public function status() | |
| { | |
| return 200; | |
| } | |
| public function toResponse() | |
| { | |
| $data = $this->prepareData(); | |
| if (request()->ajax()) { | |
| return response()->json($data); | |
| } | |
| return view('dashboard', $data); | |
| } | |
| protected function prepareData() | |
| { | |
| return [ | |
| 'tasks' => $this->tasks | |
| ]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment