Home > Back-end >  How to retrieve data from another table in MySQL while having a data from column from the first tabl
How to retrieve data from another table in MySQL while having a data from column from the first tabl

Time:06-16

Before I start, I am using Laravel 8 & PHP 7.4.29.
The effect I want to achieve is that you can get the username from another table (of users) based on column from the first table (of videos).

Let's take a closer look on the first table. It contains such columns as title, description, uploader (having a value of UUID of the user), etc. The problem I am facing is that I am actually unable to retrieve the username I want. Instead of, I am getting such error:

Trying to get property 'username' of non-object

and the code is:

(the code is written properly, but Stack Overflow syntax highlighting is broken, I guess)

<?php
$videos = DB::select("select * from videos where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2]);
    foreach ($videos as $video) {
        $users = DB::select("select * from users where uuid = ?", [$video->uploader]);
?>
<div >
    <a href="/video?id={{{ $video->videoid }}}">
        <div >
            <div >
                <div >
                    <!-- the value below is a placeholder so far -->
                    <span >4:51</span>
                </div>
            </div>
            <div >
                <h4 >{{{ $video->title }}}</h4>
                <p >{{{ $users->username }}} ● {{{ date('Y/m/d', strtotime($video->uploaddate)); }}}</p>
                <p >{{{ $video->description }}}</p>
            </div>
        </div>
    </a>
</div>
<?php
    }
?>

Assigning the value to a new variable did not work, too.

I've browsing Stack Overflow a while ago and found the solution, that I should retrieve data from both tables in one SQL request (in my case - select * from videos, users). It somehow worked, but instead of expected result (I have 1 video information in the first table & 2 users in the second table), the results were duplicated.

I actually had some experience with PHP and Laravel back then, but I can't recall how could this be done (I've took a fairly big break from programming in PHP). If there's any solution, it would be welcome.

Thanks in advance!

CodePudding user response:

its not the good approach to access model from view on laravel. you should do it in controller. however u should use get() for mltiple data and fist() method for single data to get actual data object . your code should look like

<?php
$videos = DB::select("select * from videos where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2])->get();
foreach ($videos as $video) {
    $users = DB::select("select * from users where uuid = ?", [$video->uploader])->first();

?>

CodePudding user response:

Use Joins

in your case, Use left join if you have videos without matching uploader and if you need all the videos.

also you can use inner join if you want to get videos only if the matching uploader exists in the table.

select videos.*,users.* from videos LEFT JOIN users ON users.uuid = videos.uploader where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2] 

Refer : https://www.w3schools.com/sql/sql_join_left.asp

Note : If you have a common column in both tables then the value of the videos record will be overrided by users column value

Ex : videos [id,name,uploader,addedAt] users [uuid, name]

 in this case the result will be 


    videos.id,videos,uploader,videos.addeAt,users.uuid,users.name
* The videos.name column willbe overrided by users.name

To overcome this issue use the above query like this

(Example only)

select videos.*,users.*,videos.name as "videoname" from videos LEFT JOIN users ON users.uuid = videos.uploader where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2] 
  • Related