Creating dummy data types
Create a route
In the file routes/web.php, you will need to add an appropriate route. It will look something like:
Route::get('dummies/{id}', 'DummyController@show')->name('dummies.show');
- The first argument is the url of the page, where segments in curly braces are variable section that are passed to the controller. What you have between the braces doesn't matter and isn't used, but it would be good if it represented that data you're expecting to receive (usually id or slug);
- The second argument is a combination of the Controller class, and the function on that controller to call.
- The additional
namesets a string you can call using the route() helper e.g.route('dummies.show')to get the full url.
Create / Update the Controller for the route
- If creating a new controller, use the console command
php artisan make:controller DummyController - Add a public function to match the method called in the route declaration. At this point, work out what model(s) you need.
Create the relevant Model, if required
- If creating a new model, use the console command
php artisan make:model Dummy
Create a View to display this page.
Make a view for this page in an appropriate subfolder. If creating a show view for a single Dummy, make a file as resources/views/dummies/show.blade.php
Meanwhile, back in the controller...
Instanciate either a new model, or a collection of them (if you are creating a page that represents many of them, like an index)
Single
$dummy = new Dummy;
Collection
$dummies = (new Dummy)->newCollection();
$dummies->push(new Dummy);
$dummies->push(new Dummy);
$dummies->push(new Dummy);
Then just return your view, passing whatever data you require into it.
Putting it all together
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Dummy;
class DummyController extends Controller
{
/**
* Displays a single Dummy
*
* @return View
*/
public function show($id)
{
// Ignore the id for now, and just create a dummy item.
$dummy = new Dummy;
return view('dummies.show', compact('dummy'));
}
}