Im new in yii. I have question about Active record and bussnes logic in models.
I have model and controller:
namespace app\models;
use yii\db\ActiveRecord;
class Photos extends ActiveRecord
{
}
Controller:
namespace app\controllers;
use Yii;
use app\models\Photos;
class PhotosController extends Controller
{
public function actionIndex()
{
$photos = Photos::find()
->where(['userid' => Yii::$app->user->identity->id])
->all();
return $this->render('index', ['photos' => $photos]);
}
}
I want do that in other way:
namespace app\models;
use yii\db\ActiveRecord;
class Photos extends ActiveRecord
{
public function findOneById($id)
{
return Photos::findOne($id);
}
}
And Controller:
namespace app\controllers;
use Yii;
use app\models\Photos;
class PhotosController extends Controller
{
public function actionIndex()
{
$photos = Photos::findByUserId(Yii::$app->user->identity->id);
return $this->render('index', ['photos' => $photos]);
}
}
What is correct way to do this?
Im about fat model, and tiny controller.
The second option is more true, controllers should be small and all business logic should be rendered in models or functional classes
Related
Greetings guys i'm having a challenge figuring out how to make this method in the base controller so that i initialize it there and call it in all other controllers that i wish.
I want to create this in the base controller , then call it in other controllers
$paynow = new Paynow(
'9644',
'7e3bebb4-6dbf-4f8f-9e10-aceafd02c8db',
'Return_url',
'Result_url'
);
Images
1.This is what i have done in the base controller
Image 2. This is where im trying to use it to call its member functions
Image 3. Is the error that im getting
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function callPayNow()
{
return new Paynow(
'9644',
'7e3bebb4-6dbf-4f8f-9e10-aceafd02c8db',
'Return_url',
'Result_url'
);
}
}
In your AnyController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AnyController extends Controller
{
public function anyMethod()
{
$this->callPayNow();
}
}
if I undersend you well,
Maybe the best approach is to create new controller that extends BasController:
class Controller extends BaseController
{
public function payNow()
{
return new Paynow(
'9644',
'7e3bebb4-6dbf-4f8f-9e10-aceafd02c8db',
'Return_url',
'Result_url'
);
}
}
And then in your other controller you can extend your new controller:
class UserController extends Controller
{
//For example
public function show($id, Request $request)
{
$payNow = $this->payNow();
$payment = $payNow->createPayment($currentOrder, $request->get('email'));
return response()->json("done");
}
}
Paynow will be called in every controller that extends this controller.
have been trying to get the output of these code from my vew but its giving me issues, Please i woild really be bappy to get help.
In my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
$data = App\allLeave::find(1)->full_name;
return view('leave/allLeave',["data"=>$data]);
}
}
in my employee model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class Personnel
* #package App
*/
class Employee extends Audit
{
public function leave()
{
return $this->belongsTo('App\allLeave');
}
}
In allLeaveModel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class allLeave extends Model
{
public function empolyee()
{
return $this->hasMany('App\Employee');
}
}
in the blade
{{$data->employee->full_name}}
You already assign full name to data in controller. You only need {{ $data }} in blade
If i have to do the same i will do it simply like
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
/* No need to use App\allLeave because you already have that used in
top of the project */
$data=allLeave::findorFail(1);
return view('leave.allLeave')->with('data', $data);
}
}
in front end just use
{{$data->first_name}} //same column as in database table
Note: Make sure are using laravel eloquent model relationships
In your controller should be like this:
$data = App\allLeave::find(1)->empolyee();
And your blade:
{{$data->full_name}}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class testing extends Model
{
}
should i write my all queries in this model class,even if i just want to use query builder no eloquent?
what are the good practices to write raw query or using query builder?
You can write your queries or say eloquent in your model differenciating from other logical code
Say for example you have AlbumController
namespace App\Http\Controllers;
use App\Album;
use App\Http\Controllers\Controller;
class AlbumController extends Controller
{
public function index()
{
$albums = Album::get_albums();
// other logical code
}
}
And in Album.php which is model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
public static function get_albums() {
$albums = Album::get();
return $albums;
}
}
And in your route file
Route::post('album', 'AlbumController#index');
Hope you get idea!
No that is not only the method to communicate with your database it is one possible solution only. You can use DB for query like
<?php
Namespace App\Http\Controllers;
use DB;
Class AbcController extends Controller{
Public function functionName(){
$data=DB::table(‘tableName’)->get();
return view(‘desiredPage’)->with(‘data’, $data);
}
}
go to the link for more laravel database query information
I'm following the Laracasts series and have run into an issue on the episode Laravel 5.4 From Scratch: Route Model Binding.
Laravel version:
Laravel Framework 5.6.13
The error:
Class App\Http\Controllers\Panel does not exist
This shows on both the /panel and /panel/1 pages
App\Http\Controllers\PanelController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
routes/web.php
// Main panel view
Route::get('/panel', 'PanelController#index');
// Individual panel view
Route::get('/panel/{panel}', 'PanelController#show');
App/Panel.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Panel extends Model
{
public static function activePanels()
{
return static::where('status', 1)->get();
}
}
Add this line in panel controller before the class
use App\Panel;
You need to add use App\Panel; to top of class
Or call it by full namespace $panels = App\Panel::all();
You don't included your model to class.
Add App\Panel to main include section:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panel;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
or load model in your class method manually:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PanelController extends Controller
{
public function index()
{
$panels = App\Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
I have a problem with passing variable (data) to all Views. I created BaseController that extends default Laravel controller and "global" variables are defined there. When I extend other controller with BaseController i got error that variable is not defined. Does someone knows where's the problem?
Here is code:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class BaseController extends Controller {
public function __construct() {
$obavijesti="Other data";
$izbornici="Some data";
View::share ( 'izbornici', $izbornici );
View::share ( 'obavjesti', $obavjesti );
}
}
class AdminController extends BaseController {
.
.
.
echo '<pre>';var_dump($izbornici);echo '</pre>';//Error pop ups here
.
.
.
}
You are doing something wrong here. view::share() is used for sharing a piece of data across all views not controller.
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
//If you wish to get these variables in your other controllers you do this:
class BaseController extends Controller {
public $obavijesti="Other data";
public $izbornici="Some data";
public function __construct() {
View::share ( 'izbornici', $this->izbornici );
View::share ( 'obavjesti', $this->obavjesti );
}
}
class AdminController extends BaseController {
//if you have a constructor in other controllers you need call constructor of parent controller (i.e. BaseController) like so:
public function __construct(){
parent::__construct();
}
public function Index(){
echo $this->obavijesti;
}
}
You can also use a composer to share variables to views
//1. Create a composer file at app\Composers\AdminComposer.php
//NB: create "app\Composers" if does not exists
//2. Inside AdminComposer.php add this.
<?php namespace App\Composers;
class AdminComposer
{
public function __construct()
{
}
public function compose($view)
{
//Add your variables
$view->with('izbornici', 'Other data')
->with('obavjesti', 'Some other data');
}
}
//3. In you controller do this:
<?php namespace App\Http\Controllers;
//NB: I removed your BaseController because I believe the issue is coming from //there
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class AdminController extends Controller{
public function __construct(){
//Lets use AdminComposer to share variables to adminpage.blade.php view
View::composers([
'App\Composers\AdminComposer' => array('adminpage')
]);
}
public function Index(){
return view('adminpage');
}
}
Ideally you're going to want a combination of what you have, and the other answer posted here.
<?php
class BaseController extends Controller {
protected $obavijesti = 'Other data';
protected $izbornici = 'Some data';
public function __construct() {
View::share('obavjesti', $this->obavjesti);
View::share('izbornici', $this->izbornici);
}
}
Then in all of your views, you have access to the variables $obavjesti and $izbornici. Now in your other controllers, anything that extends BaseController can do the following:
class AdminController extends BaseController {
public function index() {
echo $this->ixbornici;
echo $this->obavjesti;
}
}