Delete record error with custom method model laravel - laravel

User model
public function take($id){
return $this->find($id);
}
public function kill(){
return $this->delete();
}
Route Error 1
Route::get('delete/{userid}', function($id)
{
$user = new User;
$user->take($id); //result the content of $id
$user->kill();
});
i can't delete record with these route and only show blank page (without error).
Route errror 2
Route::get('delete/{userid}', function($id)
{
User::take($id)->kill();
});
And with above route i get error Non-static method User::take() should not be called statically
But i can delete with this route
Route::get('show/{userid}', function($id)
{
$user = new User;
$user->take($id)->kill();
});
So how to fix Route error 1, if i want to use $user-> without chain take() and kill() ? if it possible
How to fix Route error 2, if i want to use User:: , and why these error happen ?
Thanks in advance.

Try below :
Route::get('show/{$id}', function($id)
{
$user = new User;
$user->find($id)->kill();
});
I think the param accepted has to have the same things that is passed to the closure.

Related

Authorization isn’t executed in laravel

This is form ‘login’, when I’m enter information in input and after press button ‘login’ I move to page with audit where 0 is authorization isn't executed and 1 is authorization is executed. I'm trying to create authorization in laravel.
I did it in other project in order to understand how it works and then all was good. Now I'm trying to transfer it in my main project but when I am logging in nothing work.
I don't have any mistake, authorization is simply not executed. I will grateful for any help.
Registration function
public function sub(ContactSignup $request){
if(Auth::check()){
return redirect(route('user.mainpage'));
}
$contact = new SignUps();
$contact->name = $request->name;
$contact->surname = $request->surname;
$contact->age = $request->age;
$contact->password = bcrypt($request->password);
$contact->email = $request->email;
$contact->save();
Auth::login($contact);
if($contact){
Auth::login($contact);
return redirect(route('user.sign-up'))->with('success', 'Реєстрація пройшла успішно');
}
}
Function login
public function subin(ContactSignin $request){
if(Auth::check()){
return redirect()->intended(route('user.mainpage'));
}
$contact = $request->only(['email', 'password']);
if(Auth::attempt($contact)) {
dd(1);
}
else {
dd(0);
}
return redirect()->intended(route('user.mainpage'));
}
Web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return redirect()->route('mainpage');
});
Route::name('user.')->group(function(){
Route::view('mainpage', 'mainpage')->middleware('auth')->name('mainpage');
Route::get('/signin', function(){
if(Auth::check()){
return redirect(route('user.mainpage'));
}
return view('signin');
})->name('sign-in');
Route::post('/signin', [\App\Http\Controllers\ContactController::class, 'subin'])->name('sign-in');
Route::get('logout', function(){
Auth::logout();
return redirect('/');
})->name('logout');
Route::get('/signup', function(){
if(Auth::check()){
return redirect(route('user.mainpage'));
}
return view('signup');
})->name('sign-up');
Route::post('/signup', [\App\Http\Controllers\ContactController::class, 'sub'])->name('sign-up');
});
Route::get('/mainpage', function () {
return view('mainpage');
})->name('mainpage');
in your methods inside your controller you have form request class called ContactSignin
in this class you have code
public function authorize()
{
return false;
}
make it true
The form request class is responsible of validating your request ,
also contains an authorize method. Within this method, you may determine if the authenticated user actually has the authority to update a given resource
since you handle authorization logic for the request in the routes by provide
middleware('auth') it is no need to check for use authentication in your form request class
first you don't have route named ('user.mainpage') you have to define it in web.php
second , since you named your rout in
Route::get('/mainpage', function () {
return view('mainpage');
})->name('mainpage');
, you must redirect to the name of the route not to the path
for example if your route is ('/api/main')->name('main') , you have to put the rout name in the redirect method , for example redirect(route('main'));

redirect error exception array to string conversion Laravel 8

im trying to redirect my delete function back to index page that have parameter team. but it keeps throwing me Error Exception array to string conversion.
here my index fucntion on MonitorController.php
public function index(Team $team)
{
$team = Team::where('id',$team->id)->first();
$objective = Objective::with('keyresult')
->where('team_id',$team->id)
->get();
$objective = Objective::with('task')
->where('team_id',$team->id)
->get();
$objective = Objective::with('deadline')
->where('team_id',$team->id)
->get();
return view('/sistem/monitor/index', compact('objective','team'));
}
and here is my delete function inside the same file MonitorController.php
public function destroy(Team $team, Objective $objective, Deadline $deadline)
{
//for deleting objective
Objective::destroy($objective->id);
Deadline::destroy($deadline->id);
return redirect()->action([MonitorController::class, 'index',['team'=>$team]])->with('status', 'Objective Successfully Deleted');
}
here the index and destroy route
Route::get('/sistem/monitor/index/{team}', 'MonitorController#index');
Route::delete('/sistem/monitor/objective/details/{team}/{objective}', 'MonitorController#destroy');
Route::get('/sistem/monitor/index/{team}', 'MonitorController#index')->name('sistem.monitor.index');
And in your controller:
return redirect()->route('sistem.monitor.index', ['team' => $team])->with('status', 'Objective Successfully Deleted');
Inside the route you should always pass the route name. In your route on web.php you specify a custom route name by doing: ->name('sistem.monitor.index')

Redirect from controller to named route with data in laravel

I'm gonna try to explain my problem:
I have a named route called 'form.index' where I show a html form.
In FormController I retrieve all form data.
After do some stuff with these data, I want to redirect to another named route 'form.matches' with some items collection.
URLS
form.index -> websiteexample/form
form.matches -> websiteexample/matches
FormController
public function match(FormularioRequest $request)
{
// Some stuffs
$list = /*Collection*/;
return redirect()->route('form.matches')->with(compact('list'));
}
public function matches()
{
// How to retrieve $list var here?
return view('form.views.matches')->with(compact('list'));
}
The problem:
When the redirects of match function occurs, I get an error "Undefined variable: list' in matches funcion.
public function match(Request $request)
{
// Operations
$list = //Data Collection;
return redirect()->route('form.matches')->with('list',$list);
}
In view
#if(Session::has('list'))
<div>
{!!Session::get('list')!!}
</div>
#endif
You can use Redirect::route() to redirect to a named route and pass an array of parameters as the second argument
Redirect::route('route.name',array('param1' => $param1,'param2' => $param2));
Hope this helps you.

redirect too many times laravel

I try to use redirect->action It error too many redirect in my Controller
try {
//If check urL category null
if (is_null($category)){
Log::error("[Front] MenuController#menu : notfound public category ");
//error redirect
return redirect()->action('Front\HomeSlideviewController#index', $url);
}
} catch (\Exception $e) {
return 'error';
}
here is my web.php
Route::get('/{url?}', 'MenuController#menu');
Route::get('/{name?}', 'HomeSlideviewController#index')->name('promotiondetail');
I try to make a fucntion if Url is empty use redirect action
Both of your routes are identical and only the first one is matched.
Please note that, when redirecting to action, Laravel is resolving your action to route anyway, so it is the same as redirecting to route name (which is more bulletproof). By the way, the second parameter should be an array.
return redirect()->action('Front\HomeSlideviewController#index', $url);
To do what you want, you need one catchAll action and return different responses based on your logic:
/**
* #param $string
* #return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function catchAllAction($string)
{
$page = Page::whereHas('translations', function ($query) use ($string) {
$query->where('locale', App::getLocale())->where('slug', $string);
})->first();
if ($page) {
return $this->showPage($page);
}
$news = News::whereHas('translations', function ($query) use ($string) {
$query->where('locale', App::getLocale())->where('slug', $string);
})->first();
if ($news) {
return $this->showSingleNews($news);
}
throw new NotFoundHttpException('This page does not exist');
}
You have route conflict, it will call first route every time.
try to change your route.

Passing route parameter to controller Laravel 5

I'm trying to pass a route parameter to controller, but I get this error : Argument 2 passed to App\Http\Controllers\JurnalController::store() must be an instance of App\Http\Requests\JurnalRequest, none given
Below are the codes ..
Route :
Route::get('/edisi/{id}', 'JurnalController#store');
Controller :
public function store($id, JurnalRequest $request) {
$input = $request->all();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
$jurnal = Edisi::findOrFail($id)->jurnal()->create($input);
return redirect('jurnal');
}
So my question is how to pass the route parameter properly ? Thank you
new routes :
Route::get('/', function () {
return view('pages/home');
});
Route::group(['middleware' => ['web']], function () {
Route::get('edisi', 'EdisiController#index');
Route::get('edisi/create', 'EdisiController#create');
Route::get('edisi/{edisi}', 'EdisiController#show');
Route::post('edisi', 'EdisiController#store');
Route::get('edisi/{edisi]', 'EdisiController#edit');
Route::patch('edisi/{edisi}', 'EdisiController#update');
Route::delete('edisi/{edisi}', 'EdisiController#destroy');
});
Route::get('/edisi/{id}', 'JurnalController#storejurnal');
Route::group(['middleware' => ['web']], function () {
Route::get('jurnal', 'JurnalController#index');
Route::get('jurnal/create', 'JurnalController#create');
Route::get('jurnal/{jurnal}', 'JurnalController#show');
Route::post('jurnal', 'JurnalController#storejurnal');
Route::get('jurnal/{jurnal}/edit', 'JurnalController#edit');
Route::patch('jurnal/{jurnal}', 'JurnalController#update');
Route::delete('jurnal/{jurnal}', 'JurnalController#destroy');
});
new storejurnal method :
public function storejurnal(JurnalRequest $request, $id) {
$input = $request->all();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
//Insert data jurnal
$jurnal = Edisi::findOrFail($id)->jurnal()->create($input);
return redirect('jurnal');
}
When you are using resource controller, the store method does not accept any other argument except the Request instance. Try changing the method name or remove the second argument. store() method be default accepts post requests not get requests. Either put your route on top of the resource controller or change the method name.
Route::get('/edisi/{id}', 'JurnalController#store');
Route::resource('jurnals', 'JurnalController');
I hope this helps.
The correct format is:
public function store(JurnalRequest $request, $id) {
// your code
}
If you receive an argument such as Missing argument 2 as suggested in your comments, it means that either you aren't generating the routes correctly, or the url doesn't include the id segment.

Resources