Product : Model명

serial_name : 컬럼명

 

테이블 데이터 모두 가져오기

1
App\Project:all();
cs
첫번째 데이터 가져오기
1
App\Project::first();
cs
    public function read($id)
    {
        $post = Post::where('id',$id)->first();
        return response()->json($post);
    }

*first()로 가져오면 오브젝트로 가져오고 get()로 가져오면 배열이된다. 

 
날짜가 최신것 부터 가져오기
1
App\Project::latest()->get();
cs
 
id가 5인 데이터 가져오기
1
App\Project::find($id);
cs

 

1
2
3
4
$count = Project::count();
$sum = Task::sum('id'); // id 컬럼 값을 합산
$max = Task::max('id'); // 가장 큰 id 컬럼 값을 리턴
$min = Task::min('id'); // 가장 작은 id 컬럼 값을 리턴
cs
 
 
1
$max = Task::where('created_at', '>', \Carbon\Carbon::now()->subDays(7))->max('id');
cs
where 조건절 사용하기
1
2
3
4
5
6
7
8
9
10
11
12
public function getWhere()
{
    $tasks = Task::where('id', '>', 10)
                ->where('id', '<', 20)
                ->where('name', 'like', 'Ta%')
                ->orderBy('name', 'desc')
                ->skip(5)
                ->take(3)
                ->get();
  
    return  response()->json($tasks);
}
cs
[참고]

 

컬럼과 일치하는 값 쿼리수행

1
App\Product::where('serial_name', '19A0001')-get();
cs

1
App\Product::where('serial_name', '19A0001')-first();
cs

 

컬럼과 일치하는 값 update수행

1
2
3
App\Product::where('serial_name', '19A0001')->update([
'board_name' => 'ttt';
]);
cs

 

배열을 반복문으로 입력하기

1
2
3
4
5
6
7
8
9
10
11
        //시리얼번호를 배열로 가지고 온다.
        $skills = request('skills');
 
        //시리얼번호 수량
        $skills_count = count($skills);
 
        for($i=0; $i<$skills_count; $i++){
        Product::where('serial_name',$skills[$i])->update([
            'board_name' => request('t1'),
        ]);
        }
cs

 


수량조회 카운터

1
App\Product::where('quantity', 1)->count();
cs

 

합계sum 조회

1
App\Product::sum('aoi_top_part_num');
cs

 

평균avg 조회

1
App\Product::avg('aoi_top_part_num');
cs

 

기간별 조회

1
\App\Product::where('quantity', 1)->where('product_date' , '>', '2019-01-30')->where('product_date','<','2019-12-31')->count()
cs

 

검색조회 : pbas테이블 → board_name컬럼→$board_name 변수에 들어온 값을 검색하고 페이지50개씩 보여준다.

1
App\Pba::where('board_name', 'like' , '%'.$board_name.'%')->paginate(50);
cs

 

그룹으로 가져오기 pbas테이블 → project_name컬럼의 데이터를 그룹(중복)처리해서 가져온다.

1
App\Pba::get()->groupBy('project_name');
cs

 

글작성(생성)순으로 내림차순 정렬하기

    public function index()
    {
        $posts = Post::orderBy('created_at', 'desc')->get();
        return response()->json( $posts );
    }

 

+ Recent posts