Hi I'm having a problem about my codes in my PagesController.
I'm wondering what is the error about the notification it says undefined variable but it has the same codes in countOrder and countProduct and those two are working just only the notification
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\CustomerOrder;
use App\Product;
use App\Notification;
use DB;
class PagesController extends Controller
{
public function count() {
$countOrder = CustomerOrder::count();
$countProduct = Product::count();
$notification = Notification::count();
return view('/adminIndex',['customer_orders' => $countOrder],['products' => $countProduct],['notifications' => $notification]);
}
}
There may be a few issues here.
Firstly, when passing data to a view, you must use one array (rather than several):
return view('/adminIndex', ['customer_orders' => $countOrder, 'products' => $countProduct, 'notifications' => $notification]);
Secondly, the first argument of the view() helper expects the view file (found in the /resources/views folder). So if the file is adminIndex.blade.php use:
return view('adminIndex', ['customer_orders' => $countOrder, 'products' => $countProduct, 'notifications' => $notification]);
Hope this helps.
Related
I am trying to create a factory for a BlogPost model.
A blog post belongsToMany Tag and vice versa.
There is an intermediate table (blog_post_tag) to store the relations of blogposts to tags.
I would like to seed a blog post with a number of tag names.
How can one seed a DB using factories and intermediate tables?
May have an answer here
So I can add the following below to my BlogPost seeder. This would also create tags, but I would like to get existing tags (preferably 3-5 and only if any exist).
hasAttached() accepts a factory as the first argument so this will not work.
BlogPost::factory()
->hasAttached(
Tag::factory()->count(3)
)
->create();
This is how I fill my relations with existing data:
<?php
namespace Database\Factories;
use App\Models\Alpha;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Beta;
use App\Models\Gamma;
class AlphaFactory extends Factory {
protected $model = Alpha::class;
public function definition() {
$beta_ids = Beta::all()->pluck('id');
$gamma_ids = Gamma::all()->pluck('id');
return [
'name' => Str::slug($this->faker->unique()->realText(50)),
'number' => $this->faker->randomNumber(3),
'beta_id' => $this->faker->randomElement($beta_ids),
'gamma_id' => $this->faker->randomElement($gamma_ids)
];
}
}
If you want to be fancy and create sometimes new entries to relate to:
<?php
namespace Database\Factories;
use App\Models\Alpha;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Beta;
class AlphaFactory extends Factory {
protected $model = Alpha::class;
public function definition() {
$beta_id;
$beta_ids = Beta::all()->pluck('id');
if($beta_ids->isEmpty() || $this->faker->boolean($chanceOfGettingTrue = 50)) {
$beta_id = Beta::factory()->create()->id;
} else {
$beta_id = $this->faker->randomElement($beta_ids);
}
return [
'name' => Str::slug($this->faker->unique()->realText(50)),
'number' => $this->faker->randomNumber(3),
'beta_id' => $beta_id
];
}
}
I don't know if this is the right way to do this, but it works for me. If there is a better way, let me know.
I want to implement a search option on my Laravel application, but had this error:Class 'Illuminate\Support\Facades\Input' not found, I have tried to add this row at config/app like this:
'aliases' => [
....
'Input' => Illuminate\Support\Facades\Input::class,
Also at Controller I have added those rows:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
At Route I have added
Route::any('/search',function(){
$image_tmp = $request->image;
$fileName = time() . '.'.$image_tmp->clientExtension();
$q = Input::get ( 'q' );
$book = Book::where('title','LIKE','%'.$q.'%')->get();
if(count($book) > 0)
return view('home')->withDetails($book)->withQuery ( $q );
else return view ('home')->withMessage('No Details found. Try to search again!');
});
But still it doesn't work.
Try this
config/app.php
use Request instead of Input
'aliases' => [
....
'Input' => Illuminate\Support\Facades\Request::class,
And your controller
use Illuminate\Http\Request;
and remove use Illuminate\Support\Facades\Input; top of your code
you have used the class in the controller but your route is never going to one as you are using a closure. so add Input class in your web.php file. at the top of your web.php file add
<?php
use Illuminate\Support\Facades\Input;
if you are using any latest version of laravel, the Input class no longer exists. so use Request class instead
<?php
use Illuminate\Http\Request;
you can use request() global helper too to get request values.
however i would suggest you to not use closure, rather use controller for logical operation.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\News;
use App\User;
use App\Event;
public function index()
{
$datanews['newss'] = News::orderBy('created_at','desc')->get();
$dataevent['events'] = Event::orderBy('created_at','desc')->get();
$user['users'] = User::all();
return view('user/aktuelles.index',$datanews, $dataevent,$user);
}
this is a part of my controller, i can show news and events in my view but everytime it says it can not find the variable user?
i dont know why because i dont know why it shows me the other variables but not the user variable???
in my view i can see the other variables like this:
#foreach ($newss as $news)
{{$news->newstitel}}
#endforeach
but i want to show user like this:
{{$user[1]->avatar}}
i tried everything so i tried so look my controller like this:
$user = DB::table('users')->get();
or get the variable via compact
and i tried exactly the same so like this
$user['users'] = User::orderBy('created_at','desc')->get();
and a foreach in my view but it can not found the variable user??
i dont know why
this is the error
Undefined variable: users
return view('user.aktuelles.index', compact('datanews', 'dataevent', 'user');
I am not sure how the helper view() handles the 4th parameter you are passing. According to the docs you can pass data like this:
return view('user/aktuelles.index',[
'datanews' => $datanews,
'dataevent' => $dataevent,
'user' => $user,
]);
or
return view('user/aktuelles.index')->with([
'datanews' => $datanews,
'dataevent' => $dataevent,
'user' => $user,
]);
Maybe give this way a try.
Edit:
The view() helper accepts 3 Parameters: $view = null, $data = [], $mergeData = [] so the 4th parameter you are trying to pass is not even working. You have to pass your data as second parameter or with the chained function with(array $data).
You have to return value in view like below method:
Change this line
$user['users'] = User::orderBy('created_at','desc')->get();
To
$user = User::orderBy('created_at','desc')->get();
return view('user/aktuelles.index', ['datanews' => $datanews,'dataevent'=> $dataevent,'users'=>$user]);
Now, You will get result inside your view using 'users'.
You are not passing variables to view correctly use with or compact to pass variables to view.
i prefer compact as it is simple and easier.
Passing data to view using compact:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\News;
use App\User;
use App\Event;
public function index()
{
$datanews = News::orderBy('created_at','desc')->get();
$dataevent = Event::orderBy('created_at','desc')->get();
$user = User::all();
return view('user.aktuelles.index',compact('datanews', 'dataevent','user'));
}
Passing variables to view using with:
public function index()
{
$datanews = News::orderBy('created_at','desc')->get();
$dataevent = Event::orderBy('created_at','desc')->get();
$user = User::all();
return view('user.aktuelles.index)->with(['datanews' => $datanews,'dataevent'=> $dataevent,'users'=>$user]);
}
You should try this:
use Illuminate\Support\Facades\DB;
use App\News;
use App\User;
use App\Event;
public function index()
{
$data['news'] = News::orderBy('created_at','desc')->get();
$data['events'] = Event::orderBy('created_at','desc')->get();
$data['users'] = User::all();
return view('user/aktuelles.index', $data);
}
My framework is Laravel 5.2, How to use {faq} in blade ?
Route is:
Route::get('help/{faq?}', ['as' => 'help', 'uses' => 'Site\Help\IndexController#index']);
URL is:
http://localhost:8000/help/general
I have get {faq} in url.
In php, if this url: http://localhost:8000/help?faq=general use $_GET['faq'] But not work $_GET in balde in laravel.
please guide me.
use request()->route('faq') or {{request()->route('faq')}} in blade
Route::get('help/{faq?}', ['as' => 'help', 'uses' => 'Site\Help\IndexController#index']);
means that $faq is an acceptable argument for Site\Help\IndexController#index
So when we look at that
<?php
namespace App\Http\Controllers\Site\Help;
use Illuminate\Http\Request;
class IndexController {
public function index(Request $request, $faq) {
return view('site.help.index', compact('faq'));
}
}
If Faq is not passed as an argument, then you can get it off of the request object.
class IndexController {
public function index(Request $request) {
$faq = $request->has('faq') ? $request->get('faq') : null;
return view('site.help.index', compact('faq'));
}
}
Now in the view site.help.index you can use $faq.
Alternatively, you can use - as indicated by #sam, request->route('faq') in your view. Make sure you check that it exists however, first:
{{ request()->has('faq') ? request()->get('faq') : '' }}
In Laravel blade use {{request()->get('faq')}}
Since faq an optional parameter, you should do this in index() action:
public function index($faq = null)
Then you can use $faq variable and check if parameter does exist with is_null($faq)
Message:
Class 'Helpers\Config' not found
File:app/classes/Helpers/Helper.php
public static function getDictionary(){
$timezone = Config::get('app.dictionary');
File: config/app.php.
'aliasis => array(
'Helper' => 'Helpers\Helper'
File: app/config/app.php
'dictionary' => array(
'EMAIL' => 'crm#xxxx.com',
'VERSION' => "1.0.0"
)
Is there any namespace or use line that I've missed?
Because you're in a namespace in the Helpers\Helper file, when you refer to Config in that file PHP assumes you mean a class in the Helpers\ namespace. You need to prefix it with a \ to say you mean the 'root' Config class, or use a use statement:
Refer to it directly:
$timezone = \Config::get('app.dictionary');
use it:
use Config;
class Helper
{
public static function getDictionary()
{
$timezone = Config::get('app.dictionary');
// ...
}
}