Currently i have app builded using:
- Models
- Keyword
- Video
- Repositories
- VideoRepository
- KeywordRepository
- Controllers
- VideoController
- KeywordController
Keyword and Video is pivoted
In KeywordController i would like to display Keywordlist only for selected video_id.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Keyword;
use App\Repositories\KeywordRepository;
class KeywordController extends Controller
{
public function index(KeywordRepository $keywordRepo, $video_id){
$keywords = $keywordRepo->getAll();
dump($keywords);
}
At this moment i can't use find($video_id). Is there any way to use VideoRepository for retrieving correct video and than retrieve $keywordRepo elements for it?
$keywords = $keywordRepo->where('id',$video_id)->first();
Related
I have a project I'm working on right now, in which I created a database to hold some info. I've already created a view and a controller where I can add data to the DB and it works. But for some reason when I try to use on a view to list my information it won't work.
Here's my model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model
{
protected $fillable = ['descricption','image'];
}
And these are the first few lines of my view which laravel point to error:
#php
use\app\Banner;
$banner = new Banner;
$banners = Banner::all();
#endphp
Whenever I try to open this view, I get the "Class 'app\Banner' not found" error. What am I doing wrong? Since it worked to add data to the database before, and I did remember of putting the use\app\Banner;
The namespace is App not app. Always act like case sensitivity matters and you won't have these problems.
use App\Banner;
Side Note: you can pass this data to your view instead of having to do this query in your view.
hi m trying to get view of page but it says: Class App\Http\Controllers\CategoriesController does not exist
pic of folder structure https://ibb.co/gMBvwDJ
Route:
Route::match(['get','post'],'/admin/categories/index','CategoriesController#Category');
controller:
public function index()
{
return view('admin.categories.index');
}
First of all you use a wrong definition of the route match, so try this:
Route::match(['get','post'], 'CategoriesController#index');
and second, make sure that in your CategoriesController you use the right namespace, which should be:
namespace App\Http\Controllers;
at the very top of the class.
It gives you that error because the controller couldn't find the class you are calling so in order to fix the problem, in the top of your controller add
use App{ModalName};
for example if your model for this is called categories,
use App\categories;
as well as add
namespace App\Http\Controllers;
Please make sure your controller is placed in below directory structure or else you have to fix the namespace problem
I am trying (and failing!) to generate a list of factsheets from the database that have been tagged according to language (English or Vietnamese). I have a route in which the variable can be either "en" or "vn" being passed to the index function in my controller
Route::get('/pest/{id}', 'api\FactsheetsController#index');
My controller is currently as...
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class FactsheetsController extends Controller
{
public function index($lang)
{
$factsheets = DB::table("factsheetcontents as fsc")
->where('fsc.lang',$lang)
->select('fsc.title', 'fsc.id', 'fsc.shortimg')
->get();
return $factsheets;
}
I receive an empty response ([]) for my efforts. What am I doing wrong here?
Thanks, Tom
I have created a new directory Library in root of Laravel.
Inside I put the file with class:
class My {
//
}
So, in controller Laravel I try to get access to this class:
App\Library\My
But Laravel does not determine this path.
This is my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use View;
use App\Library\My;
class HomeController extends Controller
{
//
}
A complete and functional example based on the posts here:
1 - Folder and file - Create a folder under app/, in this example we will create a folder called Library.
We will also inside the folder create a file with the name of your class, here we will create a class called My.
So we will have app/Library/My.php
2 - Class and method - Now, for testing, inside the class create a static method called myMethod
<?php
namespace App\Library;
class My
{
public static function myMethod()
{
return 'it\'s work!';
}
}
3 - Controller - Now at the beginning of the Controller, we will declare the namespace of your Class with use:
<?php
namespace App\Http\Controllers;
use App\Library\My;
//rest of the controller code
Finally, to create an instance of the My class, in Controller, a new statement must be used:
//rest of the controller code
public function index()
{
$whatever = new My;
return $whatever::myMethod();
}
As above, make sure it is placed in the App directory and make sure it is properly namespaced e.g.
<?php
$fOne = new \App\library\functions;
$isOk = ($fOne->isOk());
?>
You should create Library folder inside app folder
namespace App\Library\My
app folder is alrdy used psr-4
In your controller
use App\Library\My as My
It's work for me. Hope this answer is helpful
You have to properly namespace your every class.
So you can import your class with use keyword, like so
use App\Library\My;
....
$my = new My();
Or if you've conflicting class name then you can use as keyword to alias the classname while importing
use App\Library\My as MySecond;
....
$my = new MySecond();
And if you want to directly access your class within the method then you can access it like so.
$my = new \App\Library\My();
Note: The leading \ means App was declared in the global scope.
I have a new installation of Laravel 5. The problem is that it's not recognizing my model classes. I will keep it very simple for solution purposes.
Route::get('test', function() {
$test = boxstyle::all();
....
}
My model is in the app directory
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Boxstyle extends Model {
protected $table = 'boxstyle';
protected $primaryKey = 'key1';
}
I am getting:
class boxstyle not found error
I've been searching all over the internet and can't find a solution. This installation is fresh. This isn't magic and I suspect a configuration issue but I can't find a solution. This works fine in Laravel 4.2 so I know it should work but not working in L5.
Your model is defined inside the App namespace. If the code accessing the model is not in the same namespace as the model, you need to qualify it.
Route::get('test', function() {
$test = \App\Boxstyle::all();
}
Laravel 4 did not define the models and controllers inside namespaces. Your models would have been defined inside the global namespace, so any code also in the global namespace (like your controller) would not need to qualify the model. However, Laravel 5 has made the push to put most everything inside namespaces.
To create a model in Laravel 5 try this,create a folder name "Models" folder under your App folder, now for instance you want to create a Model class for Boxstyle ...under your Models folder create a file name
"BoxstyleModel.php" inside your BoxstyleModel.php should look like this and make sure your inside the folder to namespace your model under the App\Models
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BoxstyleModel extends Model {
public static function say_hello(){
return "Hello";
}
}
in your Boxstyle Controller to be able to use your BoxstyleModel added this code at the top of your BoxstyleController use App\Models\BoxstyleModel;
now everything should be the same like Laravel 4.X.