I am trying to delete a file after I delete the product from the database, but it shows me an error, and I cannot find a solution.
Call to a member function delete() on null
public function supprod($id,Request $request)
{
$produits=Produit::find($id);
$produits->delete(); // <-- error on this line
$filename=(string)$document->nom;
Storage::disk('fichier')->delete($filename);
return redirect()->back();
}
Image of the problem
this is check the record this function $produits=Produit::find($id);
This use $produits=Produit::where('id',$id)->first();
You should really let Laravel do the work for you.
Change your action to:
public function supprod(Produit $id, Request $request) {
$filename=(string)$id->nom;
Storage::disk('fichier')->delete($filename);
$id->delete();
return redirect()->back();
}
This will make the framework find the appropriate model or throw a 404 error if it does not exist.
More information in the implicit model binding section of the documentation
Related
I've been using Laravel-5.8.35. I was invoking a GET request through a form. On my route, I redirected the form action to the same controller where the form was submitted but redirected through a different route, as,
$router->get('/merchant/sd-refund', 'Reports\ReportController#refundSecurityDeposit');
And, in my refundSecurityDeposit method, I called my SohojSdRefundService service,
public function refundSecurityDeposit(Request $request)
{
// $userId, $reason, $sdAmount was fetched from the $request instance
$this->execRefundRequest($userId, $reason, $sdAmount);
}
public function execRefundRequest($userId, $reason, $sdAmount)
{
// here the API service request data was handled,
// and stored in $data instance
return SohojSdRefundService::callSdRefundApi($data);
}
While my SohojSdRefundService service was done handling, I wanted to redirect the route to another route, as,
class SohojSdRefundService
{
public function __construct()
{
}
public static function callSdRefundApi($requestData)
{
// call to other methods inside the class to handle the API response,
// and then return to the same route which isn't working
return redirect('/admin/merchant/list');
}
}
Respectively, instead of redirecting to that route, the page happens to be still on the /merchant/sd-refund?... where the form was submitted initially. I redirected another service like this, which is working fine though. Could anyone suggest what I could be implementing wrong here? TIA.
You need to return a result in refundSecurityDeposit fucntion
public function refundSecurityDeposit(Request $request)
{
return $this->execRefundRequest($userId, $reason, $sdAmount);
}
im using laravel 7
and im using gates and policies
i have api controller called Journal_entries_controller
and i have index function
public function index()
{
$journal_entries = Journal_entry::with('get_journal_entry_lines')->get();
return response()->json($journal_entries,200);
}
like this everything working so good ..
to check gate i did this ..
public function index()
{
$auth = auth('api')->user();
if(!Gate::allows('journal_entries.view',$auth))
return 'not auth';
$journal_entries = Journal_entry::with('get_journal_entry_lines')->get();
return response()->json($journal_entries,200);
}
like that i get not auth the code stop there
and if i dd($auth) i gat the logged user like this ..
public function index()
{
$auth = auth('api')->user();
dd($auth);
}
any help here thanks ..
In the if statement:
if(!Gate::allows('journal_entries.view',$auth))
return 'not auth';
The Gate::allows is returning false by adding the ! we make the response true which is why the not auth code is returning.
The first thing to do is make sure the journal_entries.view is a gate in the App\Providers\AuthServiceProvider.
If it is a valid gate please post the contents of the gate so we know the intended functionality.
In the mean time you may want to try changing
if(!Gate::allows('journal_entries.view',$auth)) to if(Gate::denies('journal_entries.view',$auth)) or
if(Gate::allows('journal_entries.view',$auth))
The docs for gates can be found here https://laravel.com/docs/7.x/authorization#authorizing-actions-via-gates
We have a DB with 100 users.. for example
And here is the route
Route::get('/users/{user}/edit', 'UserController#edit');
Here is the method
public function edit(User $user)
{
$hi = 'Hello';
return $hi;
}
Ok.. if I do something like this
http://localhost/users/99/edit | WORKS
http://localhost/users/100/edit | WORKS
http://localhost/users/101/edit | PROBLEM
How to solve when the user change the value from URL with an inexistent record..?
In this case I would pass the user id as a parameter rather than explicit binding:
Route::get('/users/{userId}/edit', 'UserController#edit');
When a model is not found you will get a ModelNotFoundException exception thrown, you can catch it and treat this case
Controller:
use Illuminate/Database/Eloquent/ModelNotFoundException;
[...]
public function edit($userId)
{
try{
$user = User::find($userId);
//do some stuff
} catch (ModelNotFoundException $e){
//treat error (log the activity, redirect to a certain page)
//or display a 404 page
//dealer's choice
}
}
In your case the user can't be found, which is a 404:
public function edit(User $user)
{
$user = false; // dummy/example
if ($user) {
return $your_results;
} else {
return view('errors.404');
// assuming you have a folder called 'errors' inside your 'views' folder
// and the file name is `404.blade.php`
// and are using blade files.
}
}
If the user can be found, show whatever you need to, else, show a 404 blade view.
A little late, but it is an useful question.
To suppress the error message you can set the debug mode in the (dot)ENV file to false APP_DEBUG=true. Due to the route model binding, the other solutions above won't help you, because if the model is not found, you won't get into the controller. Unless you don't get the model but only the id or slug etc. and then make a query against the database within the function.
When I submit a form in Laravel, the following controller method handles it:
public function update($id)
{
//handle input
return View::make('generic.success', ["message" => 'Data submitted successfully!']);
}
This works fine. However, instead of returning a view like above I'd like to return a redirect, because when I return the view directly, reloading the page resubmits the form.
So I tried to do this:
public function update($id)
{
//handle input
return Redirect::to('/success', ['message' => 'Data submitted successfully!']);
}
In my routes file I defined the success route:
Route::get('success', 'NotificationsController#success');
And set up a notification controller to display the view:
class NotificationsController extends BaseController {
public function success($message)
{
return View::make('generic.success', ["message" => $message]);
}
When I run the above code, I get the following error from Laravel:
InvalidArgumentException
The HTTP status code "1" is not valid.
I have no idea what this is supposed to tell me, and neither does Google apparently.
Can someone shed some light on this issue?
P.S.
Incidentally, being new to Laravel, I've noticed so far that Laravel's error reporting is very user-unfriendly, in that instead of telling me I have an issue with my router, or controller, or permissions, it displays these generic errors with no humane explanation of their cause. Is there a better way to troubleshoot problems in Laravel than relying on this?
For example, in the above incident, the error report points to this line of code...
public function setStatusCode($code, $text = null)
{
$this->statusCode = $code = (int) $code;
if ($this->isInvalid()) {
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
}
...which is completely useless, as all it does is show me the code that printed the error itself.
The second parameter of the redirector's to() method is the HTTP status code that will be returned by the response, not data that will be passed along. Passing data when redirecting to GET routes can be done either via the query string or the session. The recommended solution here is to pass data via the current session using the with() method which passes that data for the next request. So in your case this would be the approach needed:
public function update($id)
{
return Redirect::to('/success')->with('message', 'Data submitted successfully!');
}
Then in your success method you can have this:
public function success($message)
{
return View::make('generic.success', ["message" => Session::get('message')]);
}
When in doubt always try checking the documentation first. The solution to this is explicitly stated in the Laravel Response Redirects Documentation.
Thanks a lot -Bogdan I found in the documentation that you post answer to my problem. In my case the solution was redirect to an action in a controller, like this...
return
\Redirect::action(
'PqrController#solicitud',
array($id)
)
->with(
'message',
'¡El estado de la solicitud ha sido actualizado correctamente!'
)
;
I redirect to a method in a controller, with one parameter array($id) and I put too in the session a message using ->with('message','Mensaje')
I've just started to learn about laravel and I want to use this framework with It's advantages. I'm asking this question to learn the right way to this with laravel.
It is printing the post from posts table which has the same id with $id.
<?php
class PostsController extends BaseController{
public function singlePost($id)
{
$thePost = Posts::find($id);
return View::make('singlePost')->with('thePost', $thePost);
}
}
Normally I do check if there is a post that's id equal to $id and if that is, return view so forth. Isn't there better way to do this with laravel like you can do with route filters.
Shortly,
How to know if there is post with that id?
How to throw exception if there is not?
...
Route Model Binding might be an option however a more universal solution is findOrFail
findOrFail will either return the model or throw a ModelNotFoundException which will display as a 404 page.
$thePost = Posts::findOrFail($id);
return View::make('singlePost')->with('thePost', $thePost);
To just check for existence you can use find and then compare with null:
$thePost = Posts::find($id);
if($thePost != null){
// post exists
}
Or simpler, just a truthy value:
$thePost = Posts::find($id);
if($thePost){
// post exists
}
See "Route Model Binding" in the documentation.
Route::model('post', 'Post', function() {
// do something if Post is not found
throw new NotFoundHttpException;
});
Route::get('post/{post}', function(Post $post) {
return View::make('singlePost')->with('thePost', $post);
});
You could also just replace find() with findOrFail() in your code, which would throw an exception of a post wasn't found with that ID.