How to rectify class not found in psy shell in laravel tinker? - laravel

I am very new to the laravel tinker part, I have one model based on that model I am trying to update some column value but it's throwing an error please help me to resolve the issue.
User::update(["status"=>"active"])->where ('id',1);
Error I am getting is
PHP FATAL error: class 'User' not found in psy shell code on line1

Try User::update(["status"=>"active"])->where ('id',1)->first();
or better still:
$user = User::find(1);
$user->update(["status"=>"active"]);
Where 'plans' comes from must be a mistake in your User model. Did you add private $with(['plans']); or something similar in the model? Maybe you should post your User.php model in the question.

Related

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);
}

Scope left Join Sub doesn't work - Laravel

I'm new to Laravel I'm making a clone of Twitter. I'm making a scope to get all the likes from the DB, but I get an error from Tinker
I know some basic SQL Queries, but this one is quite complicated, so I've got no idea what to do now.
Tweet model
public function scopeWithLikes(Builder $query)
{
$query->leftJoinSub(
'select tweet_id, sum(liked) likes, sum(!liked) dislikes from likes group by tweet_id',
'likes',
'likes.tweet_id',
'tweet.id'
);
}
Tinker command
App\Tweet::withLikes()->first();
Tinker error
TypeError: Argument 1 passed to App/Tweet::scopeWithLikes() must be an
instance of Illuminate/Database/Query/Builder, instance of
Illuminate/Database/Eloquent/Builder given, called in
C:/wamp64/www/laravel/tweety/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
on line 1164
I hope I explained it well, but if you need more information please ask me.
Thanks for your answer!
i think that you are using the wrong class for your scope, scope use
Illuminate/Database/Query/Builder
as a parameter while you pass
Illuminate/Database/Eloquent/Builder
in your Twit model file, on the top ...
remove:
use Illuminate/Database/Eloquent/Builder;
and paste:
use Illuminate/Database/Query/Builder;
Did this solve the problem? Because I run into exactly the same issue.
When I change Eloquent to Query, the error message is still the same.
Also the source that is given on Git uses use
Illuminate\Database\Eloquent\Builder;
Hubert

Laravel hasMany Relationship error Undefined property

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.

Laravel about Requests

i am new in programing laravel. so here I will not ask about my problems but I am here just want to ask how to use laravel. here I would like to ask how to use:
Determining If an Input Value Is Present in laravel. and what it does?
thanks ^^
Laravel is really a good MVC Framework.
I can suggest you some source from where you can get better understanding of Laravel Framework.
https://laravel.com/docs/5.2
https://laracasts.com/skills/laravel
For simple example - How to use Laravel after installation
Go to path using terminal ex. /var/www/laravel/project
Go to - /var/www/laravel/project/resources/views
Create a directory test and create a file index.php
Create a controller - php artisan make:controller TestController
Create a function testGet() and return view - return view('test.index'); //test is directory and index is file
Create a function testPost(Request $request) and to get data use $data = $request->all(); and then print this data.
Create a model with migration file - php artisan make:model Test --migration
Go to route file - /var/www/laravel/project/app/Http/routes.php
Add line Route::get('/test', 'TestController#testGet');
Add line Route::post('/test', 'TestController#testPost');
Now check GET request to your project http://project/test it will call testGet function.
POST request http://project/test?name=Adam will call testPost function and will print your name.
as said in the comments you better check laracasts! its the laravel school
anyway to anwser your question, its simple
View
<input type="text" name="fname">
Controller
public function doingstuff (Illuminate\Http\Request $request) {
if ($request->has('fname')) {
// a man gotta do what a man gotta do here
}
}
smooth huh? :3

Joomla 2.5 Load custom field in components layout fatal error

Im trying to load custom field in my backend component default view (default.php):
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$productType = JFormHelper::loadFieldType('ProductType',false);
$productTypeOptions = $productType->getOptions();
But I get a fatal error:
Fatal error: Call to a member function children() on a non-object in xxx\libraries\joomla\form\fields\list.php on line 89
When I load this custom field into the form, everything works perfectly.
Any ideas?
Make sure you are adding the correct path to the fields
In your $productType->getOptions() function,
Try removing:
$options = array_merge(parent::getOptions(), $options);
Well, I tried to expand my fellow above idea but it seemed an inappropiate edit, I put it here then:
This worked for me. In your getOptions, if you have something like the getOptions found here (http://docs.joomla.org/How_to_add_custom_filters_to_component_admin), you´ll have this line:
$options = array_merge(parent::getOptions(), $options);
This is the one that makes the error. Why? Well, I´m not sure. If you see the related file, you´ll find this:
foreach ($this->element->children() as $option)
So the problem is that you are calling children() on the model parent that it seems is not initialized. Why the array_merge is needed? Is discussed here (http://forum.joomla.org/viewtopic.php?f=626&t=782877)
My explanation is more like a dirty blind patch, but hope it helps to move forward.

Resources