본문 바로가기

Framework & Library/Laravel

라라벨 입문 중의 소소한 정리 1. compact(), 범위 확인 연산자 ::

 

- 화면에 데이터 뿌리는 방법 1

controller-> index()에서
return에 view(뷰화면, compact('데이터명', ' 데이터명', ... ));

그리고 해당 데이터 명을 해당 blade.php의 html 태그에 적어주면 된다!

 

-PHP compact();

: 개별 변수들을 가지고 하나의 배열을 생성하는 PHP 함수

문법

array compact( $var1, $var2 ...)
$firstName = 'John';
$lastName = 'Jeong';
$arr = compact('firstName', 'lastName');
print_r( $arr );



결과

Array
(
    [firstName] => John
    [lastName] => Jeong
)


- 콘트롤러에서 html 태그에 데이터 보내는 방법 중

$userId = Auth::id();

$변수 명 =  클래스명::클래스가 포함한 객체;

이렇게 해주면, 해당 변수 명에 클래스가 가진 요소를 꺼내 쓸 수 있다

email의 경우,

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

 

+++ 범위 확인 연산자 : :

: 범위 확인 연산자 (Paamayim Nekudotayim이라고도 함)
또는 더 간단한 용어로 이중 콜론
클래스의 static , constant 및 재정의 된 속성 또는 메서드에 액세스 할 수있는 토큰

클래스 정의 외부에서 이러한 항목을 참조 할 때는 클래스 이름을 사용

변수를 사용하여 클래스를 참조 할 수 있다

출처 :

https://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

 

-migrate 팁!

$table->id(); $table->bigIncrements('id') 의 별칭
$table->foreignId('user_id'); $table->unsignedBigInteger('user_id') 의 별칭
$table->bigIncrements('id'); 자동으로 증가하는(auto increment) UNSIGNED BIGINT (primary key) 컬럼
$table->bigInteger('votes'); BIGINT 컬럼
$table->unsignedBigInteger('user_id'); $table->bigInteger('user_id')->unsigned(); 의 별칭