Laravel not finding class - laravel

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.

Related

Trying to get shorter link laravel

This is my first question in this forum so please be patient to me ;)
I'm making a blog using Laravel and
I have a page with a view of all articles inside my database
enter image description here
Last column is "Action" column whet after hit the button you can see single article.
The problem is to show each content (title,subtitle etc.) i must create loooong UrL like this
http://127.0.0.1:8000/article/Test/Test/test/2020-05-08%2016:00:00
Is there any chance to cut URL to be like that:
http://127.0.0.1:800/article/Test
and still have all content?
Files
web.php
Route::get('article/{title_article}/{subtitle_created}/{text_article}/{created_at}','ReadController#index');
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ReadController extends Controller
{
public function index($title_article,$subtitle_article,$text_article,$created_at)
{
return view('article',compact('title_article','subtitle_article','text_article','created_at'))->with('title','Artykuł');
}
}
You should use the primary key (probably an incrementing id) of your database object (App\Article) to identify the object. Laravel has a wonderful function to automatically convert the primary key (id) in the route to an eloquent model.
In your case:
Route::get('article/{article}','ReadController#index');
and
public function index(Article $article) { ... }
I hope this helps you. If you don't understand parts of it, feel free to ask :)

Laravel observer not triggering condition

I'm learning Laravel and I've created a basic blog. Now, I'm trying to create something similar to an autoblog where posts change status based on either start_date or end_date. So I've created an enum table in my migration with the statuses I need to use. Right now, what I'm trying to do is change the post status to Upcoming if the date and time are greater than today but I'm not having any luck. Initially, I created an event and put the code below in the listener but is working (or not working) in the same way as in the observer. I read that it would be better in the observer if there will be several events.
The flow would be, when the post is created, check the start_date and change the status if the date is greater than today. The code below changes the status to Upcoming if I change it to creating, instead of created but it still doesn't take the start_date into account because it adds it to every post, regardless of the start_date. I tried using an if statement but I kept getting stuck. What am I missing or doing wrong?
Eventually, my goal is for the posts to change the status automatically without any user-initiated action. Perhaps, I could use Laravel queues for that but I haven't got that far yet. For the moment, I'm trying to get past this one.
<?php
namespace App\Observers;
use App\Models\Post;
use Carbon\Carbon;
class PostObserver
{
public function created(Post $post)
{
$post = Post::whereDate('start_date', '>', Carbon::now()->toDateString(){
$post->post_status = 1 // 1 = Upcoming status
});
}
}
Got it working like this, kind of a combination of the two responses, for which I'm thankful:
if (Carbon::parse($post->start_date)->greaterThan(Carbon::now()))
{
$post->post_status = 1;
}
Use method creating
<?php
namespace App\Observers;
use App\Models\Post;
use Carbon\Carbon;
class PostObserver
{
public function creating(Post $post)
{
if ($post->start_date->greaterThan(Carbon::now())) {
$post->post_status = 1;
}
}
}
Creating method will be invoked before post is saved to database. In that point you make check and only in case that start_date is bigger than current timestamp post_status will get value of 1. If you need you can set else block where you give value of 0 to post status in case it is not db default or if there is need for something like that.

Laravel 5 and models

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.

Calling model functions in a view in Laravel 4?

I am using a function to write a query, and I am trying to return the variable the function returns and use it in a view. In my model:
Fan.php
public function sample_query() {
$count = User::where('fbid', '=', 421930)->count();
return $count;
}
In the view I am simply trying to call it with:
#sample_query();
This is not working. I am new to this type of thing, and I suppose I don't know how to access the data pulled by queries in the model in the view. Please let me know if there is a better way, or why this isn't working. Thank you!
For your question it seems View Composer will solve your problem
View composers are callbacks or class methods that are called when a view is created. If you have data that you want bound to a given view each time that view is created throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".
View::composer('profile', function($view)
{
$view->with('count', User::count());
});
for more info read this
starting from the supposition that you have a User class:
{{ User::sample_query() }}

Codeigniter reusable sections

I have a table of data from a database that I want to display on various pages of my website. Ideally, i would like to just have an include or something that will go off and get the data, and return the html table. The html and data will be identical everytime I need to use this.
I wanted to know the best way of doing this
Thanks
EDIT
If this helps, something similar to a Django "inclusion" custom tag...for any django developers reading
you need to pass the variable $data to the view method.
This is your code:
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view');
}
Please change it to this in order to load the $data into the view:
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view',$data);
}
You should use a function in a model to fetch the data you need. Your controller calls the model function and sends the returned information to a view. You don't need to use traditional php includes with Codeigniter. I recommend a review of the user guide. It's very good and will tell you all the basic stuff you need to know to develop with CI. But to get you started, you watn to use Models, Views, and Controllers. Your url will tell CI what controller and function inside that controller to run. If your url is
http://www.example.com/my_controller/load_my_view
Then CI will do what is inside the load_my_view function in the my_controller controller. function load_my_view in turn instantiates a model "my_table" and runs a database query, returns information that the controller sends to the view. A basic example follows:
Your model
class my_table extends CI_Model{
function my_data(){
$this->db->select('column_1,column_2,column_3');
$this->db->from('my_table');
$query = $this->db->get();
if($query->num_rows()>0){
$result = $query->result();
}
else{
$result = false;
}
return $result;
}
}
Your controller
class my_controller extends CI_Controller{
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view');
}
}
Your View
<ul id = "my_db_results">
<?php foreach($my_results as $result):?>
<li><?php echo $result->column_1." : ".$result->column_2." ( ".$result->column_3." )";?></li>
<?php endforeach;?>
</ul>
It looks like a good oportunity to use cache: http://codeigniter.com/user_guide/libraries/caching.html
Ok, so here is one thing that worked for me, but its definitely not perfect.
I created a view in which made a call to the model getting the data then put the data into a table. This way, I only have to include this view to put the table anywhere.
I understand this completely ruins the point of having a MVC framework, but hopefully it demonstrates what I want to do...and it works

Resources