query does not return id attribute - laravel-5.5

i want to show a job post. in jobposts controller i coded:
public function show(JobPost $JobPost)
{
//
if (Auth::check()) {
$JobPost = JobPost::find($JobPost->id);
$CvFolders = $JobPost->CvFolders;
return view('JobPosts.show', [
'JobPost' => $JobPost,
'cvFolders' => $CvFolders
]);
}
}
and this is my jobpost model
use Illuminate\Database\Eloquent\Model;
class JobPost extends Model
{
protected $table = 'job_posts';
protected $fillable = [
'company_id',
'user_id',
'title',
'summary',
'description',
'requirements',
'benefits',
'approval',
'location',
'publish_date',
'expiration_date'
];
public function User()
{
return $this->belongsTo('App\Models\User');
}
public function Company()
{
return $this->belongsTo('App\Models\Company', 'company_id');
}
public function CvFolders()
{
return $this->hasMany('App\Models\CvFolder');
}
i get the error: "Trying to get property of non-object"
when i looked for the reason i realized that the ($JobPost -> id) returns null.
when i did: dd $JobPost, i saw that it returns all other attributes of the job post but the id.
and my IDE says the referenced field has not been found

Related

Can't add data via postman

I want to make crud in postman with codeigniter4. But I have a problem when I will add data with post function an error occurs in postman like the picture I sent.
This is a picture of the Error section
controller Konven.php
class Konven extends BaseController
{
/**
* Get all Konven
* #return Response
*/
public function index()
{
$model = new KonvenModel();
return $this->getResponse(
[
'message' => 'Product Convent retrieved',
'produk_konven' => $model->findAll()
]
);
}
/**
* Create a new KOnven
*/
public function store()
{
$rules = [
'nama_menu' => 'required',
'tipe_id' => 'required'
];
$input = $this->getRequestInput($this->request);
if (!$this->validateRequest($input, $rules)) {
return $this->getResponse(
$this->validator->getErrors(),
ResponseInterface::HTTP_BAD_REQUEST
);
}
$konvenEmail = $input['nama_menu'];
$model = new KonvenModel();
$model->save($input);
$produk_konven = $model->where('nama_menu', $konvenEmail)->first();
return $this->getResponse(
[
'message' => 'Product Convent added successfully',
'produk_konven' => $produk_konven
]
);
}
/**
* Get a single client by ID
*/
public function show($produk_konven_id)
{
try {
$model = new KonvenModel();
$produk_konven = $model->findKonvenById($produk_konven_id);
return $this->getResponse(
[
'message' => 'Product Convent retrieved successfully',
'produk_konven' => $produk_konven
]
);
} catch (Exception $e) {
return $this->getResponse(
[
'message' => 'Could not find product convent for specified ID'
],
ResponseInterface::HTTP_NOT_FOUND
);
}
}
model KonvenModel.php
class KonvenModel extends Model
{
protected $table = 'produk_konven';
protected $allowedFields = [
'nama_menu',
'status',
'tipe_id'
];
protected $updatedField = 'updated_at';
public function findKonvenById($produk_konven_id)
{
$produk_konven = $this
->asArray()
->where(['produk_konven_id' => $produk_konven_id])
->first();
$produk_konven->join('tipe', 'produk_konven.tipe_id = tipe.tipe_id');
$produk_konven->orderBy("produk_konven_id", "asc");
if (!$produk_konven) throw new Exception('Could not find client for specified ID');
return $produk_konven;
}
}
Can friends help me overcome the obstacles that I am experiencing?

how to group this relationship in the laravel?

I am trying to group users by date that is in the relationship accesses and page this result.
Model User
public function acessos()
{
return $this->hasMany(Acessos::class,'id_usuario');
}
Model Acessos
public function user()
{
return $this->belongsToMany(User::class,'id_usuario');
}
Livewire component
$data = User::query()
->search($this->search)
->with('acessos')
->has('acessos')
->paginate($this->perPage);
return view('livewire.relatorios.acessos.acessos-component',[
'data' => $data
]);
Remove the user() method from Acessos Model and change the hasMany() method to belongsToMany in User model's acessos method. This Will solve your problem.
Remove this:
public function user()
{
return $this->belongsToMany(User::class,'id_usuario');
}
And change this:
public function acessos()
{
return $this->belongsToMany(Acessos::class,'id_usuario');
}

BadMethodCallException Method username does not exist

I have a very weird problem. When I'm submitting the form, it throws an error with server-side validation.
BadMethodCallException
Method username does not exist.
LoginController.php
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/dashboard';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web')->check())
{
return redirect()->intended('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt(['username' => $request->username(),
'password' => $request->password(), 'active' => 1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
The problem is that you trying to access the input property as method, I mean $request->username() and same with password, there is no username method, that's why you are getting error. you can access input with input() method like $request->input('username') or via dynamic property $request->username
read more at docs

Testing forms (form types) in Symfony 3.3 projects

In a Symfony 3.3 project I try to test this simple form:
class FooFormType extends AbstractType
{
private $fooService;
public function __construct(FooService $fooService)
{
$this->fooService = $fooService;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'bar',
EntityType::class,
[
'class' => Bar::class,
'choice_label' => 'title',
'placeholder' => 'Please select a bar',
]
)
->add(
'baz',
ChoiceType::class,
[
'choices' => $this->fooService->lorem(),
]
)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => Foo::class,
]
);
}
}
I followed the instruction I found at http://symfony.com/doc/current/form/unit_testing.html#testings-types-from-the-service-container to write this (simplified) test:
class FooFormTypeTest extends TypeTestCase
{
protected function getExtensions()
{
$fooServiceDummy = $this->createMock(FooService::class);
$fooFormType = new FooFormType($fooServiceDummy);
$managerRegistryDummy = $this->createMock(ManagerRegistry::class);
$entityFormType = new EntityType($managerRegistryDummy);
return [
new PreloadedExtension([$fooFormType, $entityFormType], []),
];
}
/**
* #test
*/
public function submitValidData()
{
$form = $this->factory->create(FooFormType::class);
}
}
Unfortunatelly this exception is thrown:
Symfony\Component\Form\Exception\RuntimeException: Class "AppBundle\Entity\Bar" seems not to be a managed Doctrine entity. Did you forget to map it?
What is the problem here?

How can I add a related entity to a user object at the point of creation in FOSUserBundle?

In Symfony2 RC3, I am trying to create a related entity on a User object (FOSUserBundle) at the point of user creation so that I can display the appropriate fields on an edit profile form. I am doing the following in the RegistrationFormHandler.
class RegistrationFormHandler
{
protected $request;
protected $userManager;
protected $form;
public function __construct(Form $form, Request $request, UserManagerInterface $userManager)
{
$this->form = $form;
$this->request = $request;
$this->userManager = $userManager;
}
public function process($confirmation = null)
{
$user = $this->userManager->createUser();
$this->form->setData($user);
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
if (true === $confirmation) {
$user->setEnabled(false);
} else if (false === $confirmation) {
$user->setConfirmationToken(null);
$user->setEnabled(true);
}
$prog = new \MyBundle\CoreBundle\Entity\Programme();
$prog->setStartDate(date_create());
$prog->setEndDate(date_create());
$prog->setWeeklyTarget(4);
$prog->setGoal('');
$user->addProgrammes($prog);
$this->userManager->updateUser($user);
return true;
}
}
return false;
}
}
The programme record does get created in the database but with a null user_id so it seems the association isn't working correctly. Anyone know what might be causing this?
The solution was to do $programmes->setUser($this); in the addProgrammes method of my User entity