view extension in laravel - laravel

Can you please tell why page in not getting inherited
File Structure:
Views> backEnd>masterPage.blade.php
<body>
<header class="main-header">
</header>
#yield('content')
<footer class="main-footer">
</footer>
File Structure:
Views> backEnd>usercontrol>home.blade.php
#extends('backEnd.masterPage')
#section('content')
<div class="content-wrapper">
</div>
#stop
at current i am not using Controller,just using routes.php
Route::get('home', function () {
return view('backEnd.masterPage');
});

Firstly, replace #endsection with #stop. The endsection tag depreciated in Laravel 4.
Then in your routes.php
Route::get('home', function () { return view('backEnd.usercontrol.home'); })

There is no need to access through controller there are some debugging tips that might help you
1 clear you cache by this php artisan cache:clear
2 dump-auto load your file by composer dump-autoload --optimize
as such there is no mistake found in your code it should work after optimizing
noted:
please change your route to child view
Route::get('home', function () {
return view('backEnd.usercontrol.home');
});

Related

The #edit Route is not working... showing 404 not found

the #edit route is not creating. everything is ok but showing 404 not found
the problem is on the last route. I ran the php artisan route:list code but not showing any route named /profile/{user}/edit
the web.php code ->>>>>
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfilesController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/{post}','App\Http\Controllers\PostsController#show');
Route::get('/p/create','App\Http\Controllers\PostsController#create');
Route::post('/p','App\Http\Controllers\PostsController#store');
Route::get('/profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('profile.show');
Route::get('/profile/{user}/edit','ProfilesController#edit')->name('profile.edit');
the profiles controller code ->>>>>
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function edit(User $user)
{
return view('profiles.edit', compact('user'));
}
public function index(User $user)
{
return view('profiles.index', compact('user'));
}
}
the index.blade.php code --->>>>>>>>>>>>>>>
#extends('layouts.app')
#section('content')
<div class="container">
<div><h1>here is a big design</h1></div>
<br>
<div class="d-flex justify-content-between align-items-baseline">
<h1>{{ $user->username}}</h1>
Add New Post
</div>
Edit Profile
<div class="pr-5"><strong>{{$user->posts->count()}}</strong> posts</div>
<div class="pr-5"><strong></strong> followers</div>
<div class="pr-5"><strong></strong> following</div>
<br>
<div><h1>{{ $user->profile->title}}</h1></div>
<br>
<div><h1>{{ $user->profile->description}}</h1></div>
<br>
<div><h1>{{ $user->profile->url ?? 'N/A'}}</h1></div>
<h1>Posts</h1>
<hr>
<div class="row pt-5">
#foreach($user->posts as $post)
<div class="col-4 pb-4">
<a href="/p/{{$post->id}}">
<img src="/storage/{{$post->image}}" class="w-100">
</a>
</div>
#endforeach
</div>
</div>
#endsection
Change your route to
Route::get('/profile/{user}/edit',[ProfilesController::class, 'edit'])->name('profile.edit');
Your way 'ProfilesController#edit' doesn't take use App\Http\Controllers\ProfilesController; into account.
Offtopic suggestion: since you already named your routes I suggest you use the named routes in your blade files instead of hardcoding them:
Edit Profile
instead of
Edit Profile
This way, should you decide to change a URL some time later, you only need to change it in your web.php file once, not all of your hardcoded occurences

Ask about sub view in laravel blade template

I'm want to ask about sub view in laravel blade the specific situation is as follows.
a view of pages
I want when click to component 1(2), The contents of the child blade file in #section('sub-view') will be rendered in the #yield('sub-view'). and this is the code of the parent blade file.
parent blade file
#extends('layouts.default')
#section('content')
<div class="row">
<div class="col-4">
<ul class="list-unstyled">
<li>
Component 1
</li>
<li>
Component 2
</li>
</ul>
</div>
<div class="col-8">
#yield('sub-view')
</div>
</div>
#endsection
And the code of child blade file.
child blade file
#section('sub-view')
<p>This is component 1</p>
#endsection
and my route file
Route file
Route::group(['prefix' => 'projects'], function () {
Route::get('', function () {
return View::make('pages.project.projects');
});
Route::get('comp1', function () {
return view('pages.project.comp1');
});
Route::get('comp2', function () {
return view('pages.project.comp2');
});
});
Can someone just help me solve this problem?
Thank
I think you need to add an
#extends('parent') // or whatever your parent view is called
to your child view file.
For more info:
https://laravel.com/docs/5.7/blade#template-inheritance

Laravel can't find stored file

I'm trying to have a video embedded in a view, but I'm receiving a 404, and I'm not entirely sure why. I created a new laravel project and then did php artisan storage:link. This site doesn't need to use file uploads so I just stuck the file in the storage directory:
storage/app/public/product1/courses/announcements/Manager_Creating_an_Announcement.mp4
My migration file:
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('title')->nullable($value = true);
$table->string('certifcation')->nullable($value = true);
$table->string('video')->nullable($value = true);
});
}
Where video is the path
My CourseCountroller
<?php
namespace App\Http\Controllers;
use App\Course;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CourseController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index($id)
{
$course = Course::findOrFail($id);
//dd(Storage::allFiles('public'));
//this returns an array with: 0 => "public/product1/courses/announcement/Manager_Creating_an_Announcement.mp4"
return view('course.index', compact('course', $course));
}
The view that the user clicks on to go to this course looks like this:
Manager Creating an Announcement
The route in web.php looks like this:
Route::get('/product/course/{course}', 'CourseController#index');
And the course view looks like this:
#extends('layouts.app')
#section('content')
#php
#endphp
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Welcome to your course: {{$course->title}}</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<h3>Watch the video first!</h3>
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="{{asset($course->video)}}"></iframe>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
In tinker, the course looks like this:
>>> Course::all();
[!] Aliasing 'Course' to 'App\Course' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#2921
all: [
App\Course {#2922
id: 1,
created_at: null,
updated_at: null,
title: "Manager creating an Announcement",
certifcation: "Announcements",
video: "public/csm/courses/announcement/Manager_Creating_an_Announcement.mp4",
},
],
}
The error is a 404 error, page not found. When I inspect the element, I see:
<iframe src="http://127.0.0.1:8000/public/product1/courses/announcement/Manager_Creating_an_Announcement.mp4" class="embed-responsive-item"></iframe>
I also tried it with the video tag, instead of embedding it, but no go. Any help would be much appreciated!
If it is in storage, you ran php artisan storage:link, you should be able to access it like this:
"/storage/product1/courses/announcements/Manager_Creating_an_Announcement.mp4"
The Public Disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
Of course, once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:
echo asset('storage/file.txt');
https://laravel.com/docs/5.7/filesystem#the-public-disk

Undefined variable: names

I am facing difficulties pushing data from the Controller to the View. Below is my code script.
I created my controller ListController using artisan
php artisan make:controller ListController
ListController - show method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ListController extends Controller
{
public function listNames()
{
$names = array(
'Daenerys Targaryen',
'Jon Snow',
'Arya Stark',
'Melisandre',
'Khal Drogo'
);
return view('welcome', ['names' => $names]);
}
}
Created a view welcome.blade.php (which is default)
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">API DOCUMENTATION</div>
<div class="panel-body">
#foreach ($names as $n)
<p>{{$n}}</p>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Then, added the appropriate route for it (routes.php)
<?php
Route::get('/', 'ListController#listNames');
When opening localhost:8000 after running php artisan serve, it throws an Exception
ErrorException in 56f6d37e6c39b528e3f5a170141b734befe0f7a2.php line 14:
Undefined variable: names (View: /Applications/MAMP/.../views/welcome.blade.php)
So far I have tried the following:
In the Controller:
return view('welcome', compact('names')); --> DOESN'T WORK
return view('welcome', $names); --> DOESN'T WORK
return view('welcome')->with($names); --> DOESN'T WORK
return view('welcome')->with('names', $names); --> DOESN'T WORK
Hard coding the array in the view and assigning it a variable works
<?php $names = array('John Snow', 'Arya Stark');?>
<?php foreach ($names as $n):?>
<tr>
<td><?php echo $n;?></td>
</tr>
<?php endforeach;?>
I can't seem to detect the problem. Any help is appreciated.
You should pass variables using an array:
return view('welcome', ['names' => $names]);
Or just:
return view('welcome', compact('names'));
To check it do this in the view:
{{ dd($names) }}
First, pass your variables as Alexey said,
return view('welcome', ['names' => $names]);
And then, on the view, do
#foreach ($names as $n)
<p>This is name {{ $n }}</p>
#endforeach
Documentation here: https://laravel.com/docs/master/blade#displaying-data
Managed to resolve the issue.
After spending so much time looking for the bug, I tried to check if laravel version could have something to do with this. The routes.php doesn't work in Laravel v5.3+ and I was using Laravel v5.4.x.
The routes are available in a directory called routes and so I pasted the code from routes.php to routes/web.php and it worked.
<?php
Route::get('/', 'ListController#listNames');

Laravel: Pages not loading properly

This might look useless question, but I'm unable to understand the problem here.
The problems are:
1. When I load the page, first this loads:
And then, after uploading the file:
this page loads:
2. yield('content') is not working. Although #include is working fine.
3. I made master.blade.php, to load the first page of the website. Before it, I was working on the welcome page.But, then changed the work of welcome with the master blade, but when the page gets loaded, it doesn't show anything.
Routes:
Route::get('/',function() {
return view('welcome');
});
Route::group(['middleware' => ['web']], function(){
Route::get('upload',function(){
return view('pages.upload');
});
Route::post('/upload','UploadController#upload');
});
views/pages/upload:
#extends('welcome')
#section('content')
<h2>Upload Files Here</h2>
{!! Form::open(array('url' => '/upload','files'=>true))!!}
{!! Form::file('file') !!}
{!! Form::token() !!}
{!! Form::submit('Upload') !!}
{!! Form::close() !!}
#endsection
views/welcome:
<!DOCTYPE html>
<html lang="en">
<head>
#include('partials._head')
</head>
<body>
#include('partials._nav')
<div class="container">
#include('partials._messages')
#yield('content')
#include('partials._footer')
</div>
#include('partials._javascript')
UploadController/Controllers:
if($request->hasFile('file')){
$file = $request ->file('file');
$fileName = $file->getClientOriginalName();
$destinationPath = config('app.fileDesinationPath').'/'.$fileName;
$uploads = Storage::put($destinationPath,file_get_contents($file- >getRealPath()));
}
return redirect()->to('/upload');
When it's working fine when I used include, to include the upload page in welcome, but the result is like shown in the pictures.Also, When I tried to use the master blade, and exclude the use of the welcome page, by erasing the content of the welcome page, the blank page gets loaded.
What changes do I have to make to make use of master instead of the welcome page? Why are two pages getting loaded? also, why isn't yield('content') not working?
Also, the file which I'm uploading is not getting saved in the public/uploads folder.
Please can anyone help?

Resources