Maatwebsite
を使用して、データベースにExcelファイルをアップロードします。匿名関数からデータを返します
すべての行にプロパティタイプがあります(例: 'penthouse'または 'villa')。
すべての行のすべてのプロパティタイプを収集したいと思います。
次の関数を使用すると、動作しません。
Excel::filter('chunk')->load($csv->getRealPath())->chunk(250, function ($results) {
DB::table('prices')->truncate();
foreach ($results as $row) {
/**
* @var CellCollection $row
*/
array_push($this->property_types, $row->property_type);
$price = Price::create($row->all());
}
});
よう__construct
機能で$this->property_types
を定義して:これは空の配列になります
public function __construct()
{
$this->middleware('auth');
$this->property_types = [];
}
。
hereのように、配列を定義してusing
にアンパサンドを指定すると問題が解決する可能性がありますが、これは空の配列と同じ結果を返します。
$data = [];
Excel::filter('chunk')->load($csv->getRealPath())->chunk(250, function ($results) use (&$data) {
DB::table('prices')->truncate();
foreach ($results as $row) {
/**
* @var CellCollection $row
*/
array_push($data, $row->property_type);
$price = Price::create($row->all());
}
});
匿名関数内でデータを定義し、関数外のデータを取得するにはどうすればよいですか?
どのバージョンのPHPを使用しますか? –
'匿名関数からデータを返し、'無名関数で 'return $ something;') – JustOnUnderMillions
'$ this->'はインスタンスを魔法にしますか? – JustOnUnderMillions