简体   繁体   English

删除一个数据后,如何以升序从数据库中检索新数据?

[英]how to retrieve a new data from database in ascending order after one data is deleted?

I have contact names which are arranged in ascending order with their name. 我的联系人姓名和姓名按升序排列。

But when I delete one contact name , I want to retrieve the next to the deleted one in ascending order. 但是,当我删除一个联系人姓名时 ,我想按升序检索已删除联系人的姓名

But I cannot make it right. 但是我做错了。 Right now I'm retrieving with id but its not effective. 目前,我正在使用id进行检索,但是它无效。

    function deleteContact($id)
    {
            $data = Contact::where('id', '>', $id)->first();

            $contact = Contact::find($id);
            $contact->delete();

            return $data;

        }

if you want to show order By id 如果您想按ID显示订单

function deleteContact($id)
{
  $contact = Contact::find($id);
  $contact->delete();

  $data = Contact::orderBy('id','asc')->get();
  return $data;
}

if you want to show order by created date from last to first 如果要按创建日期显示从最后到第一的顺序

function deleteContact($id)
{
  $contact = Contact::find($id);
  $contact->delete();

  $data = Contact::orderBy('created_at','DESC')->get();
  return $data;
}

Try to use Order By 尝试使用Order By

Eloquent 雄辩

$data = Contact::orderBy('name', 'ASC')->get();

query builder 查询生成器

$data = DB::table('contact')->orderBy('name','ASC')->get();

Try this. 尝试这个。

function deleteContact($id)
{


        $contact = Contact::find($id);
        $contact->delete();

        $data = Contact::orderBy('name','asc')->first();
        return $data;

    }

Just use orderBy while querying Contacts not by $data = Contact::where('id', '>', $id)->first(); 只需在不按$data = Contact::where('id', '>', $id)->first();查询联系人时使用orderBy即可 $data = Contact::where('id', '>', $id)->first(); ,

function deleteContact($id)
{
    $contact = Contact::find($id);
    $contact->delete();

    $data = Contact::orderBy('name','asc')->first();
    return $data;
}

The orderBy method allows you to sort the result of the query by a given column(here you have to give 'name' column.) orderBy方法可让您按给定的列对查询结果进行排序(此处必须提供“名称”列。)

For more details check this Ordering, Grouping, Limit, & Offset 有关更多详细信息,请检查此订购,分组,限制和抵销

Try this and comment if you have any query. 尝试此操作,如果有任何疑问,请发表评论。

try this. 尝试这个。 First delete then retrieve data in ascending order. 首先删除,然后按升序检索数据。

function deleteContact($id){  
    Contact::where('id', '=', $id)->delete();
    $data = Contact::select('*')->orderBy('id', 'asc')->get();
    return $data; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM