Laravel hasMany Relationship error Undefined property - laravel

I am trying to return all media that I have linked to a device via the database. I had this working in an older version of laravel but now on the most recent verion I have copied and pasted the code over and I am getting an error.
I run a schedule and each schedule has media linked to it.
Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Schedule extends Model
{
//
public function scheduled_media(){
return $this->hasMany('App\ScheduledMedia');
}
}
Blade:
#foreach($schedule->scheduled_media as $m))
{{$m->name}}
#endforeach
Error:
Undefined property: stdClass::$scheduled_media (View: C:\xampp\htdocs\displaycontroller\resources\views\sites\showdisplay.blade.php)
I am lost as I don't understand why it's saying that it is an undefined property when I have it written in the Model as you can see above.

I found the issue. It was the way I was getting my data in the controller using raw. By changing to Schedule::all() it worked.

Related

Error Class "App\Exports\SOAReportExport" not found in laravel 8.x

I am using Laravel 8.x, livewire and etc. also laravel-excel package for exporting data.
I want to export data from a generated report and there is no model for it.
I am not sure that is it possible that can I export without model or not.
however, the problem is that laravel doesn't know my export class.
I have import this class at the top:
use Livewire\Component;
use App\Exports\SOAReportExport;
use Maatwebsite\Excel\Facades\Excel;
...
public function exportSOA()
{
$results = $this->results;
return Excel::download(new SOAReportExport($results), 'SOAReport.csv');
}
when I hover over it it shows:
Undefined type 'App\Exports\SOAReportExport'
Glad you found the solution, it's been 7 months but i want to let the answer to anyone is dealing this easy problem.
You mistyped the class name in 'App\Exports\SOAReportExport' export file.
Cheers.

Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [App\Event]

I'm creating a view list for M-M relationship for Admin view. I want to display every user that has been registered for that particular events. And I'm pretty sure I called it wrong thats the reason why I getting this error. I'm not quite sure on how to fix it. I tried googling and I still don't find any solution yet. So how can I fix this?
The relation name is users not user. Please check using below code
use DB;
use App\Event;
use App\User;
public function show()
{
$events = Event::with('users')->get();
return view('admin.event.user')->with('events', $events);
}

How can I fix error: class not found using Laravel?

I'm trying to work on my model using Laravel.
Table name: refregions
Model: app/Refregion
Controller: in one of my function I have this code:
$regions = Refregion::all();
And I defined the model on controller
use app\Refregion;
Can someone tell me why class cannot be found?
Thank you.
Because you are using the wrong path of Model in your controller.
you have to use the following one instant of this one 'use app\Refregion;'.
use App\Refregion;
In your Controller you have to use like this below
use App\Refregion;
Or
use \App\Refregion;

CodeIgniter: Undefined property when calling the model

I use CodeIgniter 3 with HMVC.
Does anyone know why do I get the following error
Severity: Notice
Message: Undefined property: Login::$login_model
On the second line below. I clearly call the model, but for some odd reason it is not linked properly
$this->load->model('auth/login_model');
$response = $this->login_model->attempt($username, sha1($password));
Then the model is pretty basic :
<?php
class Login_model extends CI_Model
{
public function attempt($user, $pass)
{
...
Now if I use an object it works, but I have the same issue in many places, including the place where I have
$this->db->select("....
where is crashing as there is no "db". What is the new solution for CodeIgniter 3? I've seen older posts, but none seem to help me out
Thanks!
just try this code put in controller:
public function __construct() {
parent::__construct();
$this->load->model('Login_model'); // load model
}
Problem is resolved, the issues were caused by the fact that my controller extended CI_Controller instead of MX_Controller. So changing
class Login extends CI_Controller
to
class Login extends MX_Controller
resolved the issue.
It took me a while to figure it out by debugging the thirdparty/MX/Loader.php, but once I saw it was looking for MX_Controller type I did the change and it worked perfectly.
This issue is one in many related to migration from CI 2 to CI 3 and also using the HMVC from Wiredesignz. Another big one is uppercase of file names and uppercase on the calls, so strictly referring to this issue I had to uppercase the calls in my controller (changed "login" to "Login"):
$this->load->model('auth/Login_model');
$response = $this->Login_model->attempt($username, sha1($password));
I did the above change already, so this was no longer a blocker, still I wanted to put it here just in case someone hits the exact same issue
Thanks all for your help

How can I render a twig template in a custom controller in Silex?

I'm playing with Silex microframework to build a very simple app.
The Silex documentation briefly illustrates how to keep your code organised using controller as class and I've also found this useful article talking about the same practice:
https://igor.io/2012/11/09/scaling-silex.html
but still can't solve my problem
The issue:
in my app.php I'm using
$app->get('/{artist}', 'MyNamespace\\MyController::getAlbum');
This is working. MyController is a class correctly loaded through composer using psr-4.
At the moment the return method of getAlbum($artist) is return $player;
What I'd like to do instead, is returning a twig view from getAlbum, something like:
return $app['twig']->render('player.twig', $player);
To do so, what I've tried to do in my custom class/controller is:
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
[...]
public function getAlbum(Request $request, Application $app, $artist)
but this is generating the following error when I try to access the routed pages:
ReflectionException in ControllerResolver.php line 43:
Class MyNamespace\Request does not exist
Whic made me think that there's a namespace conflict between myNamespace and the Silex namespaces?!
What am I doing wrong?
Is this the right way to make $app visible in my custom controller in order to use return $app['twig']... ?
Thank you in advance!
EDIT
After several other tries still didn't get to the point (replies still welcome!) but I've found a workaround solution that could be useful to anyone will incur in a similar issue. Directly in my app.php I added this
$app->get('/{artist}', function (Silex\Application $app, $artist) use ($app)
{
$object = new MyNamespace\MyController();
$player = $object->getAlbum($artist);
return $app['twig']->render('player.twig',
array(
//passing my custom method return to twig
'player' => $player,));
});
then in my player.twig I added:
{{player | raw}}
And this basically means that I still need an anonymous function to get use of my custom method which is a working solution I'm not really happy with because:
I'm using 2 functions for 1 purpose.
The return value of getAlbum is dependent from the use of "raw" in twig.
SOLVED
The workflow described works fine. It was a distraction error: I've placed the namespace of my custom class after use Symfony\Component\HttpFoundation\Request;
namespace declaration in PHP needs always to be at the top of the file, Silex wasn't able to injects $app and $request for this reason.

Resources