php newbie Posted October 17, 2014 Report Share Posted October 17, 2014 Sveiki. Saku apgūt laravel un neko nevaru saprast... $model = \DB::table($tableName); $result = $model->get(); Šitais atgriež masīvu ar objektiem, bet man vajag eloquent collection vai masīvu ar modeļiem lai varētu izsaukt model funkciju... Vai kā uzinicializēt to eloquent no tabulas nosaukuma? $model = new Eloquent('tablename'); Quote Link to comment Share on other sites More sharing options...
briedis Posted October 17, 2014 Report Share Posted October 17, 2014 Oh boy oh boy, beidzot kāds svaigs jautājums :D Lai dabūtu modeļus, tad arī selektē pēc modeļa $modelCollection = MyModel::where('stuff')->get(); Vispār jau tādi elementāri jautājumi ir atrunāti dokumentācijā: http://laravel.com/docs/4.2/eloquent#basic-usage Quote Link to comment Share on other sites More sharing options...
php newbie Posted October 18, 2014 Author Report Share Posted October 18, 2014 Nē nu dokumentāciju es skatijos un guglejos arī...Man vajag dabūt datus no kādas random tabulas un lai rezultāts būtu tas eloquent collection. Teiksim funkcija kurai es padodu tabulas nosaukumu. DB::table($tableName); neder jo rezultāts ir objektu masīvs on modeļa funkcijas nav pieejamas. Tad mēģināju uzinicializēt to Eloquent: new Eloquent(); Eloquent::construct(); Arī nesanāca. Un vispār tas eloquent objekts nemeklē datus, kaut tur ir tas visas metodes... $model = new Users(); $model->take(1); $model->get(); Šitais atgriež visus ierakstus. Tad sapratu ka tā eloquent nemaz nav pareiza klase un ka visi tie where() un tt atgriež QueryBuilder. Nu jau apnika ar šito figņu ņemties un tagad es padodu funkcijai to queryBuild objektu.. $result = My::getTableData(Users::query(), Input::get('page')); Bet tā arī nepieleca kā uzinicializēt query build objektu no tabulas nosaukuma. PHPStorm neiet dziļumā un pašam slinkums meklēt to sorce un kā tur tā instance tiek uztaisīta public static function query() { return with(new static)->newQuery(); } Quote Link to comment Share on other sites More sharing options...
briedis Posted October 18, 2014 Report Share Posted October 18, 2014 Kaut ko tu tur smagi jauc. Uztaisi modeļa klasi (id kolonna ir primārā atslēga): class MyModel extends Eloquent { protected $table = 'my_table'; } Un tad selektē, piemēram, 10 objektus: $objects = MyModel::where('nosacījums', '=', 'kautkas')->take(10)->get(); foreach($objcets as $v){ echo $v->id; } DB klase ir query builderis. Eloquent klase ir modeļiem (kas arī pamatā satur query buileri). Ja gribi modeļus, tad tev arī jāsauc uz tā modeļa selektēšana, nevis DB klasei. Quote Link to comment Share on other sites More sharing options...
php newbie Posted October 18, 2014 Author Report Share Posted October 18, 2014 Bet vai var uztaisīt Builder vai Eloquent klases instanci zinot tabulas identifikatoru? Man vajag funkciju kurai padot tabulas identifikatoru un tā atgriež datus. Pagaidam ir šāds variants: public function getTableData($queryBuilder) { ... return $queryBuilder->get(); } getTableData(Users::query()); Quote Link to comment Share on other sites More sharing options...
php newbie Posted October 18, 2014 Author Report Share Posted October 18, 2014 Un vēl jautājums par route. Teiksim ir route: Route::get('user/{name?}', function($name = 'John') { return $name; }); Vai var dabūt tā route parametrus ka masīvu? [ 'name' => 'john' ] Quote Link to comment Share on other sites More sharing options...
briedis Posted October 18, 2014 Report Share Posted October 18, 2014 Izmantojot šo var dabūt aktuālā route'a visus parametrus: Route::current()->parameters() Quote Link to comment Share on other sites More sharing options...
Kemito Posted October 18, 2014 Report Share Posted October 18, 2014 http://laravel.io/bin/nXDa9 Routēs šādi var darīt tīri, lai pārbaudītu kaut ko. Pārējo jau pa lielam vajadzētu pārnest uz kādu kontrolieri. Palasi par RESTful like kontrolieriem. Viss augstāk minētais, ko teici, aizmirsti to, izklausās, ka smagi centies sarežģīt lietas. Quote Link to comment Share on other sites More sharing options...
php newbie Posted October 18, 2014 Author Report Share Posted October 18, 2014 Izmantojot šo var dabūt aktuālā route'a visus parametrus: Route::current()->parameters() Paldies. Bet kā var default vertības uzsetot tā lai tas būtu iekšā Route::current()->parameters() Quote Link to comment Share on other sites More sharing options...
briedis Posted October 18, 2014 Report Share Posted October 18, 2014 Nu uzseto tur kur vajag. Parasti to dara kontroliera metodē. Route::get('{parametrs1}/{parametrs2}... function manaMetode($parametr = 'default', $parametrs2 = 'cita vertijba'){ } Quote Link to comment Share on other sites More sharing options...
php newbie Posted October 18, 2014 Author Report Share Posted October 18, 2014 (edited) Nu uzseto tur kur vajag. Parasti to dara kontroliera metodē. Route::get('{parametrs1}/{parametrs2}... function manaMetode($parametr = 'default', $parametrs2 = 'cita vertijba'){ } bet Route::current()->parameters() būs tukšs. Vajag lai Route::current()->parameters() būtu defaultas vērtības a nu var setParameter(), bet nu nesmuki Edited October 18, 2014 by php newbie Quote Link to comment Share on other sites More sharing options...
briedis Posted October 18, 2014 Report Share Posted October 18, 2014 nu pieliec tak tās defaultās vērtības, ja baigi vajag. $params = Route::current()->parameters() + [ 'param1' => 'vertiba', 'param2' => 'vertiba', ]; echo $params['param1']; echo $params['param2']; Quote Link to comment Share on other sites More sharing options...
php newbie Posted October 19, 2014 Author Report Share Posted October 19, 2014 (edited) Ideja bija paginatorā dabūt parametrus un action no request. Tad paginatoram vajadzētu padod tikai lapu skaitu un viss... $action = Route::currentRouteAction(); $params = Route::current()->parameters(); un tad ģenerēt linkus lapām tikai samainot 'page' parametru. $params['page'] = $currentPage + 1; action($action, $arams); Bet nu labi, padošu tos parametrus no kontroliera: public function getIndex($page = 1, $sortField = 'name', $sortOrder = 'desc') { $params = compact('page', 'sortField', 'sortOrder'); ... $pagination = View::make('layout.pagination'); $pagination->with('params', $params); $pagination->with('pages', $result['pages']); $pagination->with('page', $result['page']); ... Edited October 19, 2014 by php newbie Quote Link to comment Share on other sites More sharing options...
briedis Posted October 19, 2014 Report Share Posted October 19, 2014 Tu tak zini, ka ir pieejams arī defaultais paginators? Quote Link to comment Share on other sites More sharing options...
php newbie Posted October 19, 2014 Author Report Share Posted October 19, 2014 Zinu, bet kaut kas man vai nu nesanāca, vai nu nepatika. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.