Paginate Doubts Arable

Asked

Viewed 112 times

0

I turned that code 1 into code 2

1)//$palpites=DB::select("SELECT * FROM palpite  WHERE  id_u='$id'   order by id_c desc ");
   2) $palpites = DB::table('palpite')->where('id_u',$id)->orderby('id_c','desc') ->paginate(3);

How to turn this code below into the same 1.2 example?

$confrontos=DB::select("SELECT * FROM confrontos as c, palpite as p WHERE   p.id_u ='$id' AND EXISTS (SELECT * FROM palpite WHERE c.id = p.id_c)  order by id_c desc ");

1 answer

0


Good morning!

In the clause where it involves a sub-query you must use a Clousure (anonymous function) passing the desired information.

Example:

...

->where(function($q) use ($query) {
    $query->select(DB::raw('p.*'))
          ->from('palpite')
          ->whereRaw('c.id = p.id_c');
}

UPDATE:

After your explanation of what should be returned, my suggestion is first you modify your query, as follows:

select * from conf 
inner join pal on pal.id_c = c.id 
where pal.id_u = $id;

This way you get the same results you want. To make the pagination of this query in Laravel you must write something like:

$confrontos = DB::table('confrontos') 
->select('*') 
->join('palpites', 'palpites.id_confronto', '=', 'confronto.id') 
->where('palpites.id_usuario', $id) 
->paginate(10);
  • Can you complete it? I’m a little insecure about this code.

  • I can try yes. But I gave an example only of the part that involves the sub-query. The previous query you can base on the example you used in item (2). What is the main idea of the query? You want to return exactly what?

  • The two codes refer to the same thing(1,2)... code is correct, but the intention to transform it is to paginate! but this other code is more complex... and I’m not able to transform!

  • Got it. So, let’s go. The query that involves a sub-query is not necessary to do the paging. It doesn’t seem right. In Laravel you can simply page like this: $table = DB::table('table')->paginate(10); and in your view just render like this: $table->render();

  • Lorenzo, how would you look?

  • @Daneven you need only use the "paginate()" of Eloquent. I still don’t understand if it’s the query with the sub-query that you want to paginate or the one that you wrote in item (2). It’s confusing.

  • then! I have example 1) which I turned into 2) to be able to paginate, and it works! I want to do the same thing with the example below, to paginate! only that the code is more complex, uses two tables reference, but I would like to do the same thing.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.