I have three table company,group and company_group.
I am getting values from post while saving a company.
In my company form i have multiple select box which has a list of group.
My company is getting saved correct-ally but now i want to add record in company_group table with last inserted id of company and selected group.
I have set up has_many in both models company and group but cannot save multiple records in company_group table so help me with this.
Company model
class Company extends DataMapper {
var $has_one=array("group");
function __construct() {
parent::DataMapper();
}
}
group model
class Group extends DataMapper {
var $has_many=array("company");
function __construct() {
parent::DataMapper();
}
}
company controller
class Company_c extends CI_Controller {
public function index() {
$company = new company();
$group=new group();
$company->id=1;
$group->id=2;
$group->id=4;
$company->save($group);
}
}
Please help me with this
You're not loading the company or group objects properly. When you instantiate a company, you need to do:
$company = new Company();
$company->where('id', 1)->get();
or, quicker as a shorthand (as long as you use the id property):
$company = new Company(1);
(Note that the class name is case-sensitive).
You then do the same with the group(s) you want to load:
$groups = new Group();
$groups->where_in('id', array(2, 4)->get();
Then, when both are instantiated, you can save the company and its group(s) in one go:
$company->save($groups);
Make sure you go through the DataMapper docs thoroughly, it couldn't really be explained more clearly:
http://datamapper.wanwizard.eu/pages/save.html#Relationship
Related
I have create two tables
AREAS
-id
-areaName
SOCIETIES
-id
-area_id
-societyName
My relationship is defined like this
class Area extends Model
{
//
public function society()
{
return $this->hasMany('App\Society');
}
}
class Society extends Model
{
public function area()
{
return $this->belongsTo('App\Area');
}
}
Now I have created objects for both area and society
$area = new \App\Area();
$area->areaName = $input['areaName'];
$society = new \App\Society();
$society->societyName = $input['societyName'];
$society->area()->associate($area);
After this when I call "$society->save()" its not saving any record and giving error area_id cannot be null. I want to save society and its relation area in one go.
When I use "$area->save()" before associate its saving the record when calling "$society->save()". All I want to do is skip "$area->save()" and save both society and area in one go.
It is giving you the error because you don't have a record in the database with an id for it. You need to save the area record first and then associate it with the relation. I don't think it will work other wise, take a look at eloquent docs on laravel's website:
https://laravel.com/docs/5.1/eloquent-relationships
There is such a structure:
There is a model book (Book) and model systems age restrictions (Rars).
One book can be only one rars, but on one rars can refer a lot of books. That is, the relationship - one to many?
The model Book:
class Book extends Model
{
public function rars()
{
return $this->belongsTo('App\Rars');
}
}
The model Rars:
class Rars extends Model
{
public function books()
{
return $this->hasMany('App\Book');
}
}
In migration Book:
$table->integer('rars_id');
$table->foreign('rars_id')->references('id')->on('rars');
Run code:
$book->rars()->save(\App\Rars::where('eternal_name', 'no_limits')->first());
(Rars with this eternal_name, guaranteed to exist)
And this return:
[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::save()
What am I doing wrong?
According to the official documentation, for updating 'Belongs To' relationships you should use associate method.
So i think this will work:
$book->rars()->associate(\App\Rars::where('eternal_name', 'no_limits')->first());
$book->save();
For more information you can read here, https://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models
Do not try to edit Rars with an Instance of Book. Make an instance of Rars instead. if you have the book id, do as following.
$book = new Book();
$book = $book->find($bookId);
$rars = new Rars();
$rars = $rars->find($book->rars_id);
/* Update data */
$rars->save();
I'm totally new in Laravel, and I'm trying to create relations between models. My tables are:
patch
id
title
area
id
location
area_patch
id
patch_id
area_id
user_area_patch
id
area_patch_id
plant_id
plant_date
User model, "patchs" function should execute something like this:
SELECT
p.id, p.title, up.plant_id, up.plant_date
FROM
rsst_farming_patch p
JOIN rsst_farming_area_patch pp,
rsst_farming_user_patch up ON pp.id = up.area_patch_id AND p.id = pp.patch_id WHERE up.user_id = 1
my models:
class User extends Model {
public function patchs() {
//return user patchs
}
}
class Patch extends Model {
public function area() {
//return area that this patch belongs to
}
}
class Area extends Model {
public function patchs() {
//return available patchs
}
}
can some one make an example of this? I would like to study it. I was messing around with User model, belongsToMany and hasManyThrough, but no luck.
You may need to modify your table structure slightly to make this happen.
I see in youruser_area_patch table you are trying to link the user, area, and patches together. That's not typically how I've done it in laravel. Normally you use a pivot table to link two items together. So let me suggest something like this:
Does a patch belong to a single user? If so you should add a user_id to the patch table.
patch
id
user_id
area_id
title
Can an a Patch be in multiple areas? I kind of doubt it, so I'd add an area_id as well.
class User extends Model {
public function patchs() {
return $this->hasMany('App/Patch', 'user_id');
}
}
class Patch extends Model {
public function area() {
return $this->belongsTo('App\Area', 'area_id');
}
}
class Area extends Model {
public function patchs() {
return $this->hasMany('App\Patch', 'patch_id');
}
}
Then you can start referencing your patchs like:
$patchs = User::find(1)->patchs()
or the area that a patch belongs to by
Patch::find(1)->area()
and all the patchs in an area by
Area::find(1)->patchs()
Does this help?
I'm fairly new with Laravel. I'm still trying to learn it. My question is:
I have 3 tables named
games
game_options
game_platforms
I have 3 Models for those tables
Game Model
class Game extends Eloquent
{
protected $table = 'games';
public function platforms()
{
return $this->hasManyThrough('GamePlatform','GameOptions','id','game_id');
}
}
GamePlatform Model
class GamePlatform extends Eloquent
{
protected $table = 'game_platform';
}
GameOption Model
class GameOptions extends Eloquent
{
protected $table = 'game_options';
}
So when I do
$game = Game::find(1)->platforms;
It only shows,
{"id":1,"platform_id":20,"game_id":1}
{"id":1,"platform_id":21,"game_id":1}
{"id":1,"platform_id":22,"game_id":1}
{"id":1,"platform_id":23,"game_id":1}
{"id":1,"platform_id":24,"game_id":1}
But I need game name and platform names with those ID's. The thing is, I want to do this with eloquent only. I could go with "DB" or oldschool SQL but I want to learn if this way is possible or not.
Also I'm looking for better documentation/books for laravel. Most of what I read were only introduce laravel or far too advanced for me.
I left a comment earlier about this but now I'm pretty sure it's the answer you're looking for: you should use belongsToMany rather than hasManyThrough. So first, might I suggest you rename your tables and models to follow Laravel's conventions (plural snake_case table names, singular snake_case alphabetical order pivot table names, singular StudlyCaps model names), that way you'll have the following situation:
Tables:
games
id
name
game_option
id
game_id
option_id
options
id
option
name
Now you can rewrite your models to conform to the new structure, and use a belongsToMany relationship too:
class Game extends Eloquent
{
public function platforms()
{
return $this->belongsToMany('Option');
}
}
class Option extends Eloquent
{
public function platforms()
{
return $this->belongsToMany('Game');
}
}
Note: you don't have to model the pivot table (game_option) unless you store extra data on the pivot.
Now you should be good to get all options for a given game:
$options = Game::find(1)->options;
Or if you need to get all platforms (though I am trying to infer meaning of your code here regarding options and platforms):
$platforms = Game::find(1)->options()->whereOption('platform')->get();
you can use the with method with eloquent
$game = Game::where('id',1)->with('platforms')->get();
Should return you the game and platforms
For documentation I would first start with the documentation provided (find it to be about 50% complete) and with the api everything else is covered
You would have to model your tables like:
**games**
id
name
**game_options**
id
game_id
name
**game_platform**
id
game_options_id
platform_id /* which i assume you get from a platform master table */
Then in your Game Class:
class Game extends Eloquent
{
protected $table = 'games';
public function platforms()
{
return $this->hasManyThrough('GamePlatform','GameOptions','game_id','game_options_id');
}
}
Now, this would be assuming that Game Platform belongs to Games through Game Options.
In my application I have several mysql tables: Toronto, Vancouver, Montreal, etc... and I am using the DB-class to work with them, eg.
$data = DB::select('select * from toronto where id = ?', array($id));
What I want to do is to start using Eloquent. I am new to Laravel and was just wondering if its possible to have one model work with several tables, smth like:
class City extends Eloquent {
protected $table_a = 'toronto';
protected $table_b = 'vancouver';
protected $table_c = 'montreal';
}
It cannot, but you can. There are many ways, here's one:
Create your a City model that asks for a table name in its constructor:
class City extends Eloquent {
public function __construct($city, array $attributes = array())
{
parent::__construct($attributes);
$this->table = $city;
}
}
To use it you'll have to instantiate your class using the table name:
$toronto = new City('toronto');
Then you can do anything you want with it:
var_dump( $toronto->where('id',701057)->get() );
You can do $model->setTable('mytable') once you have a model instanciated, but for multiple tables I would rather recommend you make one "base" model with all the functionality you need and then several other classes which extend this class but define their own table with protected $table = 'table'.
However, in your case it sounds like you shouldn't be keeping the data in separate database tables at all. If you can find a way to store the state as a column instead of in separate tables, that would be better.