Laravel Eloquent“在哪里”

时间:2014-09-15 13:16:00

标签: laravel laravel-4 eloquent

我在laravel eloquent ORM编写查询时遇到问题。

我的查询是

SELECT book_name,dt_of_pub,pub_lang,no_page,book_price  
FROM book_mast        
WHERE book_price NOT IN (100,200);

现在我想将此查询转换为laravel eloquent。

13 个答案:

答案 0 :(得分:227)

查询构建器:

DB::table(..)->select(..)->whereNotIn('book_price', [100,200])->get();

锋:

SomeModel::select(..)->whereNotIn('book_price', [100,200])->get();

答案 1 :(得分:18)

您也可以通过以下方式使用 WhereNotIn

    <ToolBar x:Name="toolBar" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0">
        <Button BorderBrush="Black" Content="READ" Click="Button_Click_1"/>

        <ToolBar.Resources>
            <Style x:Key="ToolBarMainPanelBorderStyle" TargetType="{x:Type Border}">
                <Setter Property="CornerRadius" Value="0,0,0,0"/>
            </Style>
        </ToolBar.Resources>
    </ToolBar>

这将返回带有特定字段

记录的集合

答案 2 :(得分:1)

动态执行whereNotIn的方式:

 $users = User::where('status',0)->get();
    foreach ($users as $user) {
                $data[] = $user->id;
            }
    $available = User::orderBy('name', 'DEC')->whereNotIn('id', $data)->get();

答案 3 :(得分:1)

您可以通过以下方式使用WhereNotIn

$category=DB::table('category')
          ->whereNotIn('category_id',[14 ,15])
          ->get();`enter code here`

答案 4 :(得分:1)

您可以使用此示例动态调用不在哪里

$user = User::where('company_id', '=', 1)->select('id)->get()->toArray();

$otherCompany = User::whereNotIn('id', $user)->get();

答案 5 :(得分:0)

whereNotIn方法验证给定列的值是否包含在给定数组中:

$users = DB::table('users')
                    ->whereNotIn('id', [1, 2, 3])
                    ->get();

答案 6 :(得分:0)

您可以执行以下操作。

DB::table('book_mast') 
->selectRaw('book_name,dt_of_pub,pub_lang,no_page,book_price')  
->whereNotIn('book_price',[100,200]);

答案 7 :(得分:0)

在向结果中添加方法->toArray()之前,我一直无法进行子查询,我希望它对我有帮助,因为我有很多时间在寻找解决方案。

示例

DB::table('user')                 
  ->select('id','name')
  ->whereNotIn('id', DB::table('curses')->select('id_user')->where('id_user', '=', $id)->get()->toArray())
  ->get();

答案 8 :(得分:0)

这只是意味着您有一个值数组,并且想要记录,但这些值/记录除外。

您可以简单地将数组传递到whereNotIn()laravel函数中。

使用查询生成器

$users = DB::table('applications')
                    ->whereNotIn('id', [1,3,5]) 
                    ->get(); //will return without applications which contain this id's

雄辩。

$result = ModelClassName::select('your_column_name')->whereNotIn('your_column_name', ['satatus1', 'satatus2']); //return without application which contain this status.

答案 9 :(得分:0)

这是我为Laravel 7工作的版本

DB::table('user')                 
  ->select('id','name')
  ->whereNotIn('id', DB::table('curses')->where('id_user', $id)->pluck('id_user')->toArray())
  ->get();

答案 10 :(得分:0)

或在 Laravel 中尝试 pluck 这里

DB::table('user')                 
          ->select('id','name')
          ->whereNotIn('id', DB::table('curses')->where('id_user', '=', $id)->pluck('user_id'))
          ->get();  

答案 11 :(得分:0)

查询生成器:

    DB::table('book_mast')
->select('book_name','dt_of_pub','pub_lang','no_page','book_price')
    ->whereNotIn('book_price', [100,200])->get();

雄辩:

BookMast::select('book_name','dt_of_pub','pub_lang','no_page','book_price')
->whereNotIn('book_price', [100,200])->get();

答案 12 :(得分:-1)

$created_po = array();
     $challan = modelname::where('fieldname','!=', 0)->get();
     // dd($challan);
     foreach ($challan as $rec){
         $created_po[] = array_push($created_po,$rec->fieldname);
     }
     $data = modelname::whereNotIn('fieldname',$created_po)->orderBy('fieldname','desc')->with('modelfunction')->get();
相关问题