Through this query I find a element in a database by value and take the id:
Model::where($table,'=',$value)->first()->id;
But I need to create a method, because I use this code with other models; a possible example:
public function getId($model,$table,$value) {
...
}
How can I do?
Thanks!
Method
public function getId($model, $table, $value) {
return $model::where($table, $value)->first()->id;
}
Usage
$id = getId('App\User', 'name', 'John Doe');
From your code, it looks like you have this as a class method. Instead, you could create this as a global helper so you can access it anywhere.
I would create a Trait
<?php
namespace App\Models\Traits;
trait GetIdByNameTrait
{
public function scopeGetIdByName($query, $name)
{
return $query->where('name', $name)->first()->id;
}
}
The models should use it:
use Traits\GetIdByNameTrait;
Then you can use this everywhere;
$id = Model::getIdByName($value);
Related
I made a custom collection class which extends laravel collection class. This custom collection has some data specific handeling methods and a property to make my life easier.
use Illuminate\Support\Collection;
class CustomCollection extends Collection
{
public bool $myProperty = true;
public function filterBasedOnCustomStuff()
{
return $this->filter(function ($row)
{
// Some custom sorcerry
return ($var === "whatever");
});
}
}
And my question is. When I call filterBasedOnCustomStuff() method it returns a new CustomCollection object. Which is what I want of course. But I would also like to set the value of the $myProperty parametr of the new collection instance. Is it possible or I have to do that on the instance afterwards? Somethink like that:
$newCollection = $oldCollection->filterBasedOnCustomStuff();
$newCollection->myProperty = $oldCollection->myProperty;
I would like to avoid this aproach of setting it afterwards if possible.
Thank you in advance.
I would simply use a variable to hold the filtered instance:
<?php
use Illuminate\Support\Collection;
class CustomCollection extends Collection
{
public bool $myProperty = true;
public function filterBasedOnCustomStuff()
{
$filtered = $this->filter(function ($row)
{
// Some custom sorcerry
return ($var === "whatever");
});
$filtered->myProperty = $this->myProperty;
return $filtered;
}
}
I am new to Laravel. I have some functions in PaymentController. I want to call those functions from SmartpaySController. Here is the function which is available in PaymentController. Help me to call that function by staying in SmartpaySController.
public function getPaymentFailed($paymentId) {
$transactionData = $this->paymentRepo->find($paymentId);
if($transactionData) {
$data['quote'] = $this->quoteRepo->getQuoteById($transactionData->quote_id);
$data['metaTitle'] = 'Payment failed';
$data['returnMessage'] = $transactionData->return_message;
return view('payment::payment.quote_payment_failed', $data);
}
}
Thank you.
Instead of calling controller methods, the better practice is that you can create traits like: app/Traits and extend in controller
//trait
trait traitName {
public function getData() {
// .....
}
}
//Controller
class ControlelrName extends Controller {
use TraitName;
}
I recomend you to not call functions from one controller to another.
Make Helpers, Resources or implement same feature in other way
Never use controllers as object
But if you want to do it anyway you can use:
SomeController.php
class SomeController extend Controller {
public function someFunction(Request $request) {
// Here Some Code
}
}
YourController.php
use SomeController;
...
public function getPaymentFailed(Request $request, $paymentId) {
$controller_data = (new SomeController)->someFunction($request);
$transactionData = $this->paymentRepo->find($paymentId);
if($transactionData) {
$data['quote'] = $this->quoteRepo->getQuoteById($transactionData->quote_id);
$data['metaTitle'] = 'Payment failed';
$data['returnMessage'] = $transactionData->return_message;
return view('payment::payment.quote_payment_failed', $data);
}
}
Change:
public function getPaymentFailed($paymentId)
to:
public static function getPaymentFailed($paymentId)
This will make it staticly available in your SmartpaySController by doing:
PaymentController::getPaymentFailed($paymentId);
You can make use of Real-Time Facades
Using real-time facades, you may treat any class in your application
as if it were a facade.
To generate a real-time facade, prefix the namespace of the imported
class with Facades:
//...
use use Facades\App\Http\Controllers\SomeController;
//...
return SomeController::getPaymentFailed($request, $paymentId);
I'm currently rebuilding my vanilla-PHP-App with Laravel and I have the following problem.
I have multiple database-tables, that represent word categories (noun, verb, adverb, ...). For each table I created a separate Model, a route::resource and a separate resource-Controller. For example:
NomenController.php
public function show($id)
{
$vocab = Nomen::find($id);
return view('glossarium.vocab_update', compact('vocab'));
}
and
VerbController.php
public function show($id)
{
$vocab = Verb::find($id);
return view('glossarium.vocab_update', compact('vocab'));
}
...which are essentially the same except the Model class.
I don't want to create a separate Controller for each model, that does exactly the same. What would be the most simple and elegant way to solve this?
Should I just create a VocabController.php and add a parameter for the Model-name like:
Route::resource('/vocab/{category}', 'VocabController');
and then add a constructor method in this controller like
public function __construct ($category) {
if ($category == 'nomen') {
$this->vocab = App\Nomen;
}
else if ($category == 'verb') {
$this->vocab = App\Verb;
}
}
I wonder if there is a simpler method to do that. Can I somehow do this with Route Model Binding?
Thanks in advance
Simply create a trait like this in App\Traits, (you can name it anything... Don't go with mine though... I feel its pretty lame... :P)
namespace App\Traits;
trait CommonControllerFunctions {
public function show($id) {
$modelObject = $this->model;
$model = $modelObject::find($id);
return view('glossarium.vocab_update', compact('model'));
}
}
and in your NomenController and VerbController, do this:
use App\Traits\CommonControllerFunctions;
class NomenController {
use CommonControllerFunctions;
protected $model = Nomen::class;
}
and
use App\Traits\CommonControllerFunctions;
class VerbController {
use CommonControllerFunctions;
protected $model = Verb::class;
}
Note: Please note that this example is just a work-around for your particular situation only... Everyone practices code differently, so this method might not be approved by all...
I think the simpliest way it to create only one controller, eg VocabController with methods nomen, verb and whatever you want.
Routes:
Route::get('/vocab/nomen/{nomen}', 'VocabController#item');
Route::get('/vocab/verb/{verb}', 'VocabController#item');
And the model binding:
Route::model('nomen', 'App\Nomen');
Route::model('verb', 'App\Varb');
Then your method shoud look like that:
public function item($item)
{
return view('glossarium.vocab_update', $item);
}
Keep in mind, that $item is already fetched model from the database.
Looks simple (and maybe) but I need pass a received variable in a function to another function. Here's my code:
PD: I using Laravel Eloquent's Scopes
class myParentModel extends Model {
public function scopeMyScope($query, $VAR_I_WANT_TO_PASS=[]) {
return $query->with('model'])->whereHas('model', function($q, $VAR_I_WANT_TO_PASS=[]) {
$q->where('colum1',$VAR_I_WANT_TO_PASS[0])->where('colum2',$VAR_I_WANT_TO_PASS[1])->where('colum3',$VAR_I_WANT_TO_PASS[2]);
})->take(10);
}
}
and I want to do this:
$result = myParentModel::myScope([3,1,6])->get();
I resolve this using use:
class myParentModel extends Model {
public function scopeMyScope($query, $VAR_I_WANT_TO_PASS) {
return $query->with('model'])->whereHas('model', function($q) use ($VAR_I_WANT_TO_PASS) {
$q->where('colum1',$VAR_I_WANT_TO_PASS[0])->where('colum2',$VAR_I_WANT_TO_PASS[1])->where('colum3',$VAR_I_WANT_TO_PASS[2]);
})->take(10);
}
}
Is it possible to pass, somehow, a parameter to a relationship function?
I have currently the following:
public function achievements()
{
return $this->belongsToMany('Achievable', 'user_achievements')->withPivot('value', 'unlocked_at')->orderBy('pivot_unlocked_at', 'desc');
}
The problem is that, in some cases, it does not fetch the unlocked_at column and it returns an error.
I have tried to do something like:
public function achievements($orderBy = true)
{
$result = $this->belongsToMany (...)
if($orderBy) return $result->orderBy(...)
return $result;
}
And call it as:
$member->achievements(false)->(...)
But this does not work. Is there a way to pass parameters into that function or any way to check if the pivot_unlocked_at is being used?
Well what I've did was just adding new attribute to my model and then add the my condition to that attirbute,simply did this.
Class Foo extends Eloquent {
protected $strSlug;
public function Relations(){
return $this->belongsTo('Relation','relation_id')->whereSlug($this->strSlug);
}
}
Class FooController extends BaseController {
private $objFoo;
public function __construct(Foo $foo){
$this->objFoo = $foo
}
public function getPage($strSlug){
$this->objFoo->strSlug = $strSlug;
$arrData = Foo::with('Relations')->get();
//some other stuff,page render,etc....
}
}
You can simply create a scope and then when necessary add it to a builder instance.
Example:
User.php
public function achievements()
{
return $this->hasMany(Achievement::class);
}
Achievement.php
public function scopeOrdered(Builder $builder)
{
return $builder->orderBy(conditions);
}
then when using:
//returns unordered collection
$user->achievements()->get();
//returns ordered collection
$user->achievements()->ordered()->get();
You can read more about scopes at Eloquent documentation.
You can do more simple, and secure:
When you call the relation function with the parentesis Laravel will return just the query, you will need to add the get() or first() to retrieve the results
public function achievements($orderBy = true)
{
if($orderBy)
$this->belongsToMany(...)->orderBy(...)->get();
else
return $this->belongsToMany(...)->get();
}
And then you can call it like:
$member->achievements(false);
Works for the latest version of Laravel.
Had to solve this another was as on Laravel 5.3 none of the other solutions worked for me. Here goes:
Instantiate a model:
$foo = new Foo();
Set the new attribute
$foo->setAttribute('orderBy',true);
Then use the setModel method when querying the data
Foo::setModel($foo)->where(...)
This will all you to access the attribute from the relations method
public function achievements()
{
if($this->orderBy)
$this->belongsToMany(...)->orderBy(...)->get();
else
return $this->belongsToMany(...)->get();
}