I am new in laravel and php. I am developing a magazine site with laravel 4. I want to store home page in laravel cache . But In my homepage , there are many queries . Please see details below.:
My HomepageController index method:
public function index()
{
return View::make('homepage');
}
My Query Helper Function which is used for post by category in my Homepage:
public static function cat_post($category, $limit)
{
$posts = Post::whereHas('categories', function($q) use ($category)
{
$q->where('category_slug', 'like', $category);
})->with('categories')->take($limit)->orderBy('created_at', 'DESC')->get();
return $posts;
}
In My homepage.blade.php , I used this helper function many times. like below:
<?php $national = Helper::cat_post('national', 3); ?>
#foreach ($national as $post)
{{ Helper::img_src($post) }}
<h4>{{ $post->title }}</h4>
</div>
#endforeach
Now i want to put homepage in cache and when new post created, then delete old homepage from cache and store new one.
Please help me. Is it possible ?????
To store a value in cache you can use Laravel's Cache:
public function index()
{
return Cache::rememberForever('homepageCache', function()
{
return View::make('homepage');
});
}
This snipped tries to retrieve a cached version of View::make('homepage') and otherwise creates it.
Every time a post is created, updated or deleted you need to invalide your homepageCache:
Cache::forget('homepageCache');
Related
How to pass id from veiw into controller in then into another view again??
First of all this a basic question you should be able to search it anywhere on the internet. any how below is the solution.
web/routes.php:
Route::get('user/{id}','USerController#find')->name('user.get');
Or by passing user object in the route:
Route::get('user/{user}','USerController#find')->name('user.get');
UserController:
the below function accepts user object as a parameter in the route we defined above.
public function find(user $user)
{
return view('user.detail',compact('user'))
}
or
public function find($id)
{
$user = USer::find($id);
return view('user.detail',compact('user'))
}
resources/view/user/detail.blade.php:
{{ $user -> name }}
{{ $user -> email }}
and in order visit the route user.find use the below line:
View Detail
No you can use this code as reference to your project.
first solution is .
change route
Route::get('/show_vedio/{$id}', 'VedioController#show')->name(show_videos);
and use this {{ route('show_vedios', $vedio->id )}} in href
it's not entirely clear what you are trying to do, maybe i will help you:
public function show(int $id): View
{
$video = Video::find($id);
return view('video.show', ['video' => $video]);
}
I have modified default auth method in controller which redirects user after custom login to set_password page. The problem is I can redirect it well to the desired page but I need to simultaneously pass two dynamic variables which are returned through querying database, which I am unable to pass with redirect.
My modified controller method is as follows:-
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
I know that to pass a variable to an view, I need to use the compact method like follows:-
return view('set_password', compact('activated_up', 'showuser'); but it cant be done with redirect.
The way I have redirected means I am passing parameters to route in the controller method, but I need to pass variables to the redirected view instead of parameters. How to achieve that?
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
you can use With
return redirect()->route('set_password')->with('data', ['some kind of
data']);
in your view
#if (session::has('data'))
The data is {{ session::get('data') }}
#endif
Tried as described in the answer by Kuldeep Mishra but could not achieve it though, anyways I found a workaround to achieve my desired output. What I did is changed my authenticated method to this:-
protected function authenticated(Request $request, $user)
{
return redirect()->route('set_password');
}
I only redirected to the set_password route from the above method and made new method in the controller to show the view with the compacted variables like this:-
public function setPasswordForm(Request $request)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return view('set_password', compact('activated_up', 'showuser'));
}
And a small change in route web.php file:-
Route::get('/set_password', 'Controller#setPasswordForm')->name('set_password');
So finally I was able to redirect to desired page with the desired view loaded with dynamic variables.
I try to get list of all users with post and link them to different page where all their posts is listed.
Obviously I must make my list page first then my users detail page, but some how I ended up with detail page and no list page :)))
Anyway: this is what I have so far:
Controller
public function chef() {
//
return view('front.chef');
}
public function chefdetail($username) {
$foods = Food::whereHas('user', function($q) use($username){
$q->where('username',$username);
})->get();
return view('front.chef-detail', compact('foods'));
}
My routes:
Route::get('/chef/{username}', 'FrontendController#chefdetail')->name('chefdetail');
Route::get('/chefs', 'FrontendController#chef')->name('chefs');
Questions (Problems) :
How do I get list of users with posts in my chef function?
How do I add username to my chefs detail page URL
Is it correct that I used $user = User::find(1); to getting users list? because I couldn't get users by id it returns error.
With this code you pass all your users that have posts to your blade page
public function chef() {
$users = User::has('posts')->get();
return view('front.chef', compact("users"));
}
In your blade you can show them like:
#foreach ($users as $user) {
{{ $user->username }}
#endforeach
then your chefdetail function should look like this:
public function chefdetail($username) {
$user = User::where("username", $username)->with("posts")->first();
return view('front.chef-detail', compact('user'));
}
and you can show the post like this:
#foreach ($user->posts as $post) {
$post->someproperty
#endforeach
hope this helps you.
I have models User and Diares set up with the hasMany and BelongTo relstionship. I'm trying to fetch data from the Diaries table associated with a particular user. I tried auth but it didnt work for me. How can i achieve that and display it in the blade view as well. Here is my current code:
public function index(User $id)
{
$user = User::find($id);
$record = $user->diary;
return view('home')->with('record', $record);
}
The blade file it should display to:
#foreach ($record as $diary)
{{ $diary->error }}
{{ $diary->fix }}
#endforeach
In your index function the $id is not integer - this is a User instance, so you can try use this:
public function index(User $user)
{
$record = $user->diary;
return view('home')->with('record', $record);
}
I am a beginner in laravel so please excuse me if this is a simple question
I am trying to pass a db variable through my url so e.g blog?=category
It displays all results but when i change the url to blog/category1 it doesn't display anything.
DB Fields:id,category, name
1 category1 test
This is in my routes:
Route::get( '/blog', 'BlogController#index' );
Route::get('/', 'HomeController#index');
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category);
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')
->with('posts', $posts);
});
BlogController
class BlogController extends BaseController {
public function index()
{
// get the posts from the database by asking the Active Record for "all"
$posts = Post::all();
// and create a view which we return - note dot syntax to go into folder
return View::make('blog.index', array('posts' => $posts));
}
}
This is my view layout:
#extends('base')
#section('content')
#foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
#endforeach
#stop
Thanks
You need to add ->get() after your Eloquent query to fetch the results.
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category)->get();
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')->with('posts', $posts);
});