Home > OS >  How to fetch single value from the array in laravel?
How to fetch single value from the array in laravel?

Time:11-17

I am getting the data from the user which is stored in one variable called $data.what my api is doing if i pass route url it should be displayed all books.if i pass query params as search=BookName it fetches the details of the particular searchable bookname

$data=request->all();
public function run(Array $data){
 if($data){
            if(!is_null($data['search'])){
                //it will execute
            }
            else
            return this code;
        }
}

what's my problem is if i hit api along with ?search=Book1 ,it's working fine .but if run without any search it's throwing an error ,please help me to fix the issue..

Error

Undefined index: search

CodePudding user response:

$data=request->all();

public function run(Array $data){
$Search =  $data['search'] ?? null;
 if($data){
            if($Search){
                //it will execute
            }
            else
      return // something;
   }
}

Here simply the $search is storing the value of $data['search'] so if there no value/key exists in $data['search'] we just store null

  • Related