1

I am trying to use a simple query with laravel but its not returning any data. but when I run same query in phpmyadmin it returns 3 rows.

This is code in laravel.

 $pages = DB::table('static_pages')
     ->where('slug','=','(select slug from static_pages where id='.$id.')')
         ->get();

this is the query it makes.

  select * from `static_pages` where `slug` = (select slug from static_pages where id=2)

Can you tell me what could be the reason?

8
  • make as DB:row in where clause Commented Sep 21, 2017 at 11:49
  • Do you know about model Commented Sep 21, 2017 at 11:55
  • @AjayKumar I am new to laravel. I have worked in CI earlier. in CI it was very easy to use models. but here I was only able to make a class and there i put the table name. using just like this : CLASS::where()->get(); Commented Sep 21, 2017 at 11:58
  • you can make model using artisan command like php artisan make:model StaticPage and it will be in app\StaticPage linked to static_pages table the you can access it like StaticPage::where(...)->get() and it's very easy to use instead of using DB Commented Sep 21, 2017 at 12:02
  • but why you are trying to get a page by its slug that you are getting it by its id why not you get the page by its id directly??!! slug and id are unique, so you can get the page by id also?? Commented Sep 21, 2017 at 12:02

4 Answers 4

3

If what you refer to is the relationship between two tables then you might want to look at Laravel Model Relationship, however if you want to retrieve all records having the same slug of the record with id = 2 then you can also do it without using two DB:raw:

DB::table('static_pages')->where('slug', function ($query) {
        return $query->from('static_pages')->where( 'id', '2')->select('slug');
    })->get();

If you want to inspect the sql query it generates then use toSql() instead of ->get()

One evil thing to anticipate is if your inner query returns a collections of values then your comparison might become questionable. However since the field you use to make the query is unique, then this should not be a problem.

Hope this is useful.

Sign up to request clarification or add additional context in comments.

Comments

1

It worked by changing from this

 $pages = DB::table('static_pages')->where('slug','=','(select slug from static_pages where id='.$id.')')->get();

to this

$pages = DB::table('static_pages')->where('slug','=',DB::table('static_pages')->where('id','=',$id)->pluck('slug'))->get();

thanks to jishad

1 Comment

or as simple as: $pages = DB::table('static_pages')->where('slug','=',DB::raw('(select slug from static_pages where id='.$id.')'))->get(); so just ad raw to your code
0

try this one

DB::table('static_pages')->whereRaw("`slug` = (select slug from static_pages where id=2)")->get();

hope it will help you!

Comments

0

you can do this

 $pages = DB::table('static_pages')->where('slug','=',DB::raw('(select slug from static_pages where id='.$id.')'))->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.