Laravel : Passing value from view to controller - php

My View:
<table border="1" style="text-align: left;">
<td>
<b>Firstname</b>
</td>
<td>
<b>Actions</b>
</td>
#foreach($Newslist as $News)
<tr>
<td>
{{ $News->lastname }}
</td>
<td>
<button type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
#endforeach
</table>
My routes/web.php:
Route::Post('NewsEdit/{{$News->id}}/EditForm','NewsControllere#EditForm');
My Controller.php:
<?php
namespace App\Http\Controllers;
use App\Newsmodel;
use Illuminate\Http\Request;
class NewsControllere extends Controller
{
public function EditForm($NewsId)
{ dd(request()->all());
echo "deepakkeynes";exit();
//$Newsmodel = Newsmodel::find($NewsId);
//return view('/News')->with('News',$Newsmodel);
}
}
While clicking the edit button in the view, the following result is obtained:
Result: 404 page
Expected Result: The Id of the edit button along with the echo value!
Can anyone help me out? I am a newbie in laravel..!

Have a look at this LaraCast tutorial on Route Model Binding. This explains the underlying documentation well.
Essentially:
routes/web.php:
Route::get('NewsEdit/{news}/EditForm','NewsControllere#EditForm');
newsController.php:
public function EditForm(Newsmodel $news)
{
return view('/News')->with('News',$news);
}

Related

Laravel route problem redirects 404 error

I have a problem, I made a support system, and when I want to enter the page where I watch the ticket, I will get 404 not found. Basically the route is that of the id in the database.
Routes:
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
Controller:
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate() {
$tickets = Ticket::latest()->get();
return view('updateTicket', compact('tickets'));
}
View:
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<form>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</form>
</tr>
#endforeach
</tbody>
I really don't understand the problem, a way to solve and did not receive error 404? Btw, I read the other topics and I couldn't find a solution.
The link you are setting in the href attribute is only the ticket id, but it should be, for example, "/viewTickets/1"
Try to put this:
href="{{route('updateTicket', ['ticket' => $ticket->id])}}"
In your route :
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
In your Controller :
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate(Request $request, $ticket) {
$tickets = Ticket::find($ticket);
return view('updateTicket', compact('tickets'));
}
In your Blade :
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</tr>
#endforeach
</tbody>

how can i fix "Trying to get property of non-object"

i have a favorite list in my website for the users and they can add their favorite house to the wishlist
it goes well but he can not see the wishlist page and an error comes like this:
Trying to get property 'image' of non-object
it's my relations
class Home extends Model
{
protected $guarded = [];
public function favorite()
{
return $this->hasMany(favorite::class,'house_id');
}
}
class favorite extends Model
{
protected $guarded = [];
public function house()
{
return $this->belongsTo(home::class);
}
}
my index function in controller:
public function index()
{
$favorite = favorite::where('user_id',auth()->user()->id)->get();
return view('favorite.index',compact('favorite'));
}
my index:
#foreach($favorite as $fav)
<tr>
<td>
<a href="property-detail.html"><img src="{{$fav->home->image}}" alt=""
width="100"></a>
</td>
<td>{{$fav->home->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->home->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
First thing that your favorite model doesn't have home relation your relation is house and when you want to get its value you can use optional helper function like this:
optional($fav->house)->title
You are trying to access a relationship with a wrong name. You defined the relationship with name house:
public function house(){
return $this->belongsTo(home::class);
}
So you need to use this name to access:
#foreach($favorite as $fav)
<tr>
<td>
<img src="{{$fav->house->image}}" alt="" width="100">
</td>
<td>{{$fav->house->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->house->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
But you will have a problem too if the house relation come null. To avoid you can use the solution proposed by #Mohammed Aktaa:
optional($fav->house)->title

Error While Fetching db using eloquent

I have Tried to fetch data from MySQL db using eloquent in my laravel application it shows Parse Error here my code :
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class design extends Model {
//Table Name
protected $table='designs';
//Primary key
public $primarykey='id';
// TimeStamp
public $timestamps=true;
}
Controller Page:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\design;
class designController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$design=design::all();
//$design = design::select('Design No','Design Name','Chain Weight/Length','status')->where('status','=','Active')->get();
return view('pages.design')->with('design',$design);
}
}
page for listing:
the error occurring while using for-each
#extends('layouts.layout')
#section('content')
<div class="main">
<div class="container col-md-12 col-sm-12 col-xs-12">
<h2 class="text-center">Designs</h2>
#if(count($design)>=1)
<h2>Done</h2>
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Design No</th>
<th>Design Name</th>
<th>Weight/Lenght</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($design as $design)
<tr>
<td>{{$design->Design No}}</td>
<td>{{$design->Design Name}}</td>
<td>{{$design->Chain Weight/Length}}</td>
<td>
<button type="button" class="btn btn-warning">Edit</button>
<button type="button" class="btn btn-danger">{{$design->status}}</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endif
</div>
</div>
#endsection
it doesn't allow space in column name it should be in lower cases as design_no , design_name in your DB
and then fetch as
#foreach($design as $new_designs)
<td>{{$new_designs->design_no }}</td>
// and so on
#endforeach
Hope it will work.
try this
{{$design->{'Design No'} }}
Yous should try this:
Controller Page
public function index()
{
//
$designs=design::all();
return view('pages.design',compact('designs'));
}
View Page
#foreach($designs as $design)
<tr>
<td>{{$design->Design_No}}</td>
<td>{{$design->Design_Name}}</td>
<td>{{$design->Chain_Weight/Length}}</td>
<td>
<button type="button" class="btn btn-warning">Edit</button>
<button type="button" class="btn btn-danger">{{$design->status}}</button>
</td>
</tr>
#endforeach

Why do I keep getting 'Undefined variable: charities' in Laravel 5.4?

I keep getting this undefined variable error. I am trying to display all the charities to the users, I am doing the exact same function for the 'charityowners' type of user and it is working for them.
Route:
Route::get('charities', 'HomeController#displayCharities');
Home Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use Image;
use App\Charities;
class HomeController extends Controller
{
public function displayCharities()
{
$charities = Charities::all();
return view('charities', ['charities' => $charities]);
}
}
View:
<table class="table">
<tr>
<td> <strong> Name </strong> </td>
<td> <strong> Image </strong> </td>
<td> <strong> Description </strong> </td>
<td> <strong> Action </strong> </td>
</tr>
#foreach($charities as $charity)
<tr>
<td> {{ $charity->name }} </td>
<td> Not Available </td>
<td> {{ $charity->description }} </td>
</tr>
#endforeach
</table>
The error I keep getting is:
ErrorException in d9249250b321fd33ae875d6ca2417a0420928492.php line 39:
Undefined variable: charities (View: C:\wamp64\www\EasyDonationNew\laravel\resources\views\charities.blade.php)
What am I missing??
You can try this
return view('charities')->with('charities', $charities);
It works now by adding:
#if(isset($ownedCharities))
Before the foreach loop.
Try initializing the variable $charities globally as an empty string or as null.
On the global scope a var is only seen if it is defined.

Fecthing the data from database in Laravel 5.1

Good day
I'm facing some errors while trying to fetch data from database
here are my codes
my table name is meeting and I used the formal documentation to write the codes
controllers.php
public function meetingdet()
{
$mretive = \DB::table('meeting')-> $name = $request->input('meetingname');}
view
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td><li>foreach ($name as $retrive) {
echo $retrive-> meeting;}
#endforeach
</li>
</td>
</tr>
</table>
but this what is showing for me
syntax error, unexpected 'endforeach' (T_ENDFOREACH) and whan I remove
endforeach it display for me the for each without fetching the data
By the way I'm working on laravel 5.1
Regards
You can try with the following sample code:
controllers.php
//include your model file
use App\model;
public function meetingdet()
{
$meetingData = model::getMeetingData();
$view = array(
'name'=>$meetingData
);
return view('test', compact('meetingData'));
}
Model.php
public static function getMeetingData()
{
$value=DB::table('meeting')->orderBy('meeting_id', 'asc')->get();
return $value;
}
view.blade.php
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td>
<ul>
<li>
#foreach ($meetingData as $retrive)
{{{ $retrive->meeting_id }}}
#endforeach
</li>
</ul>
</td>
</tr>
</table>
<li>
#foreach ($name as $retrive)
{{ $retrive->meeting }}
#endforeach
</li>
you didn't add # to foreach..

Categories