Missing required parameter in laravel - php

I've been trying to get this fixed for days now. I don't know what i'm doing wrong. I'm trying to edit a post in the dashboard.
My route looks like this
Route::get('/edit-post/post_id/{post_id}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostedit',
]);
for that route i have a controller
public function showPostEdit(Request $request, Post $post, $post_id){
$posts= $post->where('post_id',$post_id)->get();
return view('pages.dashboard.user.edit-post',compact('posts',auth()-
>user()->id));
}
My blade syntax looks like this
#if(Auth::check())
#if(auth()->user()->id === $posts->user_id)
<li class="list-inline-item"><span class="fa fa-edit"></span></li>
<li class="list-inline-item"><span class="fa fa-trash"></span></li>
#endif
#endif

Solution 1: primary key should be id instead of post_id in post table.
Route::get('/edit-post/{post}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostEdit',
]);
public function showPostEdit(Post $post){
return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
}
Solution 2:
Route::get('/edit-post/{postId}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostEdit',
]);
public function showPostEdit($postId){
$post= (new Post())->where('post_id',$postId)->get();
return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
}

Related

Want to add {{$value['category']}} in {{$value['category']}} in Laravel

I want to set {{$value['category']}} in {{route('')}}
This is my code:
#foreach($details as $value)
{{$value['category']}}
#endforeach
Controller function:
public function viewhome(Request $req){
$product = product::all()->unique('category');
return view('homepage',['details'=> $product]);}
Route:
Route::get('/homepage/db_val', 'HomeController#db_val')->name('db_val');
How to declare href properly. And what will be the route. Thank you.
1) warn : you call db_val function in HomeController but you show viewhome method in your question.
Route::get('/homepage/db_val', 'HomeController#<b>db_val</b>')->name('db_val');
if you want use method viewhome :
Route::get('/homepage/db_val', 'HomeController#<b>viewhome</b>')->name('db_val');
2) route is used with named route
you have a route named 'db_val' --> Route::.... ->name('db_val');
so it must be used like that ,
<a href='{{route('db_val')}}
3) in your case , assuming $value in foreach is an array with a 'category' index inside
you can use url instead route
#foreach($details as $value)
<a href="{{url('/your_url')}}/{{$value['category']}}">
link to {{$value['category']}}
</a>
#endforeach
4) but blade spirit is
route.php
Route::get('/showcategory/{id}','HomeController#showcategorie')->name('showcat');
view
#foreach($details as $value)
<a href="{{route('showcat', $value['category'])}}">
#endforeach
it means : you have one named route /showcategory with a parameter /{id}
Route:
Route::get('/homepage/db_val_1', 'HomeController#db_val_1')->name('db_val_1');
Route::get('/homepage/db_val_2', 'HomeController#db_val_2')->name('db_val_2');
Route::get('/homepage/db_val_3', 'HomeController#db_val_3')->name('db_val_3');
...
Controller Function :
public function db_val_1(Request $req){
$all = product::all()->where('category', 'db_val_1');
return view('homepage', ['details'=> $all]);
}
public function db_val_2(Request $req){
$all = product::all()->where('category', 'db_val_2');
return view('homepage', ['details'=> $all]);
}
public function db_val_3(Request $req){
$all = product::all()->where('category', 'db_val_3');
return view('homepage', ['details'=> $all]);
}
...
View home: In route the value will be ($value->category) like this.
#foreach($details as $value)
{{$value['category']}}
#endforeach
I got the solution. Thank you for helping.

Pass data to view in Laravel

I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
#foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
#endforeach
</ul>
I use Laravel version 5.5
Make sure in your route file on routes/web.php you have a route to the index function like so:
Route::get('urlToYourView', 'YourController#index');

Faced the error when wanted to create pagination in Laravel

I wanted to create pagination in index page and as I read in Laravel documentation it is possible to use paginate method with orderBy method when I do that I faced error:"Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.". The code I am using:
public function index()
{
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal=$user->personalAccounting->orderBy('asc')->paginate(2);
$balance=$user->balance;
return view('personal.index', compact('personal','balance'));
}
#extends('layouts.app')
#section('content')
<h1>Домашняя Бухгалтерия</h1>
<br>
#if($balance==0)
<h2 class="text-center">Ваш Баланс: <span class="badge badge-primary">0.00</span></h2>
#elseif($balance<0)
<h3 class="text-center">Ваш Баланс: <span class="badge badge-danger">{{$balance}}</span></h3>
#else
<h3 class="text-center">Ваш Баланс: <span class="badge badge-success">+{{$balance}}</span></h3>
#endif
<br>
Создать
<br>
#if(count($personal)>0)
#foreach($personal as $pers)
<br>
<div class="card">
<h2><a href="/personal/{{$pers->id}}" >{{$pers->TypeOfAccounting}}</a></h2>
<h5 class="">Наименование:<span class="badge">{{$pers->Name}}</span></h5>
</div>
#endforeach
{{$personal->links()}}
#else
<p>Не найдено записей</p>
#endif
#stop
public function index()
{
$user = auth()->user(); // It's already the user model
$personal = $user->personalAccounting()->orderBy('YOUR_COLUMN')->paginate(2);
$balance = $user->balance;
return view('personal.index', compact('personal','balance'));
}
Notice personalAccounting() instead of personalAccounting The reason is, when you call the attribute without (), Laravel will load all the data from your DB in a collection. Every modification you do after, will be done with PHP.
When you request with the method using (), you're modifying the QueryBuilder which mean the orderBy and the pagination will be executed by MySQL which will also increase the performance and reduce the memory usage.
Give a column name before asc
public function index()
{
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal=$user->personalAccounting->orderBy('**your_column_name**','asc')->paginate(2);
$balance=$user->balance;
return view('personal.index', compact('personal','balance'));
}
Try to use latest().
public function index()
{
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal=$user->personalAccounting->latest('***Add column name***')->paginate(2);
$balance=$user->balance;
return view('personal.index', compact('personal','balance'));
}

Route [/forum?filter=me] not defined

i want to send filter request to show only my discussions
it's my route
Route::resource('/forum','ForumsController');
<div class="list-group-item">
My Discussions
</div>
its my ForumController
switch (request('filter'))
{
case 'me':
$discussions = Discussion::where('user_id',Auth::id())->paginate(3);
}
Found a solution to send link :)
Home
This make the route like below:-
http://localhost/forum/public/forum?filter=me
If you user Route::resource function, it has default route name .
Route web.php
Route::resource('/forum','ForumsController');
View.php
<div class="list-group-item">
My Discussions
</div>
Controller.php
public function index(Request $request){
switch ($request->filter){
case 'me':
$discussions = Discussion::where('user_id', Auth::id())->paginate(3);
}
return view('View.php', compact('discussions'));
}

Defining multiple variable laravel for use in if statement

I would like to be able to display different content depending on which variable is called in the url.Ignoring the fact $slug is slug, let's say it's a post id instead so if $id is active then show just the post else if $month is active show all posts from that month elseif $month and $id = null show all. That's what I'm trying to achive
Routes.php
Route::get('blog/{slug}', ['as' => 'blog.slug', 'uses' => 'BlogController#getSlug']);
Route::get('blog/{month}', ['as' => 'blog.slug', 'uses' => 'BlogController#getSlug']);
BlogController.php
<?php
class BlogController extends BaseController {
public function BlogIndex()
{
//get the posts from the database by asking the Active Record for "all"
$blogs = Blog::all();
$blogs = DB::table('blogs')->paginate(3);
// and create a view which we return - note dot syntax to go into folder
return View::make('pages.blog', array('blogs' => $blogs));
}
public function ShowbySlug($slug)
{
$blogs = Blog::where('slug', '=', $slug)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('slug', $slug)
->with('blogs', $blogs);
}
public function ShowbyMonth($month)
{
$blogs = Blog::where('month', '=', $month)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('month', $month)
->with('blogs', $blogs);
}
}
blog.blade.php
#foreach ($blogs as $blog)
#if(isset($blogs))
<div class="blog-outer-wrap">
<img src="images/blog/{{ $blog->img}}">
<div class="blog-header">{{ $blog->header }}</div>
<div class="blog-text">{{ $blog->content }}</div>
<a href="{{ URL::route('blog.slug', [$blog->slug]) }}">
</div>
#elseif(isset($slug))
<div class="blog-outer-wrap">
<img src="images/blog/{{ $blog->img}}">
<div class="blog-header">{{ $blog->header }}</div>
<div class="blog-text">{{ $blog->content }}</div>
</div>
#endif
#endforeach
You really can't do that, because you just declared the same route twice. When you say a route is blog/{slug}, slug is only a placeholder. It's exactly the same¹ as blog/{month}. All it says is: "I expect blog/ followed by anything." It doesn't matter if you call your anything slug or month. That is, the parameters do not add up.
What you can do is, if you consider slug is always a string and month is always a number (or the name of the month²), is to apply where clauses to your route parameters, like so:
// Route for all blog posts
Route::get('blog', 'BlogController#showAll');
// Route for all blog posts in a month; only numbers as parameters
Route::get('blog/{month}', 'BlogController#showByMonth')
->where('month', '[0-9]+');
// Route for all blog posts by title; anything else as parameters
Route::get('blog/{slang}', 'BlogController#showBySlang');
And on your controller, define three methods, one for each route:
public function showAll() {
$blogs = Blog::all();
return View::make('pages.blog')
->with('blogs', $blogs);
}
public function showByMonth($month) {
$blogs = Blog::where('month', $month)
->get();
return View::make('pages.blog')
->with('blogs', $blogs);
}
public function showBySlug($slug) {
$blogs = Blog::where('slug', $slug)
->get();
return View::make('pages.blog')
->with('blogs', $blogs);
}
¹ Unless you're using some more advanced routing features, but that's not really the point here.
² It is doable with the where clause, but it would be ugly if you wanted to consider all upper/lower case combinations. E.g: ([Jj][Aa][Nn][Uu][Aa][Rr][Yy]|[Ff][Ee][Bb][Rr][Uu][Aa][Rr][Yy]|...)

Categories