Laravel compact is not working - php

I'm trying to make a posting system, with a resource routing combination on the posts. When I try to run the app to view the posts, it returns an error stating that the posts could not be found within the view. I have the controller code for the index and the show functions:
public function index()
{
$posts = Post::latest()->get();
return view('view', compact('posts'));
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
The view that I have for the app uses the post variable to display the posts:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-2">
<div class="panel panel-default">
<!-- Posts will be displayed on the same panel -->
<div class="panel-body" id="view">
#foreach($posts as $post)
<article id="post">
<a href="/view/posts{{ $post->id }}">
{{ $post->title }}
</a>
<div class="body">
{{ $post->body }}
</div>
<!-- Footer for posts will include interaction features -->
</article>
#endforeach
</div>
</div>
</div>
</div>
</div>
Is there something that the laravel installation isn't doing correctly? Is the compact function set up correctly?

Your controller index function should be something like this:
public function index()
{
$posts = Post::get();
return view('posts.index', compact('posts'));
}

The return of the index action is wrong
return view('view', compact('posts'));
Change 'view' with 'posts'

Use compact('posts')
If you're a beginner checkout the laracasts video series to get a good understanding of the Laravel framework.

Related

laravel view doesn't show data after adding new route

It works really good, but when I've created new page something goes wrong and now my blog view doesn't show data, but blogs view still correctly show data . I am trying to show detailed data of each blog when user click on button "Details"
MainController:
public function blog(Blogs $blog)
{
return view('blog', compact('blog'));
}
public function blogs()
{
return view('blogs',['blogs' => Blogs::all(),]);
}
blogs.blade.php:
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<div class="row">
#foreach($blogs as $blog)
#include('layouts.cardBlog', compact('blog'))
#endforeach
</div>
#endsection
and cardBlog.blade.php:
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{($blog->image) }}">
<div class="caption">
<h3>{{ $blog->title }}</h3>
<p>{{ $blog->body }}</p>
<p>
<a href="{{route('blog', $blog->id) }}"
class="btn btn-default"
role="button">#lang('main.more')</a>
#csrf
</p>
</div>
</div>
</div>
blog.blade.php
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<h1>{{ $blog->title}}</h1>
<img src="{{$blog->image }}">
<p>{{ $blog->body }}</p>
#endsection
web.php:
Route::get('/', 'MainController#index')->name('index');
Route::get('/categories', 'MainController#categories')->name('categories');
Route::get('/about', 'MainController#aboutus')->name('about');
Route::get('/contact-us', 'ContactUSController#contactUS')->name('contact-us');
Route::post('contactus', ['as'=>'contactus.store','uses'=>'ContactUSController#contactSaveData']);
Route::get('/contacts', 'MainController#contacts')->name('contacts');
Route::get('/blogs', 'MainController#blogs')->name('blogs');
Route::get('/blog/{id}', 'MainController#blog')->name('blog');
Route::get('/intership', 'MainController#intership')->name('intership');
Route::get('/{category}', 'MainController#category')->name('category');
Route::get('/{category}/{product}/{skus}', 'MainController#sku')->name('sku');
Route::post('subscription/{skus}', 'MainController#subscribe')->name('subscription');
What's is the error that it shown?
I suggest use "compact" in this line code
return view('blogs',['blogs' => Blogs::all(),]);
Something like this
public function blogs()
{
$blogs = Blogs::all();
return view('blogs',compact('blogs'));
}
Try this
public function blog($id)
{
$blog = Blogs::findOrFail($id)
return view('blog', compact('blog'));
}

Q: how do I show, update, edit user profile by username in Laravel?

I have a custom authentication guard Applicant and already have created ApplicantsProfile model.
Problem 1 :
so far i am stuck with the show method, it is working but whatever ID i passed to the route it renders the view .
Problem 2 :
I want to access each applicant profile by it's ID
Problem 3 :
How do edit and update the authenticated user profile?
Profile Model
class ApplicantsProfile extends Model
{
protected $fillable = [
'job_title', 'applicant_id',
];
public function applicant()
{
return $this->belongsTo(Applicant::class);
}
}
Profile Controller
class ApplicantsProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth:applicant');
}
public function show($id)
{
$profile = ApplicantsProfile::find($id);
return view('applicant.profile', compact('profile'));
}
}
Profile View
#extends('layouts.auth')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card">
<div class="card-header">{{Auth::guard('applicant')->user()->name}}</div>
<div class="card-body">
{{Auth::guard('applicant')->user()->profile->job_title}}
</div>
</div>
</div>
</div>
</div>
#endsection
Route
Route::get('/account/{profile}', 'ApplicantsProfileController#show');
If I go to route: account/1 or account/2 it renders the view of the authenticated user.
in your profile view, you use Auth so it's normal to show these results.
you have to modify your view blade to something like :
#extends('layouts.layout')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card">
<div class="card-header">
{{ $applicantsProfile['name'] }}
</div>
<!-- complete your code like so -->
in your controller you have to do something like:
public function show(ApplicantsProfile $applicantsProfile)
{
return view('applicant.profile', compact('applicantsProfile'));
}
and in your web.php route:
Route::get('/account/{applicantsProfile}', 'ApplicantsProfileController#show');

question mark in the url with laravel

i'm learning laravel for now , i'm trying to build a crud application how i got the url with a question mark how i can remove it from the url
the url that i got is like ..../blogs?1
here is the view
#extends ('layouts.app')
#section('content')
<div class="row">
#foreach($blogs as $blog)
<div class="col-md-6">
<div class="card">
<div class="card-header">
{{$blog -> title}}
</div>
<div class="card-body">
{{$blog->content}}
</div>
</div>
</div>
</div>
#endforeach
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
how can i fix this , thank you in advance
Because the second argument in route('blogs_path', $blog->id) is parameter.
try this:
Routes:
Route::name('blogs_path')->get('/blogs/{id}/','BlogController#index');
Controller:
public function index(Request $request, $id)
{
...
}
You made a mistake in the routing of the template Blade.
{{ route('blogs_path1', ['id' => $blog->id]) }}

Output to same view from different controllers

Is it possible to output to the same view from multiple controllers. I have one view called 'dashboard'. I have two variables: $users and $friends. I want to send data to these variables from different controllers. Do I need to add two controllers to the same route?
The view:
<div class="panel friendlist" id="friendlist">
<div class="panel-heading"><h3 class="panel-title">Result List</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($friends as $friend)
<li class="list-group-item">{{ $friend->username }}
</li>
#endforeach
</ul>
</div>
</div>
</section>
<section class="row posts">
<div class="col-md-6 col-md-3-offset">
<header><h3>other posts</h3></header>
#foreach($posts as $post)
<article class="post">
<p>{{ $post->content }}</p>
<div class="info">Posted by {{ $post->user->username }} on {{ $post->created_at }}</div>
The post controller:
public function getDashboard()
{
$posts = Post::orderBy('created_at','desc')->get();
return view('dashboard',['posts' => $posts]);
}
The friend controller:
public function getFriends()
{
$friends = Auth::user()->friends()->where('status','accepted')->get();
return view('dashboard',['friends' => $friends]);
}
Current route:
Route::get('/dashboard',[
'uses' => 'PostController#getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
#Amartya Barua, you can use view composer to share some variables to multiple views https://laravel.com/docs/5.8/views, Or you can create BaseService, and write reusable getters and inject BaseService to your controller, in this way you can able to access required getters to your Controller, if any question comment my answer
Simple thing you can do is
First create one Model and in this model create two functions for this two variable values.
In Controller where you want to use dashboard as view add model to your controller and simply pass call function of that model and pass this function values to view.

Variable doesn't seem to be passing from controller to to view

I'm trying to pass a variable from a controller route to a view so i can display data relating to new assoicate model. It's working for other contrllers, but i can't figure out why it's not here.
Controller
#Index - Not passing down
public function index(){
$Advert = new PropertyAdvert();
return view('pages/Advert/index', compact('Advert'));
}
#Show - Is working, this is just an example.
public function show($id){
$user = Auth::user();
$Advert = PropertyAdvert::where('id', $id)->first();
return view('pages/Advert/show', compact('Advert', 'user'));
}
This is the route
Route::get('/property', 'AdvertisementController#index');
Route::get('/advertise', 'AdvertisementController#create')->middleware('auth');
Route::post('/createadvert', 'AdvertisementController#store');
Route::get('/property/{id}', 'AdvertisementController#show');
This is the template i'm trying to pass down to show.blade.php
<div class="row text-center" style="display:flex; flex-wrap:wrap">
<div class="col-lg-12">
<h3>Our most recent properties</h3>
#foreach ($Advert as $property)
<div class="col-md-3 col-sm-6">
<div class="thumbnail">
<img src="data:image/gif;base64,{{ $property->photo }}">
<div class="caption">
<h4>{{$property->address .', '. $property->town .', '. $property->county}}</h4>
</div>
<p>
More Info!
</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
Try this:
public function index(){
$Advert = PropertyAdvert::get();
return view('pages/Advert/index', compact('Advert'));
}
You need to actually access the data for that model.

Categories