Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fetch single value from the array in laravel?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Undefined index: search

>Solution :

$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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading