Laravel雄辩查询正确的数据 [英] Laravel eloquent query right data

查看:124
本文介绍了Laravel雄辩查询正确的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当新的laravel和雄辩。



我有2个表线程消息你可以看到以下链接中的结构:



线程





消息





现在我的目标是仅查询与登录用户在消息表中具有相同user_id的表线程的线程。



有没有办法通过雄辩,或者我必须为此写一个查询?



我目前得到所有的线程像这样:

  $ thread = Thread :: findOrFail($ id); 

但是这会给出安全问题,因为你可以去任何线程,如果你改变我的路由中的id。



编辑



我目前的节目功能:



< p $ p> public function show($ id)
{
$ currentUserId = Auth :: user() - > id;
$ threads = Thread :: forUser($ currentUserId) - > latest('updated_at') - > get();

try {
$ thread = Thread :: findOrFail($ id);
} catch(ModelNotFoundException $ e){
Session :: flash('error_message','Oops,not found。');
return redirect('messages');
}


if(array_has($ threads,$ thread)){
$ users = User :: whereNotIn('id',$ thread-> participantsUserIds($ currentUserId)) - >获得();
$ thread-> markAsRead($ currentUserId);
return view('messenger.show',compact('thread','users'));
} else {
Session :: flash('error_message','Oops,not found。');
return redirect('messages');
}

}

我需要一种方法来检查$ thread is inside $ threads。

解决方案

如果你想使用雄辩,你必须先定义一个关系。
一条消息属于线程和用户。以下是如何定义关系:
消息模型内部:

  public function user()
{
return $ this-> belongsTo('App / User'); //用户模型
}

public function thread()
{
return $ this-> belongsTo('App / Thread'); //线程模型
}

要定义逆向,您可以执行以下操作:
内部用户模型:

  public function threads()
{
return $ this-> hasMany ( '应用/螺纹');
}

线程模型内:


$ b $公共功能消息()
{
return $ this-> hasMany('App / Message'); b

  
}

现在,您可以在控制器中执行以下操作:

  $ threads = Auth :: user() - > threads; 

现在您拥有当前登录用户的所有线程。
我不知道我是否得到这个问题,所以请问。



编辑:
你可以这样检查:

  $ thread = Thread :: find($ id); 
$ isCurrentUserThread = false;
foreach(Auth :: user() - >线程为$ currentUserThread){
if($ currentUserThread-> id == $ thread-> id){
$ isCurrentUserThread =真正;
// $线程属于当前用户
}
}

if($ isCurrentUserThread){
//线程属于当前用户
} else {
//它不属于当前用户
}


I am fairly new to laravel and eloquent.

I have 2 tables threads and messages you can see the structure in the links below:

threads

messages

Now my goals is to query only the threads from table threads that have the same user_id in the messages table as the logged in user .

Is there a way to do this via eloquent or do I have to write a query for this?

I currently get all the threads like this:

$thread = Thread::findOrFail($id);

But this gives security issues since you can go to any thread if you change the id in my route.

EDIT

My current show function:

public function show($id)
    {
        $currentUserId = Auth::user()->id;
        $threads = Thread::forUser($currentUserId)->latest('updated_at')->get();

        try {
            $thread = Thread::findOrFail($id);
        } catch (ModelNotFoundException $e) {
            Session::flash('error_message', 'Oops, not found.');
            return redirect('messages');
        }


        if(array_has($threads, $thread)){
            $users = User::whereNotIn('id', $thread->participantsUserIds($currentUserId))->get();
            $thread->markAsRead($currentUserId);
            return view('messenger.show', compact('thread', 'users'));
        }else{
            Session::flash('error_message', 'Oops, not found.');
            return redirect('messages');
        }

    }

I need a way to check if $thread is inside $threads.

解决方案

If you want to use eloquent you must first define a relationship. One message belongs to a thread and a user. Here is how to define the relationships: Inside the Message model:

public function user()
{
   return $this->belongsTo('App/User'); //User model
}

public function thread()
{
  return $this->belongsTo('App/Thread'); //Thread model
}

To define the inverse you do the following: Inside User model:

public function threads()
{
  return $this->hasMany('App/Thread');
}

Inside the Thread model:

public function messages()
{
   return $this->hasMany('App/Message');
}

Now you can do the following in your controller:

$threads = Auth::user()->threads;

Now you have all threads by the currently logged in user. I am not sure if I got the question right so ask away.

Edit: You could check like so:

$thread = Thread::find($id);
$isCurrentUserThread = false;
foreach(Auth::user()->threads as $currentUserThread) {
   if($currentUserThread->id == $thread->id) {
       $isCurrentUserThread = true;
      //$thread belongs to the current user
   }
}

if($isCurrentUserThread) {
    //the thread belongs to the current user
} else {
   //it doesn't belong to the current user
}

这篇关于Laravel雄辩查询正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆