'List'는 Laravel (또는 PHP 자체에 대한 특정 키워드)과 같아요. Laravel을 처음 사용합니다.Laravel에서 'List'모델을 만드는 방법은 무엇입니까?
데이터베이스에 lists
이라는 테이블이 있습니다. 이 테이블에는 title
이라는 필드가 있습니다. 또한, 표는 lists
에서 id
열을 참조하는 list_id
열이, tasks
라고, 그래서 제가 지금하고 있어요 것은 :
응용 프로그램/모델/MyAppList.php
class MyAppList extends Eloquent {
protected $table = 'lists';
public function tasks() {
return $this->hasMany('Task', 'list_id');
}
}
응용 프로그램/모델 /Task.php
class Task extends Eloquent {
public function myapplist() {
return $this->belongsTo('MyAppList');
}
}
응용 프로그램/routes.php
Route::get('/', function() {
$v = Task::find(1);
dd($v->myapplist()->lists('title'));
});
이렇게하면 해당 작업을 소유하고있는 목록의 제목이 올바르게 출력됩니다.
모델을 이름 짓는 것이 효과가 있다고 생각했습니다./routes.php
Route::get('/', function() {
$lists = MyApp\List::all()->lists('title');
dd($lists);
});
는하지만이 작동하지 않습니다
응용 프로그램 :이 파일은 클래스 파일 namespace MyApp;
다음 내부 List
것, List.php
될 것이다.
편집 1 :
응용 프로그램/routes.php
Route::get('/', function() {
$lists = List::all()->lists('title');
dd($lists);
});
:
내가 파일의 이름과 클래스로 '목록'이라는 단어를 떠나이라면 내가 시도 clariy하려면
내가 얻을 :
Symfony \ Component \ Debug \ Exception \ FatalErrorException
syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting '('
I 클래스를 네임 스페이스 경우 List
:
Route::get('/', function() {
$lists = Mdl\List::all()->lists('title');
dd($lists);
});
내가 얻을 :
Symfony \ Component \ Debug \ Exception \ FatalErrorException
syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING)
오류가 발생합니까? –
List는 실제로 PHP 예약 키워드이므로 "상수, 클래스 이름, 함수 또는 메소드 이름"으로 사용할 수 없습니다. 자세한 정보는 http://www.php.net/manual/en/reserved.keywords.php를 참조하십시오. 해결책? 다른 클래스 이름, 실제로 그것을 할 수있는 다른 방법이 없습니다. 어떤 사람들은 "ListModel"이 좋다고 말하고 싶지만, 대신이 목록의 내용에 대해 더 자세하게하려고 노력할 것입니다. SubscriptionList? UserList? 등 –
@ NathanLoding 추가 한 편집 1 – johnRivs