How to remove query part from $request-all() in Laravel framework?
While I cannot guarantee that I perfectly understood the question but based on my understanding, to filter out fields that are not sent as part of the request, then using request()->only(['field', 'field_2'])
should be sufficient given the instance stated in L5.5 upgrade notice about request->only
method:
It means given the example I have, if field_2
is not present in the request, then it is discarded.
If you are on Laravel BEFORE 5.5 then $request->all()
will give you only existing field, else for L5.5 you use request->only()
You can give it a try and see how they work.
ON Laravel <= 5.4 and also L5.5
Finally, if you still get those fields as null then you can simply use array_filter($request_array)
e.g:
$request_only = $request->all();
$requests_without_null_fields = array_filter($request_only);
This will remove all fields that are null preserving only fields that are not null.
Hope this is useful.
Laravel 5 - Remove Parameter From All Request Objects at Controller Level
Perhaps you want global middleware?
First arrange for the middleware to run on all routes:
// routes.php
$app->middleware([
App\Http\Middleware\Apitoken::class
]);
Then define what the middleware should do:
// src/App/Http/Middleware/Apitoken.php
<?php
namespace App\Http\Middleware;
use Closure;
class Apitoken
{
public function handle($request, Closure $next)
{
unset($request['api_token']);
return $next($request);
}
}
laravel Eloquent ORM delete() method
Before delete
, there are several methods in laravel.
User::find(1)
and User::first()
return an instance.
User::where('id',1)->get
and User::all()
return a collection of instance.
call delete
on an model instance will returns true/false
$user=User::find(1);
$user->delete(); //returns true/false
call delete
on a collection of instance will returns a number which represents the number of the records had been deleted
//assume you have 10 users, id from 1 to 10;
$result=User::where('id','<',11)->delete(); //returns 11 (the number of the records had been deleted)
//lets call delete again
$result2=User::where('id','<',11)->delete(); //returns 0 (we have already delete the id<11 users, so this time we delete nothing, the result should be the number of the records had been deleted(0) )
Also there are other delete methods, you can call destroy
as a model static method like below
$result=User::destroy(1,2,3);
$result=User::destroy([1,2,3]);
$result=User::destroy(collect([1, 2, 3]));
//these 3 statement do the same thing, delete id =1,2,3 users, returns the number of the records had been deleted
One more thing ,if you are new to laravel
,you can use php artisan tinker
to see the result, which is more efficient and then dd($result)
, print_r($result);
How to delete multiple row from DB table by laravel?
Since each Eloquent model serves as a query builder, try this, all in one line:
Comment::where('post_id',$id)->delete();
Tested in tinker, works as expected, returns count of deleted rows.
Documentation: https://laravel.com/docs/5.3/queries#deletes
Laravel Request getting current path with query string
Try to use the following:
\Request::getRequestUri()
How to get all query string from Request class in laravel 5.0
Personally i used this:
public function foo(Request $request)
{
$all = $request->all(); // there you have an array with all input submitted
//do something with input
return redirect()->action('HomeController@index');
}
How to change value of a request parameter in laravel
Use merge()
:
$request->merge([
'user_id' => $modified_user_id_here,
]);
Simple! No need to transfer the entire $request->all()
to another variable.
Read more about Laravel's merge()
here:
https://laravel.com/docs/collections#method-merge
Related Topics
Laravel PHP Fatal Error: Require(): Failed Opening Required ... Bootstrap/Autoload.Php on Line 17
Selected Value Get from Db into Dropdown Select Box Option Using PHP MySQL Error
How to Submit Multiple PHP Forms While Each Form Has Its Own Submit Button
How to Restrict User from Downloading or Saving Pdf File
Codeigniter Session Is Not Working on PHP 7
Php: How to Detect If a Session Has Expired Automatically
How to Bind an Array to an In() Condition in a Pdo Query
How to Parameterize the Table Name in a Prepared Statement
Laravel, Sync() - How to Sync an Array and Also Pass Additional Pivot Fields
Mysql Return Result If 0 Rows Returned
Executing Python Script in PHP and Exchanging Data Between the Two
How to Get Only Date from Datetime in Codeigniter
How to Install Imagick/Imagemagick PHP Extension on Windows 7
Telegram Bot Api: How to Send a Photo Using PHP
Phpexcel Write Long Number in Cell