views/show.blade.php
#extends('layouts.app')
#section('content')
<h5>Showing Task {{ $task->title }}</h5>
<div class="jumbotron text-center">
<p>
<strong>Task Title:</strong> {{ $task->title }}<br>
<strong>Description:</strong> {{ $task->description }}
</p>
</div>
#endsection
Controllers/HomeController.php
public function show(Task $task)
{
return view('show', compact('task', $task));
}
routes/web.php
Route::get('show/{id}', 'HomeController#show')->name('show');
views/viewalltask.blade.php
<td>{{$data->title}}</td>
No error / No Record / instead of particulr record display blank page
You need to change the route configuration to:
Route::get('show/{task}', 'HomeController#show')->name('show');
This way Laravel's IOC container knows how to resolve/bind it.
Must match the variable name used in the method definition:
public function show(Task $task)
Related
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'));
}
I have a Many to Many relationship between Wallet & User Models:
User.php:
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
Wallet.php:
public function users()
{
return $this->belongsToMany(User::class,'user_wallet','wallet_id','user_id')->withPivot('balance');
}
And the pivot table user_wallet goes like this:
Now I need to get the name of wallets and the balance of that wallet in a table at Blade.
So at the Controller, I added this:
public function index($id)
{
// $id is the user id
$userWallet = Wallet::with("users")->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
}
And then at the Blade:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users()->balance }}
</div>
#empty
Empty
#endforelse
But I get this error:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance
So what's going wrong here? How can I properly get the balance at the pivot table?
You have to loop user
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
#if(isset($wallet->users)&&count((array)$wallet->users))
#foreach($wallet->users as $user)
{{$user->pivot->balance }}
#endforeach
#endif
</div>
#empty
Empty
#endforelse
Your query should be
$userWallet = Wallet::with(["users"=>function ($query)use($id){
$query->where('user_id',$id);
}])->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
That's because you use the relation's query builder object with users() but you need user property to get the collection:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->balance }}
</div>
#empty
Empty
#endforelse
But even the collection instance doesn't have a balance property as long as it is not a custom collection. You probably need to loop over the collection or use pluck to get the model's balance property:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->pluck('balance')->join(', ') }}
</div>
#empty
Empty
#endforelse
I have two tables, Companies and Projects. A company hasMany projects and a project belongsTo a company.
Company.php model
protected $fillable = [
'id', 'name', 'description'
];
public function projects()
{
return $this->hasMany('App/Project');
}
Project.php model
protected $fillable = [
'name', 'description', 'company_id', 'days'
];
public function company()
{
return $this->belongsTo('App/Company');
}
From my index.blade.php, I list the companies only and I have made them clickable so that when a user clicks on a company listed, they are taken to show.blade.php where the name of the company and the projects that belong to that company are displayed like so.
<div class="jumbotron">
<h1>{{ $company->name }}</h1>
<p class="lead">{{ $company->description }}</p>
</div>
<div class="row">
#foreach($company->projects as $project)
<div class="col-lg-4">
<h2>{{ $project->name }}</h2>
<p class="text-danger">{{ $project->description }}</p>
<p><a class="btn btn-primary" href="/projects/{{ $project->id }}" role="button">View Projects »</a></p>
</div>
#endforeach
</div>
Now am getting an undefined variable $project error. So I decided to declare variable in my show() function of the CompaniesController.php like so
public function show(Company $company)
{
$company = Company::find($company->id);
$projects = Company::find(1)->projects;
return view('companies.show', ['company' => $company, 'projects' => $projects]);
}
And access variable in show.blade.php like so
<div class="jumbotron">
<h1>{{ $company->name }}</h1>
<p class="lead">{{ $company->description }}</p>
</div>
<div class="row">
#foreach($projects as $project)
<div class="col-lg-4">
<h2>{{ $project->name }}</h2>
<p class="text-danger">{{ $project->description }}</p>
<p><a class="btn btn-primary" href="/projects/{{ $project->id }}" role="button">View Projects »</a></p>
</div>
#endforeach
</div>
Now am getting a Class 'App/Project' not found error when I access show.blade.php. I am having a challenge passing company projects to the view. Any help will be appreciated. Here are my routes;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('companies', 'CompaniesController');
Route::resource('projects', 'ProjectsController');
I would be hilarious if I am right....
In your models where defining relations replace App/Project with App\Project. Do the same for Company.... Replace "/" with "\".
You have to namespace Project class properly
Make sure file name is Project.php
Make sure inside Project.php namespace declaration is correct: namespace App;
Make sure class name inside Project.php is 'Project' : class Project extends Model { ...
Make sure you have imported it in controller. use App\Project
After all that done you will not get error:
Class 'App/Project' not found
You have correctly done passing variable in view but have a look here for another examples and methods passing about it:
https://laravel.com/docs/7.x/views
Hope this helps you
You're already using model binding. In your show method, you do not need to find. just return what you need
public function show(Company $company)
{
return view('companies.show', ['company' => $company];
}
In your view, you can then do:
#foreach($company->projects as $project)
...
#endforeach
So i was trying to display a record from a database with laravel, and i have defined the variable in #foreach statement but when i run it it shows ErrorException Undefined variable , although all variable already inside the foreach statement, am i missing a method function in my controller?
this is the view
welcome.blade.php
<div class="blog-item">
<div class="blog-text text-box text-white">
#foreach ($guestbooks as $guestbook)
<div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $guestbooks->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
<!-- Blog item -->
<div class="blog-item">
<div class="blog-text text-box text-white">
<div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $guestbook->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
<!-- Blog item -->
<div class="blog-item">
<div class="blog-text text-box text-white">
<div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $guestbook->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
#endforeach
</div>
</div>
this is the controller
GuestbookController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Guestbook;
class GuestbookController extends Controller
{
public function index()
{
$guestbooks = Guestbook::get();
return view('post.post_textarea',[
'guestbooks' => $guestbooks
]);
}
public function store(Request $request)
{
Guestbook::create([
'name' => $request->name,
'message' => $request->message
]);
return redirect()->back();
}
}
and this is the routes
Route::get('/posting','GuestbookController#index')->name('guestbook');
Route::post('/posting','GuestbookController#store')->name('guestbook.store');
Your GuestbookController function index() is returning a view called post.post_textarea and passing your $guestbook variable to that view, while you are trying to get that variable in your welcome.blade.php.
Change your index function to return welcome view like this:
public function index()
{
$guestbooks = Guestbook::get();
return view('welcome',[
'guestbooks' => $guestbooks
]);
}
i figured the problem was that there are two routes routing into the same url as Route::get('/posting','GuestbookController#index')->name('guestbook'); so i delete the other one and it works thanks about that typo tough #Denis Ćerić
I have a problem with Larave's framework program with deleting by CRUDS method DELETE.
My route method is:
Route::delete('cats/{cat}/delete', function(Furbook\Cat $cat){
$cat->delete(); return redirect('cats.index')
->withSuccess('Cat
has been deleted.'); });
My view with delete url:
#extends('layouts.master')
#section('header')
Back to the overview
<h2>
{{ $cat->name }}
</h2>
<a href="{{ url('cats/'.$cat->id.'/edit') }}">
<span class = "glyphicon glyphicon-edit"></span>
Edit
</a>
<a href ="{{ url('cats/'.$cat->id.'/delete') }}">
<span class ="glyphicon glyphicon-trash"></span>
Delete
</a>
<p>Last edited: {{ $cat->updated_at }}</p>
#endsection
#section('content')
<p>Date of Birth: {{ $cat->date_of_birth }} </p>
<p>
#if ($cat->breed)
Breed:
{{ url('cats/breeds/'.$cat->breed->name) }}
#endif
</p>
#endsection
My Cat model:
<?php
namespace Furbook;
use Illuminate\Database\Eloquent\Model;
class Cat extends Model {
// We specified the fields that are fillable in the Cat model beforehand
protected $fillable = ['name','date_of_birth','breed_id'];
// informacja o tym, żeby nie uaktualniać update_at w tabeli kotów
public $timestamps = false;
public function breed(){
return $this->belongsTo('Furbook\Breed');
}
}
?>
When I'm clicking on delete link, there is an error like this:
MethodNotAllowedHttpException in RouteCollection.php line 233:
I don't know what's wrong. Could you somoene help me with solving problem?
Could someone help me with this problem?
I would be very grateful, greetings.
This is to do with the Request you are making. You must either create form with the delete method like so
<form action="{{ url('cats/'.$cat->id.'/delete') }}" method="DELETE">
<button class ="glyphicon glyphicon-trash">Delete</button>
</form>
OR change your route to a get
Route::get('cats/{cat}/delete', function(Furbook\Cat $cat){
$cat->delete();
return redirect('cats.index')->withSuccess('Cat has been deleted.');
});
If you go the form route don't for get to add the {{ csrf_field() }}
https://laravel.com/docs/5.4/csrf
Using Route::delete(), you cannot place it in an anchor. Make a form with DELETE method.
{!! Form::model($cat, ['method' => 'DELETE', 'url' => 'cats/'.$cat->id.'/delete']) !!}
<button type="submit">Delete</a>
{!! Form::close() !!}