How to Remove Query Part from $Request->All() in Laravel Framework

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:

enter image description here

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



Leave a reply



Submit