Reference: How can I update an existing Eloquent relationship in Laravel 4?
$userinfo = \Userinfo::find($id);
\User::find($id)->userinfo()->associate($userinfo)->save();
I'm getting the error: Call to undefined method Illuminate\Database\Query\Builder::associate()
Here is the entire method:
public function saveUser($id)
{
$user = \User::find($id);
$userdata = \Input::all();
$rules = array(
'email' => 'required|email',
'state' => 'size:2',
'zip' => 'size:5',
'phone' => array('regex:/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/')
);
$validator = \Validator::make($userdata, $rules);
if ($validator->passes())
{
if ($userdata['email'] !== $user->email)
{
$rules = array('email' => 'unique:users');
$validator = \Validator::make($userdata, $rules);
if ($validator->fails()) return Redirect::route('admin.user.edit', array('user' => $user))
->with('error', 'Specified email already exists.');
}
$user->email = $userdata['email'];
$user->firstname = $userdata['firstname'];
$user->lastname = $userdata['lastname'];
$userinfoArray = array(
'address' => $userdata['address'],
'city' => $userdata['city'],
'state' => $userdata['state'],
'zip' => $userdata['zip'],
'phone' => preg_replace('/[^0-9]/', '', $userdata['phone'])
);
$user->save();
if (!$user->userinfo)
{
$userinfo = new \Userinfo($userinfoArray);
$userinfo = $user->userinfo()->save($userinfo);
}
else
{
$userinfo = \Userinfo::find($id);
\User::find($id)->userinfo()->associate($userinfo)->save();
//$user->userinfo()->update($userinfoArray);
}
return \Redirect::route('admin.user.detail', array('id' => $id))
->with('success', 'User updated.');
}
return \Redirect::route('admin.user.edit', array('id' => $id))
->withInput()
->withErrors($validator);
}
associate() is a method of the belongsTo relationship, but it looks like from the above you are trying to call it via the hasOne relationship.
I am just guessing as you have not provided your eloquent model class code so can't see how you have set the relationships exactly, but if you have:
class User extends Eloquent {
public function userinfo()
{
return $this->hasOne('Userinfo');
}
}
class Userinfo extends Eloquent {
public function user() {
return $this->belongsTo('User');
}
}
Then associate needs to be called against Userinfo as this has the belongsTo relationship to which the associate() method is attached.
For example
$user = \User::find(4);
$userinfo = \UserInfo::find(1);
$userinfo->user()->associate($user);
$userinfo->save();
Will set the foreign key user_id in the user_info table to the id of the $user object.
Looking at your above code it doesn't appear that this is what you are actually trying to do and that the
$user->userinfo()->update($userinfoArray);
call which you have commented out will in fact do what you seem to be trying to achieve, which is to update the userinfo that is related to the current user if that user already exists.
Hope this helps.
Glen
Change hasOne to belongsTo. It will look like:
class User extends Eloquent {
public function userinfo()
{
return $this->belongsTo('Userinfo');
}
}
class Userinfo extends Eloquent {
public function user() {
return $this->belongsTo('User');
}
}
I was stuck on this problem for a few days and it ended up being quite simple to solve. I had created a folder called 'models' in my 'app' folder but I had forgotten to reconfigure my auth.php file.
This was my error.
Call to undefined method Illuminate\Database\Query\Builder
I fixed it by opening the auth.php in the config folder and changing the following line to include my models folder.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => Foodie\User::class,
fix:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => Foodie\Models\User::class,
Hope this helps!
You need to specify the field related like this:
public function profile()
{
return $this->hasOne('App\AdmProfile', 'id');
}
Related
I have simple comment rating logic now.
For example i have following controller :
public function rating_change(Request $request, Comment $comment)
{
if ($request['action'] == 'up') {
$comment->positive_rating = $comment->positive_rating + 1;
} else if ($request['action'] == 'down') {
$comment->negative_rating = $comment->negative_rating + 1;
}
$comment->save();
return ['positive' => $comment->positive_rating, 'negative' => $comment->negative_rating];
}
And the route for that method:
Route::put('/comments_rating/{comment}', function (Comment $comment, Request $request) {
$commentController = new CommentController();
return $commentController->rating_change($request, $comment);
});
Model:
class Comment extends Model
{
use HasFactory;
protected $fillable = [
'body',
'user_id',
'item_id'
];
protected $casts = [
'user_id' => 'integer',
'item_id' => 'integer',
];
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
public function post()
{
return $this->belongsTo(Items::class, 'id');
}
}
And resource:
return [
'id' => $this->id,
'user_id'=>$this->user_id,
'body'=>$this->body,
//https://github.com/jenssegers/date
'created_at' => Date::parse($this->created_at)->diffForHumans(),
'updated_at' => $this->updated_at->format('Y-m-d H:i'),
'author'=>[
'id'=>$this->author->id,
'name'=>$this->author->name,
],
'rating'=>[
'positive'=>$this->positive_rating,
'negative'=>$this->negative_rating
]
];
The current purpose to prevent change rating by the same user multiple times.(Server side block)
And return the following flag (changed or smth) to frontend.
How should i to do this?
Should i use the separate table and store all user actions to get flag of changed them in all my comments?
Should i use the https://laravel.com/docs/8.x/redis for that purpose or sql is enough?
Maybe there is some built in laravel solutions or libraries?
I use laravel sanctum to authorize.
I have one DB which has 3 tables: user, statuses and friends.
My status table has a relationship column "parent id" which is NULL but stores the user_id of a user that replies to a status. Below is my Status.php code:
namespace Pictogram\Models;
use Illuminate\Database\Eloquent\Model;
class Status extends Model
{
protected $table = 'statuses';
protected $fillable = [
'body'
];
public function user()
{
return $this->belongsTo('Pictogram\Models\User', 'user_id');
}
public function scopeNotReply($query)
{
return $query->whereNull('parent_id');
}
public function replies()
{
return $this->hasMany('Pictogram\Models\Status', 'parent_id');
}
}
And blow is from my route file: This controls replies to status
Route::post('/status', [
'uses' => '\Pictogram\Http\Controllers\StatusController#postStatus',
'as' => 'status.post',
'middleware' => ['auth'],
]);
Route::post('/status/{statusId}/reply', [
'uses' => '\Pictogram\Http\Controllers\StatusController#postReply',
'as' => 'status.reply',
'middleware' => ['auth'],
]);
And my status controller .php
class StatusController extends Controller
{
public function postStatus(Request $request)
{
$this->validate($request, [
'status' => 'required',
]);
Auth::user()->statuses()->create([
'body' => $request->input('status'),
]);
return redirect()
->route('home')
->with('info', 'Status updated.');
}
public function postReply(Request $request, $statusId)
{
$this->validate($request, [
"reply-{$statusId}" => 'required',
], [
'required' => 'The reply body is required.'
]);
$status = Status::notReply()->find($statusId);
if (!$status) {
return redirect()->route('home');
}
if (!Auth::user()->isFriendsWith($status->user) && Auth::user()->id !== $status->user->id)
{
return redirect()->route('home');
}
$reply = Status::create([
'body' => $request->input("reply-{$statusId}"),
])->user()->associate(Auth::user());
$status->replies()->save($reply);
return redirect()->back();
}
}
And lastly this is the line 2673 of models .php below:
protected function getRelationshipFromMethod($method)
{
$relations = $this->$method();
if (! $relations instanceof Relation) {
throw new LogicException('Relationship method must return an object of type '
.'Illuminate\Database\Eloquent\Relations\Relation');
}
return $this->relations[$method] = $relations->getResults();
}
I am using Laravel 5.2. Now my issue is that the reply gets save to the table because the empty parent_id then takes the user_id of the user who replied but then it brings up an error page that has these errors below.
Error1/2
Error2/2
Make sure your relationship name is proper in Status model.
check your replies() realtion in Status model.
it return the Status Relationship i should be the realtion of replies i.e Reply
public function replies()
{
return $this->hasMany('Pictogram\Models\Reply', 'parent_id');
}
I am newbie in Laravel so i really need some help. I want to ask when
I commented the part 'photo' => required why if I update without entering the photo it shows some error like call to a member function getClientOriginalName() on null. So the real question is I want to update without entering photo and it should still to be updated.
This is my code in Controller to upload photo
public function update($id, UpdateBannerRequest $request)
{
$input = $request->all();
//get original file name
$filename = Input::file('photo')->getClientOriginalName();
$input['photo'] = $filename;
Input::file('photo')->move($this->path, $filename);
$banner = $this->BannerRepository->findWithoutFail($id);
if (empty($banner)) {
Flash::error('Banner not found');
return redirect(route('banner.index'));
}
$banner = $this->BannerRepository->update($input, $id);
Flash::success('Banner updated successfully.');
return redirect(route('banner.index'));
}
This is the code on my model
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Banner extends Model
{
use SoftDeletes;
public $table = 'banners';
protected $dates = ['deleted_at'];
public $fillable = [
'title',
'description',
'photo',
'status'
];
protected $casts = [
'title' => 'string',
'description' => 'string',
'photo' => 'string',
'status' => 'integer'
];
public static $rules = [
'title' => 'required',
'description' => 'required',
//'photo' => 'required',
'status' => 'required'
];
}
$validator = Validator::make(
$request->all(),
array(
'photo' => 'required',
),
array(
'photo' => 'Please choose file',
)
);
If Photo is not mandatory directly use this
if(!empty($request->photo)){
//do something
}
else{
Flash::error('Banner not provided');
return redirect(route('banner.index'));
}
Hope this will help.. let me know if any issue..Thank you
your update function would look like
public function update($id, UpdateBannerRequest $request)
{
$input = $request->all();
$banner = $this->BannerRepository->findWithoutFail($id);
if(!empty($request->photo)){
//do something for saving the name of file in database and other value respectively using
// $filename = Input::file('photo')->getClientOriginalName();
// $banner->photo = $filename;
}
else{
Flash::error('Banner not provided');
return redirect(route('banner.index'));
}
$banner->save();
Flash::success('Banner updated successfully.');
return redirect(route('banner.index'));
}
The simplest validation required would be to test if Input::hasFile('photo'), this should be placed before you call Input::file('photo')->getClientOriginalName()
if( Input::hasFile('photo') == false )
{
Flash::error('Banner not provided');
return redirect(route('banner.index'));
}
https://laravel.com/docs/4.2/requests#files
You should check bellow code.
if(isset(Input::file('photo'))
Before work with it.
I know this is the same quetion title like others, but i cant find the right answer for me. Here is my problem.
I am trying to insert data to the other table after users data inserted. But what i got is Call to a member function create() on null.
Here is my code of AuthController.php
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user->usersbasics()->create([
'user_id' => $user->id,
]);
return $user;
}
Here is my User model
public function usersbasics()
{
$this->hasOne('App\UsersBasics');
}
Here is my UsersBasics model:
public function user()
{
$this->belongsTo('App\User');
}
I tried var_dumb($user->usersbasics()) and the result is NULL.
What is whrong with my code? because i use the same for my other work, and its fine. Please someone explain me and give me a solution. Please..
thank you,
This is a late answer, but I ran into the same problem.
You don't have the return keyword on both your relationships.
public function user()
{
return $this->belongsTo('App\User');
}
and
public function usersbasics()
{
return $this->hasOne('App\UsersBasics');
}
This fixxed it for me.
You need to set the parameters fillable in your User model.
class User extends Model
{
protected $fillable = ['name', 'email', 'password'];
}
Your controller method is protected instead of public.
If you look at the existing AuthController, we have function you mentioned:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
This method is then called with postRegister (public, so it can be called from routes.php), where we pass only the input from Request as the parameter.
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$user = $this->create($request->all());
return redirect($this->redirectPath());
}
I assume you tried to call the create() method directly from controller, which doesn't work because it's protected and can only be called from within the class itself. If you really want to make create() method work without postRegister, you would do something like this:
public function create(Request $request)
{
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => bcrypt($request->input('password')),
]);
$user->usersbasics()->create([
'user_id' => $user->id,
]);
return $user;
}
And then call this in your routes.php
Route::post('urlToPostMethod', 'Auth\AuthController#create');
The other, generally nicer way would be to call this instead
Route::post('urlToPostMethod', 'Auth\AuthController#postRegister');
I'm making a multi select form element for updating schools and specialties pivot table school_specialty. The problem is that when I change only something in multi select not other inputs or textareas, I can't listen model events so I can't sync school_specialty table. But when I fill in any other input it's works perfect. Here's my multi select from blade:
{{Form::select('specialties[]', $specialties_data, $school->specialties, array('multiple' => 'true', 'id' => 'multi-select'))}}
This is my update method from school controller:
public function update($id)
{
$data = Input::only('name', 'type_id', 'description', 'info_specialties', 'contacts', 'specialties', 'financing_id', 'district_id', 'city_id');
$school = School::find($id);
$school->name = $data['name'];
$school->type_id = $data['type_id'];
$school->description = $data['description'];
$school->info_specialties = $data['info_specialties'];
$school->contacts = $data['contacts'];
$school->cover_photo = Input::file('cover_photo');
$school->set_specialties = $data['specialties'];
$school->financing_id = $data['financing_id'];
$school->set_district_id = $data['district_id'];
$school->city_id = $data['city_id'];
try {
$school->save();
} catch (ValidationException $errors) {
return Redirect::route('admin.schools.edit', array($id))
->withErrors($errors->getErrors())
->withInput();
}
return Redirect::route('admin.schools.edit', array($id))
->withErrors(array('mainSuccess' => 'School was created.'));
}
And here's my example school model:
<?php
class School extends Eloquent {
protected $table = 'schools';
protected $fillable = array('name', 'type_id', 'description', 'city');
protected $guarded = array('id');
protected $appends = array('specialties');
public $set_specialties;
public $set_district_id;
protected static function boot()
{
parent::boot();
static::updating(function($model)
{
$data = array(
'name' => $model->name,
'type_id' => $model->type_id,
'description' => $model->description,
'specialties' => $model->set_specialties,
'city_id' => $model->city_id
);
$rules = array(
'name' => 'required|min:3|max:50',
'type_id' => 'required|min:1|max:300000',
'description' => 'required|min:10',
'specialties' => 'required|array',
'city_id' => 'required|min:1|max:300000'
);
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
throw new ValidationException(null, null, null, $validator->messages());
} else {
return true;
}
});
static::updated(function($model)
{
if ( $model->set_specialties != null )
{
$model->specialty()->sync($model->set_specialties);
}
});
}
public function specialty()
{
return $this->belongsToMany('Specialty', 'school_specialty');
}
}
?>
When updating only school specialities the School model events aren't triggered because the School model stays the same.
I think the simplest and most elegant solution is to touch the school model instance. This will modify the updated_at field for the School object and thus trigger the model events.
To do this add the following lines before the try/catch block:
if ($school->set_specialties !== null) {
$school->touch();
}
Also, validation shouldn't be handled in the model observers. Check form request validation here: https://laravel.com/docs/5.6/validation#form-request-validation.