I am am trying to save to my database, and as part of that save I am trying to sync my many to many relationship, however I am getting the following error from my API,
"BadMethodCallException","message":"Call to undefined method Illuminate\\Database\\Query\\Builder::sync()"
I would have thought that this is because the relationships I have in my model are not many to many so cant be synced, but they look correct to me,
class Organisation extends Eloquent {
//Organsiation __has_many__ users (members)
public function users()
{
return $this->belongsToMany('User')->withPivot('is_admin');
}
//Organisation __has_many__ clients
public function clients()
{
return $this->belongsToMany('Client');
}
//Organisation __has_many__ teams
public function teams()
{
return $this->belongsToMany('Team');
}
//Organisation __has_many__ projects
public function projects()
{
return $this->hasMany('Project');
}
}
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
public function organisations()
{
return $this->belongsToMany('Organisation')->withPivot('is_admin');
}
}
I am running the sync after a successful save,
if(isset($members)) {
$organisation->users()->sync($members);
}
and members is certainly set. The organsisation is created in the following way,
public function create()
{
//
$postData = Input::all();
$rules = array(
'name' => 'required',
);
$validation = Validator::make(Input::all(), $rules);
if($validation->fails()) {
return Response::json( $validation->messages()->first(), 500);
} else {
$organisation = new Organisation;
// Save the basic organistion data.
$organisation->name = $postData['name'];
$organisation->information = $postData['information'];
$organisation->type = 'organisation';
/*
* Create an array of users that can used for syncinng the many-to-many relationship
* Loop the array to assign admins to the organisation also.
*/
if(isset($postData['members'])) {
$members = array();
foreach($postData['members'] as $member) {
if(isset($postData['admin'][$member['id']]) && $postData['admin'][$member['id']] == "on") {
$members[$member['id']] = array(
'is_admin' => 1
);
} else {
$members[$member['id']] = array(
'is_admin' => 0
);
}
}
}
/*
* Create an array of clients so we can sync the relationship easily
*
*/
if(isset($postData['clients'])) {
$clients = array();
foreach($postData['clients'] as $client) {
$clients[] = $client['id'];
}
}
/*
* Create an array of teams so we can sync the relationship easily
*
*/
if(isset($postData['teams'])) {
$teams = array();
foreach($postData['teams'] as $team) {
$teams[] = $team['id'];
}
}
/*
* Create an array of projects so we can sync the relationship easily
*
*/
if(isset($postData['projects'])) {
$projects = array();
foreach($postData['projects'] as $project) {
$projects[] = $project['id'];
}
}
if( $organisation->save() ) {
if(isset($members)) {
$organisation->users()->sync($members);
}
if(isset($teams)) {
$organisation->teams()->sync($teams);
}
if(isset($teams)) {
$organisation->clients()->sync($clients);
}
if(isset($projects)) {
$organisation->projects()->sync($projects);
}
$organisation->load('users');
$organisation->load('teams');
$organisation->load('clients');
$organisation->load('projects');
return Response::make($organisation, 200);
} else {
return Response::make("Something has gone wrong", 500);
}
}
}
I was looking a while for the problem and I didn't see any (I was looking at first sync as you suggested) but I looked again and I think the problem is not syncing users here. Probably the problem is:
if(isset($projects)) {
$organisation->projects()->sync($projects);
}
You are trying to use sync on 1 to many relationship because you defined it this way:
return $this->hasMany('Project');
So either change hasMany here into belongsToMany if it's many to many relationship (that's probably the case) or don't use sync here for $projects because it works only for many to many relationship.
Related
When I run the code I get no error but the data I am trying to display is not displaying it's just blank.. can someone tell me what I'm doing wrong?
My controller:
public function openingPage($id) {
$this->getGames();
$games = $this->getGames();
return view('caseopener')->with('games',$games);
}
private function getGames() {
$games = array();
foreach ($this->data->items as $item) {
$game = new Game($item);
$games[] = array(
'id' => $game['id'],
'name' => $game['name'],
'price' => $game['price'],
'image' => $game['image'],
);
}
return $games;
}
The 'Game' Model that is used in 'getGames function':
class Game extends Model
{
private $id;
public $data;
public function __construct($id) {
parent::__construct();
$this->id = $id;
$this->data = $this->getData();
}
private function getData() {
$game = DB::table('products')->where('id', 1)->first();
if(empty($game)) return array();
return $game;
}
}
The view:
#foreach ($games as $game)
<div class="gold">$ {{ $game['price'] }}</div>
#endforeach
I think you are over-complicating things. You could simplify your flow like this:
Given your provided code, it seems like you are using a custom table name ('products') in your Game model. So we'll address this first:
Game.php
class Game extends Model
{
protected $table = 'products'; //
}
Now, it seems like you're searching an array of Game ids ($this->data->items). If so, you could make use of Eloquent for your query, specially the whereIn() method:
YourController.php
public function openingPage($id)
{
$games = Game::whereIn('id', $this->data->items)->get();
return view('caseopener')->with('games', $games);
}
Optionally, if you want to make sure of just returning the id, name, price and image of each Game/product, you could format the response with API Resources:
php artisan make:resource GameResource
Then in your newly created class:
app/Http/Resources/GameResource.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class GameResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'price' => $this->price,
'image' => $this->image,
];
}
}
So now just update your controller:
YourController.php
use App\Http\Resources\GameResource;
public function openingPage($id)
{
$games = Game::whereIn('id', $this->data->items)->get();
return view('caseopener')->with('games', GameResource::collection($games));
} // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I've been trying for countless hours now, but still having issues updating a models relationship, the closest I've got to is a 'Method fill does not exist.' error.
Listing model:
class Listing extends Model
{
protected $fillable = [
'uid', 'start_date',...........
];
public function locations()
{
return $this->hasMany('App\ListingLocation');
}
}
Location (relationship to listing - hasMany):
class ListingLocation extends Model
{
protected $fillable = [
'listing_id', 'location',
];
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
This returns my model and relationship, which I can view with dd($listing)
$listing = Listing::with('locations')->findOrFail($id);
This will update my listing model, which I can see the changes after calling dd($listing) again
$listing->fill($array);
However when I attempt to fill the relationship as per below, I get 'Method fill does not exist.'
$listing->locations->fill($array['locations']);
How can I update the relationship successfully before calling $listing->push();?
Change your location to a single record, not a collection
For example:
$listings->locations->first()->fill($array['locations']);
to fill every record use foreach
#foreach($listings->locations as $location)
$location->fill(do_something);
#endforeach
I ended up creating a new class to extend hasMany which allowed me to use sync as per alexweissman at https://laracasts.com/discuss/channels/general-discussion/syncing-one-to-many-relationships.
Extract from forum:
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* #link https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Relations/HasMany.php
*/
class HasManySyncable extends HasMany
{
public function sync($data, $deleting = true)
{
$changes = [
'created' => [], 'deleted' => [], 'updated' => [],
];
$relatedKeyName = $this->related->getKeyName();
// First we need to attach any of the associated models that are not currently
// in the child entity table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->newQuery()->pluck(
$relatedKeyName
)->all();
// Separate the submitted data into "update" and "new"
$updateRows = [];
$newRows = [];
foreach ($data as $row) {
// We determine "updateable" rows as those whose $relatedKeyName (usually 'id') is set, not empty, and
// match a related row in the database.
if (isset($row[$relatedKeyName]) && !empty($row[$relatedKeyName]) && in_array($row[$relatedKeyName], $current)) {
$id = $row[$relatedKeyName];
$updateRows[$id] = $row;
} else {
$newRows[] = $row;
}
}
// Next, we'll determine the rows in the database that aren't in the "update" list.
// These rows will be scheduled for deletion. Again, we determine based on the relatedKeyName (typically 'id').
$updateIds = array_keys($updateRows);
$deleteIds = [];
foreach ($current as $currentId) {
if (!in_array($currentId, $updateIds)) {
$deleteIds[] = $currentId;
}
}
// Delete any non-matching rows
if ($deleting && count($deleteIds) > 0) {
$this->getRelated()->destroy($deleteIds);
$changes['deleted'] = $this->castKeys($deleteIds);
}
// Update the updatable rows
foreach ($updateRows as $id => $row) {
$this->getRelated()->where($relatedKeyName, $id)
->update($row);
}
$changes['updated'] = $this->castKeys($updateIds);
// Insert the new rows
$newIds = [];
foreach ($newRows as $row) {
$newModel = $this->create($row);
$newIds[] = $newModel->$relatedKeyName;
}
$changes['created'][] = $this->castKeys($newIds);
return $changes;
}
/**
* Cast the given keys to integers if they are numeric and string otherwise.
*
* #param array $keys
* #return array
*/
protected function castKeys(array $keys)
{
return (array) array_map(function ($v) {
return $this->castKey($v);
}, $keys);
}
/**
* Cast the given key to an integer if it is numeric.
*
* #param mixed $key
* #return mixed
*/
protected function castKey($key)
{
return is_numeric($key) ? (int) $key : (string) $key;
}
}
You can then override Eloquent's hasMany method in your model class:
/**
* Overrides the default Eloquent hasMany relationship to return a HasManySyncable.
*
* {#inheritDoc}
*/
public function hasMany($related, $foreignKey = null, $localKey = null)
{
$instance = $this->newRelatedInstance($related);
$foreignKey = $foreignKey ?: $this->getForeignKey();
$localKey = $localKey ?: $this->getKeyName();
return new HasManySyncable(
$instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
);
}
/**
* Get all of a user's phone numbers.
*/
public function phones()
{
return $this->hasMany('App\Phone');
}
A sync method will now be available to any hasMany relationships you have on this model:
$user->phones()->sync([
[
'id' => 21,
'label' => "primary",
'number' => "5555551212"
],
[
'id' => null,
'label' => "mobile",
'number' => "1112223333"
]
]);
I'm working with PageKit CMS. I have 2 tables with Many To Many relation (item and type).
Item model:
class Item implements \JsonSerializable
{
...
/**
* #ManyToMany(targetEntity="Type", tableThrough="#prefix_item_type", keyThroughFrom="item_id", keyThroughTo="type_id")
*/
public $types;
...
}
Type model:
class Type implements \JsonSerializable
{
...
/**
* #ManyToMany(targetEntity="Item", tableThrough="#prefix_item_type", keyThroughFrom="type_id", keyThroughTo="item_id")
*/
public $items;
...
}
In backend interface on item edit page I created multi select with all types. When I send item save request, I get type ids.
My save item method have a look:
public function saveAction($data, $id = 0, $selected_types = [])
{
/*
* $selected_types = array(2) {
* [0]=>int(1)
* [1]=>int(2)
* }
*/
if (!$id || !$item = Item::query()->related(['types'])) {
if ($id) {
App::abort(404, __('Item not found'));
}
$item = Item::create();
}
if (!$data['slug'] = App::filter($data['slug'] ?: $data['title'], 'slugify')) {
App::abort(400, __('Invalid alias'));
}
if(!App::user()->hasAccess('ext_name: manage all items')) {
$data['user_id'] = App::user()->id;
}
if(!App::user()->hasAccess('ext_name: manage all items') && !App::user()->hasAccess('ext_name: manage own items') && $item->user_id !== App::user()->id) {
App::abort(403, __('Access denied'));
}
$item->save($data);
/*
* Here I need to sync $item->types with $selected_types ids
*/
return [
'message' => 'success',
'entity' => $item,
];
}
How can I sync this relation if I have current item id and new type ids?
Is there a way that the below code can be shortened? It's starting to look a bit messy and I wanted to know if there was a better way.
/**
* Update user.
*
* #param $request
* #param $id
* #return mixed
*/
public function updateUser($request, $id)
{
// Get user
$user = $this->user->find($id);
// Sync job titles
if($request->has('job_title'))
{
$user->jobTitles()->sync((array)$request->get('job_title'));
} else {
$user->jobTitles()->detach();
}
// Sync employee types
if($request->has('employee_type'))
{
$user->employeeTypes()->sync((array)$request->get('employee_type'));
} else {
$user->employeeTypes()->detach();
}
if($request->has('status')) {
$data = $request->only('first_name', 'last_name', 'email', 'status');
} else {
$data = $request->only('first_name', 'last_name', 'email');
}
// Save user changes
return $this->user->whereId($id)->update($data);
}
Any help would be appreciated.
Here is what I would have done:
Extracted the ManyToMany relationships to their own methods.
Initialized the $data variable and overridden it if necessary.
Removed the comments. The code is readable enough, no need for them
Code:
public function updateUser($request, $id)
{
$user = $this->user->find($id);
$data = $request->only('first_name', 'last_name', 'email');
$this->syncJobTitles($user);
$this->syncEmployeeTypes($user);
if($request->has('status')) {
$data['status'] = $request->status;
}
return $user->update($data);
}
private function syncJobTitles($user)
{
if(request()->has('job_title'))
{
$user->jobTitles()->sync((array) request()->get('job_title'));
} else {
$user->jobTitles()->detach();
}
}
private function syncEmployeeTypes($user)
{
if(request()->has('employee_type'))
{
$user->employeeTypes()->sync((array) request()->get('employee_type'));
} else {
$user->employeeTypes()->detach();
}
}
There are different ways to refactor that code:
If that code is only used on that part of your code you could leave it there or
Option 1
Move that business logic to the user model
class User extends Eloquent
{
...
public function applySomeRule($request)
{
if($request->has('job_title')) {
$this->jobTitles()->sync((array)$request->get('job_title'));
} else {
$this->jobTitles()->detach();
}
// Sync employee types
if($request->has('employee_type')) {
$this->employeeTypes()->sync((array)$request->get('employee_type'));
} else {
$this->employeeTypes()->detach();
}
}
}
And your controller could finish like
public function updateUser($request, $id)
{
// Get user
$user = $this->user->find($id);
$user->applySomeRule($request);
if($request->has('status')) {
$data = $request->only('first_name', 'last_name', 'email', 'status');
} else {
$data = $request->only('first_name', 'last_name', 'email');
}
// Save user changes
return $this->user->whereId($id)->update($data);
}
Option 2 If that business logic is used on different controller methods you can use Middlewares, so that you move that logic to middleware and in your route definition you use the middleware you created, let's say SomeRuleMiddleware.
Your routes would look like:
Route::put('user/{id}', [
'middleware' => 'SomeRuleMiddleware',
'uses' => 'YourController#updateUser'
]);
Option 3 You could move all your business logic to Repositories (read about Repository Pattern) and SOLID principles, that way your logic and rules will keep on Repositories and your controllers would keep clean, something like this:
class YourController extends Controller
{
protected $userRepo;
public function __construct(UserRepository $userRepo)
{
$this->userRepo = $userRepo;
}
public function updateUser($request, $id)
{
$data = $request->all();
$result = $this->userRepo->updateUser($id, $data);
return $result;
}
}
I hope I can explain this clearly, apologies in advance if it is confusing. I have a goals table which hasOne of each of bodyGoalDescs, strengthGoalDescs and distanceGoalDescs as shown below
goals.php
class Goal extends BaseModel
{
protected $guarded = array();
public static $rules = array();
//define relationships
public function user()
{
return $this->belongsTo('User', 'id', 'userId');
}
public function goalStatus()
{
return $this->hasOne('GoalStatus', 'id', 'goalStatus');
}
public function bodyGoalDesc()
{
return $this->hasOne('BodyGoalDesc', 'id', 'bodyGoalId');
}
public function distanceGoalDesc()
{
return $this->hasOne('DistanceGoalDesc', 'id', 'distanceGoalId');
}
public function strengthGoalDesc()
{
return $this->hasOne('StrengthGoalDesc', 'id', 'strengthGoalId');
}
//goal specific functions
public static function yourGoals()
{
return static::where('userId', '=', Auth::user()->id)->paginate();
}
}
each of the three tables looks like this with the function details changed
class BodyGoalDesc extends BaseModel
{
protected $guarded = array();
public static $rules = array();
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'bodyGoalDescs';
//define relationships
public function goal()
{
return $this->belongsTo('Goal', 'bodyGoalId', 'id');
}
}
a goal has either a body goal, a strength goal, or a distance goal. I am having a problem with this method in the controller function
<?php
class GoalsController extends BaseController
{
protected $goal;
public function __construct(Goal $goal)
{
$this->goal = $goal;
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$thisgoal = $this->goal->find($id);
foreach ($this->goal->with('distanceGoalDesc')->get() as $distancegoaldesc) {
dd($distancegoaldesc->DistanceGoalDesc);
}
}
}
when I pass through goal 1 which has a distance goal the above method dies and dumps the Goal object with the details of goal 1 and an array of its relations including an object with DistanceGoalDes.
when I pass through goal 2 it passes through exactly the same as if I had passed through goal 1
if I dd() $thisgoal i get the goal that was passed through
what I want ultimately is a method that returns the goal object with its relevant goal description object to the view but this wont even show me the correct goal details not too mind with the correct relations
this function is now doing what I want it to do, I am sure there is a better way (besides the fact that its happening in the controller right now) and I would love to hear it.
public function show($id)
{
$thisgoal = $this->goal->find($id);
if (!$thisgoal->bodyGoalDesc == null) {
$goaldesc = $thisgoal->bodyGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('bodygoaldesc', $goaldesc);
} elseif (!$thisgoal->strengthGoalDesc == null) {
$goaldesc = $thisgoal->strengthGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('strengthgoaldesc', $goaldesc);
} elseif (!$thisgoal->distanceGoalDesc == null) {
$goaldesc = $thisgoal->distanceGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('distancegoaldesc', $goaldesc);
}
}