I am struggling with the relation hasOne. here is what I have
User model:
class User extends Authenticatable
{
use Notifiable;
public function candidat()
{
return $this->hasOne('App\Model1');
}
public function element ()
{
return $this->hasOne('App\Model2');
}
}
Model1 class:
class Model1 extends Model
{
//
public function user()
{
return $this->belongsTo('App\User');
}
}
Model2 class:
class Model2 extends Model
{
//
public function user()
{
return $this->belongsTo('App\User');
}
}
Controller class:
public function savingcandidat(CandidatRequest $requete){
$candidat = $this->candidatSave($requete);
$user = User::query()->where('email',$requete->email)->get(); //there i get the user
//here is where i get the error : "Method Illuminate\Database\Eloquent\Collection::candidat does not exist."
$user->candidat()->save($candidat);
}
I get the error:
"Method Illuminate\Database\Eloquent\Collection::candidat does not
exist."
when I use tinker it works properly, thank you in advance for your help
$user = User::query()->where('email',$requete->email)->get();
This will return a collection result and it will not have that model/builder method. use first() instead of get if it's just one record
Like this
$user = User::query()->where('email',$requete->email)->first();
Related
I have the following model:
class EmailAddress extends Model
{
public function scopePrimary($query)
{
return $query->firstWhere('is_primary', true);
}
}
class User extends Model
{
public function emailAddresses()
{
return $this->hasMany(EmailAddress::class);
}
}
echo $user->emailAddresses()->primary()->get();
I would expect Laravel to return a model since firstWhere() essentially does LIMIT 1 in the query but instead I always get a collection with one model. Am I doing something wrong? How to fix that?
Thanks in advance!
Maybe you can utilize the hasOne of many relationship:
class User extends Model
{
public function primaryEmailAddress()
{
$this->hasOne(EmailAddresses::class)->ofMany([], function ($query) {
$query->where('is_primary', true);
});
}
}
I have a pivot model called UserTask in which I have an accessor function:
class UserTask extends Pivot implements HasMedia
{
use HasMediaTrait;
public function getCompletedAttribute()
{
return $this->getMedia()->isEmpty() && $this->completed;
}
public function task()
{
return $this->belongsTo(Task::class);
}
}
I specify the relation in my Task model like this:
class Task extends Model
{
public function users()
{
return $this->belongsToMany(User::class, 'user_task')->using('App\Models\UserTask')->withPivot('completed');
}
}
I get the following error:
"message": "Undefined property: App\Models\UserTask::$completed",
Anyone know why this is happening?
Try this accessor function:
public function getCompletedAttribute($value)
{
return $this->getMedia()->isEmpty() && $value;
}
I have been trying to associate a User with a Profile.
Here is my code:
Profile Model:
class Profile extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
}
User Model:
class User extends Authenticatable
{
...
public function profile(){
return $this->hasOne('App/Profile');
}
}
ProfileController:
public function show($username)
{
$user = User::where('username',$username)->first();
$profile = $user->profile;
return view('profiles.show')->withProfile($profile);
}
Route (It's meant to be "profil"):
Route::resource('profil','ProfileController');
Error:
http://prntscr.com/ff6y0a
Any tips on doing the rest of the CRUD functionality is very welcome!
Change App/Profile to App\Profile in your User model relationship.
I want to insert the remark field into the database, but it seems that it doesn't work. So instead, I get the following error.
Class 'App\Job' not found
Jobs.php
class Job extends Model
{
public function index()
{
return view('create-job');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
JobController.php
class JobController extends Controller
{
public function postCreateJob(Request $request)
{
// Validation
$post = new Job();
$post->remarks = $request['remarks'];
$request->job()->save($post);
return redirect()->route('home');
}
public function index()
{
return view('create-job');
}
}
Your model name is Jobs.
Need to change the name of the model to Job
Hope this helps.
How can I get all the submissions belongs to a particular user?
Trying this:
$user=User::find(1);
dd($user->submissions);
Throwing error:
Call to undefined method Illuminate\Database\Query\Builder::hasMany()
Will I have to loop through the models?
Here are the models:
class User extends Eloquent implements ConfideUserInterface, BillableInterface
{
public function categories()
{
return $this->hasMany('Category');
}
public function forms()
{
return $this->hasManyThrough('App\Models\Form', 'Category');
}
public function submissions()
{
return $this->hasMany('Category')->hasMany('Form')->hasMany('Submission');
}
}
class Category extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function forms()
{
return $this->hasMany('App\Models\Form');
}
public function submissions()
{
return $this->hasManyThrough('Submission', 'App\Models\Form', 'id', 'form_id');
}
}
namespace App\Models;
class Form extends \Eloquent {
public function user()
{
return $this->category->user();
}
public function category()
{
return $this->belongsTo('Category');
}
public function submissions()
{
return $this->hasMany('Submission');
}
}
class Submission extends \Eloquent {
public function user()
{
return $this->form->category->user();
}
public function category()
{
return $this->form->category();
}
public function form()
{
return $this->belongsTo('App\Models\Form');
}
}
That doesn't really work that way with chaining relations...
What you can do, is this:
$submissions = Submission::whereHas('form.category.user', function($q){
$q->where('id', 1);
})->get();
Note that whereHas with nested relationships has only been added in the latest Laravel 4 release. Make sure to composer update.
If i'm not getting your question wrongly, you need to define relationship correctly.
According to laravel 4.2 you can use below kind of code to define relations:
class User extends \Eloquent implements ConfideUserInterface, BillableInterface{
//...
public function submissions(){
return $this->hasMany('Submission');
}
}
//...
class Submission extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
//...
}
To further reading take a look at: http://laravel.com/docs/4.2/eloquent#relationships