Passing parameters in Laravel 5.2 Registration Form Update-proof - php

I am using the standard Laravel 5.2 authentication.
But i want to have certain variables available in my Registration view
The route that i'm interested in is this one:
Route::get('register', 'Auth\AuthController#showRegistrationForm');
The method showRegistrationForm is created in a trait called RegistersUsers , This trait is located in Illuminate\Foundation\Auth .
public function showRegistrationForm()
{
if (property_exists($this, 'registerView')) {
return view($this->registerView);
}
return view('auth.register');
}
I can just simply pass trough my parameters here, but the problem is that this file is located in the vendor directory , so when i run Composer Update my changes will be overwritten & my website would break. Is there a update-proof way to do this?

You can overwrite the method in your AuthController:
class AuthController extends Controller
{
....
public function showRegistrationForm()
{
$data = ['foo', 'bar'];
if (property_exists($this, 'registerView')) {
return view($this->registerView, compact('data'));
}
return view('auth.register', compact('data'));
}
}

Related

Laravel route resolving to a different method

I'm trying to set up a basic Laravel CRUD application, and I'm getting stuck setting up the pages for each action.
When I visit the route case/create, it opens the page for show instead.
routes/web.php
use App\Http\Controllers\HospitalCase as HospitalCase;
Route::controller(HospitalCase::class)->group(function() {
Route::get('/cases','index')->middleware('auth')->name('cases');
Route::get('/case/{id}','show')->middleware('auth');
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/edit/{$id}','edit')->middleware('auth');
Route::post('/case/edit/{$id}','update')->middleware('auth');
Route::get('/case/delete/{$id}','destroy')->middleware('auth');
});
HospitalCase.php controller
class HospitalCase extends Controller
{
function index()
{
echo 'index';
}
function create()
{
echo 'create';
}
function show($id)
{
echo 'show';
}
function store()
{
// validation rules
}
function edit($id)
{
return view('case/edit');
}
function update($id)
{
}
function destroy($id)
{
}
}
This is what I see on the browser:
I have been trying to figure this out for hours and can't think of what I'm doing wrong.
PS: The auth middleware is using laravel breeze (unmodified)
The reason it's showing the show route is because you defined
Route::get('/case/{id}','show')->middleware('auth');
before it, therefore, it's matching case/create as show('create')
Try defining the route afterwards.
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/{id}','show')->middleware('auth');
Just want to reiterate what #TimLewis has suggested, I think you need to put this route:
Route::get('/case/create','create')->middleware('auth');
Above this route:
Route::get('/case/{id}','show')->middleware('auth');
But you could try using Laravel’s route resource so you don’t need to write out all the routes -
use App\Http\Controllers\HospitalCaseController;
Route::resource('case', HospitalCaseController::class);

how to block a user for some blade unsing laravel 6?

I created a small site by laravel 6, with the four blade index, create, edit, show and an authentication system, I want everyone to see the blades index and show, and the blades create and edit prohibit that if user authenticate.
TinghirsController.php
public function __construct() {
$this->middleware('auth');
}
public function index()
{
$tinghirs=Tinghir::orderBy('created_at','desc')->paginate(30);
return view('tinghirs.index', ['tinghirs' => $tinghirs]);
}
public function create()
{
return view('tinghirs.create');
}
public function show($id){
$tinghirs = Tinghir::where('id',$id)->firstOrfail();
return view('tinghirs.show', ['tinghirs' => $tinghirs]);
}
public function edit($id) {
$tinghir = Tinghir::find($id);
return view('tinghirs.edit', ['tinghir' => $tinghir]);
}
Route/web.php
Route::resource('tinghirs','TinghirsController');
As per the documentation, you can specify which controller methods you want to apply a piece of middleware to. In you're case you want to apply the auth middleware to all methods except index and show.
To achieve change the middleware call in your __constructor method to be:
$this->middleware('auth')->except('index', 'show');

Laravel AdminLTE - How to call method and use its data in view

I am using Laravel AdminLTE and I have it all configured, there is just one part I do not understand. I made my route like so:
Route::get('/admin/painlevel', function () {
return view('painlevel');
});
and I have this method in app/Http/Controllers/v1/PainLevelController.php
public function index()
{
return PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
}
How would I call that method and display the data in my painlevel view?
Your current route is merely returning the view('painlevel') directly.
You need to update your route to:
Route::get('/admin/painlevel', 'V1\PainLevelController#index');
In your controller:
public function index()
{
$data = PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
return view('painlevel', compact('data'));
}
You might want to start glancing through the documentations, start with Route, Controller and View
route create like this
Route::get('/administrator', 'administrator\LoginController#index');
and controller create like this
public function index()
{
$data['title']="Admin | DashBoard";
$data['name']="Dilip Singh Shekhawat";
view('administrator/menu_bar',$data);
return view('administrator/dashboard',$data);
}
its working .

How can I use the show method of my resource controller on a soft deleted model?

So I have a resource controller called ProjectController and I added soft delete and use it as an archive.
Route::get('project/archive', 'ProjectController#trash')->name('project.archive');
Route::resource('project', 'ProjectController');
In this archive, I have a list of the projects.
public function trash()
{
$projects = Project::onlyTrashed()->get();
return view('projects.archive', compact('projects'));
}
Now I want to use the show method to view these projects.
In my \App\Providers\RouteServiceProvider I added:
Route::bind('project', function ($value) {
return \App\Project::withTrashed()->find($value);
});
But this way, I am able to edit the project.
I tried to bind project/show, project/{project}/show, but that does not work.
How can I use the show method of my resource controller on a trashed project?
My complete ProjectController looks like:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProjectRequest;
use App\Project;
class ProjectController extends Controller
{
public function index()
{
$projects = Project::all();
return view('project.index', compact('projects'));
}
public function create()
{
return view('project.create');
}
public function store(ProjectRequest $request)
{
Project::create($request->all());
return redirect()->route('project.index');
}
public function show(Project $project)
{
return view('project.show', compact('project'));
}
public function edit(Project $project)
{
return view('project.edit', compact('project'));
}
public function update(ProjectRequest $request, Project $project)
{
$project->update($request->all());
return redirect()->route('project.index');
}
public function destroy(Project $project)
{
$project->delete();
return redirect()->route('project.archive');
}
public function trash()
{
$projects = Project::onlyTrashed()->get();
return view('project.archive', compact('projects'));
}
}
By adding a check in the \App\Providers\RouteServiceProvider.
Route::bind('project', function ($value) {
if (Route::currentRouteName() === 'project.show') {
return \App\Project::withTrashed()->find($value);
}
return \App\Project::find($value);
});
Hopefully this will help others.
I may be completely misunderstood the problem, but as far as I understood:
You can exclude some functions of a controller, such as;
Route::resource('photos', 'PhotoController')->only([
'index', 'show'
]);
Route::resource('photos', 'PhotoController')->except([
'create', 'store', 'update', 'destroy'
]);
More detailed information on https://laravel.com/docs/5.7/controllers#restful-partial-resource-routes
Additionally, if you want to implement a resource controller that uses only trashed projects -rather than a regular laravel model- as a resource you have to generate a new controller via something like
php artisan make:controller -r ProjectArchiveController
and customize the functions of it accordingly. For more options on make:controller command, you can use;
php artisan help make:controller
Hope this all helps.
New addition:
Route::get('project/{id}', 'ProjectController#trash')->name('project.archive');
Route::resource('project', 'ProjectController');
and declaration of the function trash,
public function trash(int $id)
You can add a withTrashed() method to the routes file. From the laravel docs https://laravel.com/docs/8.x/routing#implicit-soft-deleted-models
use App\Models\User;
Route::get('/users/{user}', function (User $user) {
return $user->email;
})->withTrashed();
I also posted on laracasts.com, where I partly borrowed from OP's answer.
I use this not for an archive of soft-deleted models, but for a restore route. But the problem is the same: route model binding only works for non-trashed models. I wanted a solution that:
works on all models which use the SoftDeletes trait;
only works on routes which are specifically meant for restoring soft-deleted models;
only works on models which are actually soft-deleted;
Therefore, I utilized the resolveRouteBinding() method (originally in \Illuminate\Database\Eloquent\Model), creating a more generic solution:
\App\Models\Traits\SoftDeletes:
<?php
namespace App\Models\Traits;
use Illuminate\Database\Eloquent\SoftDeletes as EloquentSoftDeletes;
use Illuminate\Support\Facades\Route;
trait SoftDeletes {
use EloquentSoftDeletes;
/**
* Retrieve the model for a bound value.
*
* Modified from \Illuminate\Database\Eloquent\Model::resolveRouteBinding()
*
* #param mixed $value
* #param string|null $field
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value, $field = null) {
$route_name_parts = explode(".", Route::currentRouteName());
if (end($route_name_parts) === 'restore') {
return $this->onlyTrashed()->where($field ?? $this->getRouteKeyName(), $value)->first();
}
return $this->where($field ?? $this->getRouteKeyName(), $value)->first();
}
}
For OP's archive, just add another check on the route name in that if-statement.
On the model, I swap the Eloquent version of SoftDeletes for this one. Now I can just define a route as:
Route::patch(
'/projects/{project}/restore',
[ProjectController::class, 'restore']
)->name('projects.restore');
Thus only routes with a name ending in 'restore' will work on a trashed model, all other routes only work on non-trashed models.
Note
With this, the restore routes exclusively work with trashed models. If you want them to work with non-trashed models as well (but why?), just replace the onlyTrashed() method with the withTrashed() method.

Bind a primitive to Laravel IoC container and resolve in a controller method

I'm trying to resolve a primitive inside a controller method.
This is the register method of my Provider:
public function register()
{
$this->app->when('App\Http\Controllers\InvalidCustomerController')
->needs('$customers')
->give(function () {
return InvalidCustomer::latest()
->paginate(20);
});
}
And this is the controller method I'm trying to resolve $customers:
public function index($customers)
{
return view(
'customer.invalid.index',
compact('customers')
);
}
$customers is not filled.
Everything will work if I resolve that on constructor.
What am I doing wrong?
ps: I'm using Laravel 5.2
Not sure if you found a solution but to use a primitive in a controller, pass it through the __constructor of the controller class as so:
private $customers;
public function __construct($customers) {
parent::__construct();
$this->customers = $customers;
}
This $customers variable can then be used elsewhere inside of the class.

Categories