I am using GitHub - jenssegers/laravel-mongodb: A MongoDB based Eloquent model and Query builder for Laravel;
In my Laravel project I created DB model that sets Model table name dynamically (in Mongodb case collection) .
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class DbData extends Model
{
protected $collection = 'default_collection';
function __construct($collection)
{
$this->collection = $collection;
}
}
This works when I am creating new DbData object, for data insert:
$data = new DbData('dynamic_collection_name');
$data->variable = 'Test';
$data->save();
But this solution is not enough of I want to use this DbData model for querying data from my database.
What I want to achieve is to add possibility to pass variable for DbModel, for instance something like this:
$data = DbData::setCollection('dynamic_collection_name');
$data->get();
You could perhaps do something like this on your class.
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class DbData extends Model
{
protected $collection = 'default_collection';
public function __construct($collection)
{
$this->collection = $collection;
}
public static function setCollection($collection)
{
return new self($collection);
}
}
This will allow you to call DbData::setCollection('collection_name') and the collection name will only be set for that specific instance.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class DbData extends Eloquent
{
use HasFactory;
// if you need to set default collection also then uncomment below line.
// protected $collection = 'defaultCollectionIfWantsToSet';
/**
* set collection name
*
* #param string $collection
* #return $this
*/
public static function setCollection($collection)
{
$instance = new self();
$instance->collection = $collection;
return $instance;
}
// OR you can use function as like below also
// public static function setCollection($collection)
// {
// $instance = new self();
// return $instance->setTable($collection);
// }
}
this will allow you to call DbData::setCollection('collection_name') and the collection name will only be set for that specific instance
I tested with Laravel 8 & 9
Related
I'm developing an application where my data comes from external server in JSON format.
I would like to set a relationships between each models, but without using a database table.
Is it possible ?
Something like that:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'https://.../server/flights.json';
}
You could make a service class which handles the request and returns class instances:
namespace App\Services;
class FlightService
{
/**
* #var FlightFactory
*/
private $flightFactory;
public function __construct(FlightFactory $flightFactory)
{
$this->flightFactory = $flightFactory;
}
public function getAllFlights()
{
$flightsJson = $this->getFromExternalCurl();
return $this->flightFactory->buildFlightList($flightsJson);
}
private function getFromExternalCurl()
{
return Curl::to('http://www.foo.com/flights.json')
->withData( array( 'foz' => 'baz' ) )
->asJson()
->get();
}
}
Basically the service would make the external API call and the response is passed to a factory which creates the instances.
Note that you just need to add the factory in the construct and it's binded because laravel uses https://laravel.com/docs/5.4/container
namespace App\Factories;
class FlightFactory
{
public function buildFlightList($flightJsonList)
{
$flightCollection = collect();
foreach($flightJsonList as $flightJson) {
$flightCollection->push($this->buildFlight($flightJson));
}
return $flightCollection;
}
public function buildFlight($flightJson)
{
$flight = new Flight();
// add properties
return $flight;
}
}
The factory will return a Collection which is verry usefull because it contains usefull methods, or you can return an array.
In this example I used a curl library https://github.com/ixudra/curl but it can be replaced with native php or other libraries.
Then you can use by injecting the FlightService in your controllers.
P.S: Code not tested but represents a possible approach
I have a model named 'Poll'. Inside Poll model I defined a boot method like follows:
public static function boot()
{
parent::boot();
self::created(function($model){
// dd($model);
$speakers = $model->speakers()->get();
// dd($speakers);
// What I want to do here is: create poll options relation from speakers as follows
// $poll->poll_options()->create([
// 'option' => $speaker->name,
// ]);
}
}
I am adding the speakers relation and it is working perfect.
But inside this boot method, inside self::created if I tried to get the speakers relation, it is always empty (dd($speakers) line). Is it because of the boot method runs just after the model is saved into DB and the relations not at all saved?
I am getting newly created model in the line: dd($model) mentioned in the code.
UPDATE
I tried with events also.
My Poll Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Cookie;
use App\Events\PollCreated;
class Poll extends Model
{
........
protected $events = [
'created' => PollCreated::class,
];
.......
public function speakers()
{
return $this->belongsToMany('App\Models\Speaker','poll_speaker','poll_id','speaker_id');
}
}
app/Events/PollCreated.php:
namespace App\Events;
use App\Models\Poll;
use Illuminate\Queue\SerializesModels;
class PollCreated
{
use SerializesModels;
public $poll;
/**
* Create a new event instance.
*
* #param Poll $poll
* #return void
*/
public function __construct(Poll $poll)
{
// $this->poll = $poll;
$event = $poll->event()->first();
// dd($event);
// dd($poll->speakers()->get());
// dd($poll->load('speakers'));
}
}
Here also I am not getting speakers, in the line: dd($poll->speakers()->get());
my Speaker model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
class Speaker extends Model
{
use CrudTrait;
……..
public function polls()
{
return $this->belongsToMany('App\Models\Poll');
}
……..
}
The problem is with timing as models must always be created before they can be set in a many-to-many relationship. So there is no possible way that in a many-to-many relationship during the created event the relationship is already set as the created events are always raised before the relationships.
Anyone looking for a solution can probably experiment with the chelout/laravel-relationship-events package as this adds relationship events to models.
To be sure, I tested this out with a simple application of users and computers.
User.php
class User extends Model
{
use HasBelongsToManyEvents;
public static function boot() {
parent::boot();
self::created(function($model){
Log::info('user::created');
});
static::belongsToManyAttaching(function ($relation, $parent, $ids) {
$ids = implode(' & ', $ids);
Log::info("Attaching {$relation} {$ids} to user.");
});
static::belongsToManyAttached(function ($relation, $parent, $ids) {
$ids = implode(' & ', $ids);
Log::info("Computers {$ids} have been attached to user.");
});
}
public function computers() {
return $this->belongsToMany(Computer::class, 'user_computers');
}
}
Computer class is the same in reverse. And for the following code:
$user = User::create();
$user->computers()->attach([
Computer::create()->id,
Computer::create()->id
]);
This was the outcome:
user::created
computer::created
computer::created
Attaching computers 69 & 70 to user.
Computers 69 & 70 have been attached to user.
i'm tryint to store a new Model, with many related new models.
I have my Service model
class Service extends Model{
public function serviceoperations() {
return $this->hasMany("App\Models\ServiceOperation");
}
}
and my ServiceOperation model
class ServiceOperation extends Model{
public function service() {
return $this->belongsTo("App\Models\Service");
}
}
in my store function i'm creating a new service model, with many serviceoperation models, using a transaction:
use App\Models\Service;
use App\Models\ServiceOperation;
use Illuminate\Support\Facades\DB;
public function store(Request $request) {
$service = new Service();
$ops = [];
foreach ($operations as $operation) {
$op = new ServiceOperation();
$ops[] = $op;
}
DB::transaction(function()use($service, $ops) {
$service->save();
$service->serviceoperations()->saveMany($ops);
});
}
My Question is: Is there another way to add my new related serviceoperation models to my new service model, and persist them in the database in a single command?
You must use associate method.
$service->serviceoperations()->associate($serviceoperations)
Hope it help you :)
I'm trying to sort entire dataset of main model through column of relational model. I am using Laravel ORM 5.2.43 and Jensenggers MongoDb 3.1
Here are the models I have
UserEventActivity.php - Mongo Model
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class UserEventActivity extends Eloquent
{
protected $collection = 'user_event_activity';
protected $connection = 'mongodb';
public function handset() {
return $this->hasOne('HandsetDetails', '_id', 'handset_id');
}
public function storeDetail() {
return $this->hasOne('StoreDetails', 'st_id', 'store_id');
}
}
HandsetDetails.php - Mongo Model
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class HandsetDetails extends Eloquent
{
var $collection = 'user_handset_details';
var $connection = 'mongodb';
}
StoreDetails.php - MySql Model
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Illuminate\Database\Eloquent\Model as Eloquent;
class StoreDetails extends Eloquent
{
use HybridRelations;
protected $connection = 'mysql';
protected $table = 'icn_store';
}
Php script
$activity = UserEventActivity::join('handset ', 'handset._id', '=', 'handset_id')
->join('storeDetail', 'store_id', '=', 'storeDetail.st_id')
->orderBy('handset.handset_make', 'desc')
->select('storeDetail.*', 'handset.*')
->get()
->toArray();
This data from UserEventActivity is not stored based on handset_make field in handset relation.
Please help me to achieve the expected result
As far as I know MongoDB does not support joins like this.
A way around it could be to use eager loading.
So your UserEventActivity model might look like this:
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class UserEventActivity extends Eloquent
{
protected $collection = 'user_event_activity';
protected $connection = 'mongodb';
public function handset() {
return $this->hasOne('HandsetDetails', '_id', 'handset_id');
}
public function storeDetail() {
return $this->hasOne('StoreDetails', 'st_id', 'store_id');
}
public function getHandsetMakeAttribute()
{
return $this->handset->handset_make;
}
}
Note the getHandsetMakeAttribute() accessor.
Then you may be able to make your call with this:
$activity = UserEventActivity::with('storeDetail')
->with('handset')
->get()
->sortByDesc('handset_make')
->toArray();
Not at all tested but worth a go.
I want to replace the Laravels builder class with my own that's extending from it. I thought it would be as simple as matter of App::bind but it seems that does not work. Where should I place the binding and what is the proper way to do that in Laravel?
This is what I have tried:
my Builder:
use Illuminate\Database\Eloquent\Builder as BaseBuilder;
class Builder extends BaseBuilder
{
/**
* Find a model by its primary key.
*
* #param mixed $id
* #param array $columns
* #return \Illuminate\Database\Eloquent\Model|static|null
*/
public function find($id, $columns = array('*'))
{
Event::fire('before.find', array($this));
$result = parent::find($id, $columns);
Event::fire('after.find', array($this));
return $result;
}
}
And next I tried to register the binding in bootstrap/start.php file like this :
$app->bind('Illuminate\\Database\\Eloquent\\Builder', 'MyNameSpace\\Database\\Eloquent\\Builder');
return $app;
Illuminate\Database\Eloquent\Builder class is an internal class and as such it is not dependency injected into the Illuminate\Database\Eloquent\Model class, but kind of hard coded there.
To do what you want to do, I would extend the Illuminate\Database\Eloquent\Model to MyNamespace\Database\Eloquent\Model class and override newEloquentBuilder function.
public function newEloquentBuilder($query)
{
return new MyNamespace\Database\Eloquent\Builder($query);
}
Then alias MyNamespace\Database\Eloquent\Model to Eloquent at the aliases in app/config/app.php
Both of the answers are correct in some way. You have to decide what your goal is.
Change Eloquent Builder
For example, if you want to add a new method only for eloquent models (eg. something like scopes, but maybe a little more advanced so it’s not possible in a scope)
Create a new Class extending the Eloquent Builder, for Example CustomEloquentBuilder.
use Illuminate\Database\Eloquent\Builder;
class CustomEloquentBuilder extends Builder
{
public function myMethod()
{
// some method things
}
}
Create a Custom Model and overwrite the method newEloquentBuilder
use Namespace\Of\CustomEloquentBuilder;
use Illuminate\Database\Eloquent\Model;
class CustomModel extends Model
{
public function newEloquentBuilder($query)
{
return new CustomEloquentBuilder($query);
}
}
Change Database Query Builder
For example to modify the where-clause for all database accesses
Create a new Class extending the Database Builder, for Example CustomQueryBuilder.
use Illuminate\Database\Query\Builder;
class CustomQueryBuilder extends Builder
{
public function myMethod()
{
// some method things
}
}
Create a Custom Model and overwrite the method newBaseQueryBuilder
use Namespace\Of\CustomQueryBuilder;
use Illuminate\Database\Eloquent\Model;
class CustomModel extends Model
{
protected function newBaseQueryBuilder()
{
$connection = $this->getConnection();
return new CustomQueryBuilder(
$connection, $connection->getQueryGrammar(), $connection->getPostProcessor()
);
}
}
Laravel Version: 5.5 / this code is untestet
The answer above doesn't exactly work for laravel > 5 so I done some digging and I found this!
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L1868
use this instead!
protected function newBaseQueryBuilder()
{
$conn = $this->getConnection();
$grammar = $conn->getQueryGrammar();
return new QueryBuilder($conn, $grammar, $conn->getPostProcessor());
}