Laravel 5: dynamic class name in morphedByMany - laravel-5

Is this possible in laravel 5?
public function attachable($type)
{
// sample: $type = 'SomeContent';
return $this->morphedByMany($type::class, 'message_attachable');
}
I am getting this error:
Dynamic class names are not allowed in compile-time ::class fetch
edit:
[2018-06-17 22:59:13] local.ERROR: Dynamic class names are not allowed in compile-time ::class fetch {"userId":1,"email":"system#gmail.com","exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 64): Dynamic class names are not allowed in compile-time ::class fetch at U:\\www\\prado247\\app\\Models\\Message\\Message.php:25)
[stacktrace]
#0 {main}
"}

You have to specify the classpath:
public function attachable($type)
{
// sample: $type = 'SomeContent';
return $this->morphedByMany('App\\'.$type, 'message_attachable');
}

Is type meant to be the string name of the class or is it an object of that instance? If it's an instance and you want to know which class it is an instance of you should use
return $this->morphedByMany(get_class($type), 'message_attachable');
You can read about the documentation of get_class in the PHP Docs but basically it returns the class of the instance as a string that Eloquent can use.

Related

Accessing Property of a method from another method inside a class in Laravel

I want to Access the property "na" of "ha" public method from "test" method in same class. But I am getting error "Trying to get property 'ni' of non-object"
public function ha(){
$ni = 'fs';
$nin = 'adfsfsfs';
}
public function test()
{
echo $this->ha()->ni;
}
Variables inside a method are only available inside that method. You should either define on your class or return the values from the method and call the method to get the values. Also please note that this is not about laravel, but it's about php. For more information about variable scope: check this

Can multi word variable be passed to blade component?

I have created new component using php artisan make:component Test.
Now, when creating that component, if I try to pass one word variable everything works fine (<x-test :testID="'test'"). But when I try passing it as multi word, separating words by using underscore (<x-test :test_id="'test'"), I am facing next error:
Unresolvable dependency resolving [Parameter #0 [ <required> $test_id ]] in class App\View\Components\Test
And that class looks like this:
class Test extends Component
{
public $test_id;
public function __construct($test_id)
{
$this->test_id = $test_id;
}
public function render()
{
return view('components.test');
}
}
Is this normal behaviour and is it even allowed to use underscore when creating variables inside components?
Livewire components do not support the classical __construct constructor. Instead you better use the mount() method.
Furthermore you should work with public properties like this
public $test_id;
public function mount()
{
// properly initialize the var
$this->test_id = 0;
}
and reference (see Data Binding) it in your view with
<x-test wire.model="test_id"/>
I generally would always go for camelCase naming, f. ex. testId. This is definately fully supported in Livewire (see docs).
Some general hint about naming variables
What you describe with one word and multi word variable is named
camel case, f. ex. myVariableName, Wikipedia
snake case, f. ex. my_variable_name, Wikipedia
Update for comment component
class Comments extends Component
{
public int $postId;
public function mount()
{
$this->postId = 0;
}
public function render()
{
$comments = Comments::where('post_id', $this->postId)->get();
return view('post.list', [
'comments' => $comments,
]);
}
}
In your views you can use something like this.
<x-comments :postId="$post->id"/>
You don't need to inject it into the mount method. Just use the public property.
I face the same issue. I can't pass in :dd_array="$dd_color" to an anonymous blade component, but passing it like :ddArray="$dd_color" works fine.

why im getting error count(): Parameter must be an array or an object that implements Countable in laravel?

I tried to define a local scope in my project to show popular(most downloaded)in my Home Page...so this is my File Model codes:
public function scopePopular($query)
{
return $query->orderBy('file_download_num');
}
As you know "file_download_num" is the field in database im trying to get data from...
so this is how I used this scope in my HomeController:
$popularFiles = File::Popular()->get();
I didn't use count() method in my Controller or Model!But still getting Error!so any suggestion?
notice:php version is 7.3
I found that adding a toArray() method in controller can help count() method work correctly...it worker for me...so controller should be like this:
$popularFiles = File::Popular()->get()->toArray();

How to extend Code igniter cache class Redis class

I am using redis for caching in one of my assignment. I am using CI default redis library for this purpose.
Now the issue with library is that it has some specific set of method which are used to set, get, delete , increment and decrement the redis keys & values.
I want to additional function of redis like lpush, rpush,lrem, lrange etc.
So to achieve this , i am trying to extend default CI redis class. which i am putting in application/libraries/driver/cache_redis_extended.php.
my code for this class is as follow.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Cache_redis_extended extends CI_Cache_redis
{
function __construct()
{
parent::self;
}
public function rpush($list, $data)
{
$push = $this->_redis->multi(Redis::PIPELINE);
return $push->rpush($list, json_encode($data));
}
public function lrem($list, $data)
{
if((is_string($data) && (is_object(json_decode($data)) || is_array(json_decode($data))))) {
$data = $data;
}else{
json_encode($data);
}
return $this->_redis->lrem($list,-1, $data);
}
public function __destruct()
{
if ($this->_redis)
{
$this->_redis->close();
}
}
}
and in my model I am loading this class as follows
$CI->load->driver('cache', array('adapter' => 'redis'));
But I get this error:
Unable to load the requested class: cache_redis_extended
Any help is appreciated for this issue.
As I can see your driver name is not started with Capitalized , so
it can be possible the cause of your issue.
Because according to codeigniter the naming rule of a class as follows
Naming Conventions
File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.
change your file name
from cache_redis_extended.php
to Cache_redis_extended.php
I hope it will be helpful for you.

Laravel 4: get url parameter from controller

I'm new to laravel 4 and I'm currently having the following problem:
In my routes.php I have the line:
Route::get('maps/{map_type}', 'MapsController#loadMaps');
And in my MapsController I'd like to get the {map_type} to use it.
So my question: How can I retrieve map_type in my Controller?
Your first argument in your loadMaps method is your map type.
public function loadMaps($map_type) {
return $map_type;
}
Take a look at the first two code snippets on http://laravel.com/docs/controllers.
Controllers receive parameters as parameters in the called function:
class MapsController extends BaseController {
public function loadMaps($map_type) {}
}
Check http://laravel.com/docs/controllers#basic-controllers for more info

Resources