Hey does anyone have some knowedlge on getting a custom module to talk with the UserRepo? I created a module, added the Repo as it was add on the frontend users listing. But I am getting nothing. When I try to print the array it comes back as php memory exceeded, however I do not believe its a php memory issue. My code for the module controller is:
<?php
namespace Modules\Staff\Http\Controllers\Admin;
use App\Contracts\Controller;
use App\Models\Enums\UserState;
use App\Repositories\Criteria\WhereCriteria;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use League\ISO3166\ISO3166;
use Prettus\Repository\Exceptions\RepositoryException;
/**
* Class AdminController
* @package Modules\Staff\Http\Controllers\Admin
*/
class AdminController extends Controller
{
private $userRepo;
/**
* @param UserRepository $userRepo
*/
public function __construct(UserRepository $userRepo)
{
$this->userRepo = $userRepo;
}
public function index(Request $request)
{
$where = [];
if (setting('pilots.hide_inactive')) {
$where['state'] = UserState::ACTIVE;
}
try {
$this->userRepo->pushCriteria(new WhereCriteria($request, $where));
} catch (RepositoryException $e) {
Log::emergency($e);
}
$users = $this->userRepo
->with(['airline', 'current_airport'])
->orderBy('pilot_id', 'asc');
return view('staff::admin.index', [
'users' => $users,
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('staff::admin.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
}
/**
* Show the specified resource.
*/
public function show()
{
return view('staff::admin.show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit()
{
return view('staff::admin.edit');
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request)
{
}
/**
* Remove the specified resource from storage.
*/
public function destroy()
{
}
}
Thanks! Do you happen to know a good way to insert into the database? I’m lost after the form submits and does it’s checks… not sure where the actual insert sql command is! 🤪
Figured that out. But having an issue on finding where findWithoutFail functions are stored.
Do you happen to know a good way to insert into the database?
I would get the form values from the submission using a Request (or App\Requests\Request…) $request, then I would validate it using $request->validate([…]) (although it might be done automatically), then call $this->REPO->create([..]) to insert it.
1 hour ago, nickkecooper said:
But having an issue on finding where findWithoutFail functions are stored.
Not sure what you mean here, if it’s where the code actually is, it should be here
and it should auto-fill the values based on the model data (e.g. $users) you have, provided you have setup the Form::model().. binding before this. For more info, checkout how Nabeel does it in the admin area (airports for example), or view the Collective HTML docs here.
If you wanted to make it more traditional, you could also do something like this:
\<form action="{{ route('/users', $user-\>id) }}" method="post"\> @csrf @method('PUT') OR PATCH \<div class="form-group"\> \<label for="..."\>FIELDNAME TO DISPLAY\</label\> \<input type="text" name="FIELDNAME" id="..." class="form-control" value="{{ old('FIELDNAME', DEFAULT VALUE) }}" /\> OLD will display the last data the user has entered if it fails validation. @errors fields... \</div\> \</form\>
What’s your routes that you have put in Routes/web.php (or AppServiceProvider)?
Edit: Just saw you posted at the same time haha. It could be related to the module issue on github, but what routes have you defined? Also, did it throw any errors or just a “Page Not Found”?
The name(‘update’) bit is what the route() uses to get the name, so by default it should be MODULENAME.update or (::) but it might not be working right now because of the issue.
I made it this now. Still isn’t working, saying it is undefined. I also changed the admin.php line to show what you had put. Maybe it is an issue with the modules.
You have to set a name for the route with ->name(“admin.update”); and then call it with “staff.admin.update”. If you look at the installer module you’ll see it. I thought it defined route names automatically but only for resources
Fixed! Thank you Now when I do it the correct way on the create.blade.php, I get this error Column not found: 1054 Unknown column ‘_token’. Should I do an except command on my insert?
No, the _token is the CSRF token so that’s not stored in the database at all. Just realised that the CSRF token has been disabled (not sure why), so if you have anything to do with @csrf or _token in your form just remove it for now.