Jump to content
Endrick

Laravel Error : Trying to get property 'data' of non-object

Recommended Posts

Hello,

I get this error in Laravel: 
ErrorException Trying to get property 'data' of non-object

https://prnt.sc/u2tq2z Thats the line where is the error located

My Code;

    **data.blade,php**
   

@foreach($students as $row)
    <tr>
      <th scope="row">{{ $row->name }}</th>
      <td></td>
      <td></td>
      
    </tr>

    @endforeach

    **DataController.php**
   

    

 public function index()
        {
    
            
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://apingweb.com/api/rest/students',
            CURLOPT_RETURNTRANSFER => true,
            // CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
            // CURLOPT_USERPWD => 'ADMIN : SECRETE123',   <-----  for Basic Auth
            // CURLOPT_TIMEOUT => 30,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
            ),
        ));
    
        $response = curl_exec($curl);
    
        curl_close($curl);
    
        $result = json_decode($response);
          
       $students = $result->data;
    
        return view("data", compact("students"));
        }

 

Share this post


Link to post
Share on other sites
$result = json_decode($response);
          
$students = $result->data;

Whatever $response was, it was not valid JSON. Find out what it was.

Share this post


Link to post
Share on other sites
11 hours ago, Endrick said:

 

it works with php artisan serve but i want it to work without it

php artisan serve starts the php development server.  How do you plan to deploy/run the application if you are not going to use the php development server? 

Share this post


Link to post
Share on other sites

To run a web application you require a webserver.  To run a web application with php you also require some support for php integrated into the webserver.  There are a number of different webservers, the two most popular being apache and nginx.  There are a number of different ways of integrating php with the webserver.  With Apache you can choose to use mod_php which is an apache module that integrates php or you can use apache with php-fpm (fastcgi process manager).  With Nginx, your option is to use php-fpm.  Eventually people typically also will need a database or data persistence server like mongodb, and often will have a caching server like Redis.   

On your local machine you can install the webserver and everything else you will need and run those.  There are popular installation and configuration setups like WAMP and MAMP that have been around for a long time, and many people use.  I have mixed feelings about these.  The problem is that they install all these things on your workstation, some of which you might not want or need, and then they are running typically even when you don't need them because you aren't developing.  They also have to be upgraded and managed as time goes on.

In recent years there was a move by most developers and teams to using virtualization.  With virtualization, you can run a virtualized server on your workstation and interact with that server in the same way you might interact with a hosted server.  The great thing about virtualization is that you can start an environment when you want it and stop/pause it when you don't.  It's also isolated from your workstation.  This has many advantage including the ability to develop on the the same platform you will deploy to, as most servers are running some sort of linux, with the vast majority being either RHEL/Centos or Debian/Debian based distributions.  

Installing a virtualization manager like Virtualbox or Vmware facilitates the use of a provisioning manager like Vagrant.  With Vagrant you can use vagrant "boxes" which will download and start up a full virtualized server environment with all the server processes you might want or need.  This is how lots of people were doing development even a few years ago, and there are many people who probably still use Vagrant.

More recently many developers have moved to Docker.  Docker supports "containers" which are small environments that can run anything from a full server, to a single process.  I won't go into all the details, but with Docker you can start up the things you need  (for example apache/mod_php in one container, mysql in a 2nd container) and allow you to run your application locally while also allowing you the same iterative development process you might use with the php server.

To understand all of this, you also have to understand a fair amount about networking, dns, linux distros, package managers and shell scripting, as well as the support and configuration of a webserver. 

If you want to proceed, then I'd recommend learning about Docker.  There are some projects that have pre-built docker environments you can look at.  https://laradock.io/ is a popular one that is focused on laravel projects, but can be used for other php projects.  I've also heard good things about https://lando.dev/  although I haven't had a chance to experiment with it, and then there is the massive but generic LAMP http://devilbox.org/  

Any of these will help you get a jump start on using Docker as an alternative to installing everything locally.

At the end of the day, going to localhost:8000 vs localhost is not a sufficient reason to spend all the time going down the rabbit hole of all this stuff as you are still learning the basics of web development and php coding.

 

  • Like 1

Share this post


Link to post
Share on other sites

 

I already have that of course, but I would like to know whether it works without the command php artisan serve

Share this post


Link to post
Share on other sites

Make sure you have your error_reporting set to E_ALL and display_errors set to On in the php.ini file for your development environment so you can be informed of any errors that occur.

Dump the value of $response with var_dump($response); so you can see what you're getting as a response after curl_exec is complete.

Share this post


Link to post
Share on other sites
2 hours ago, Endrick said:

 

I already have that of course, but I would like to know whether it works without the command php artisan serve

You already have what?  php artisan serve starts up the development server on a port.  You are accessing the port.  When you say it "doesn't work" you would need to be a lot more specific about what you already have installed and configured.

Share this post


Link to post
Share on other sites

 

I have found a tutorial here in which this is also done without the port and I want that with my current code as well 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.