Laravel 4 - Deleting a record from the database - laravel

Once again, i am stumped.
I have a table of letters, each with the following link attached
Delete {{ $letter->lettername }}
From the docs i can see i have to run the following from my routes.php
$letter = Letter::find(1);
$letter->delete();
My question is, what do i have to enter in the href to pass the letters ID onto the routes file, and then how do i pass that to the find() parameter?
Do i do something like this
Delete {{ $letter->lettername }}
If so, how do i put that ID into the find paramter.
Im confused.
Any help would be greatly appreciated
Cheers
My Routes file is as follows:
Route::get('letters', array(
'before' => 'auth|userdetail',
function()
{
// Grab the letters, if any, for this user
$letters = Letters::forUser(Auth::user())->get();
$data = [
'letters' => $letters
];
return View::make('letters', $data);
}
));

You could do it like this
Delete {{ $letter->lettername }}
But it would be better if you have a named route
Delete {{ $letter->lettername }}
Then in your routes
Route::get('letters/delete/{id}', array('as' => 'letters.delete', 'before' => 'auth|userdetail', function($id)
{
echo ("You want to delete letter id: " . $id . " from the system");
// Put your delete code here
}

Related

Laravel parameters without explicitly specifying

I want to generate the page url without $name, something like this
mysite.app/statestring
BUT with the route get('/action/{name}', ...) i can get only this
mysite.app/statestring/somename
If i changing route to Route::get('/action', ...) it doesnt work, the error is "Missing argument 1"
My web.php
Route::get('/action/{name}', [
'uses' => 'DoActionController#getAction',
'as' => 'returnAction',
]);
My Controller action
public function ($name)
{
return view('returnaction', ['name'=>$name]);
}
My home page
<body>
#foreach ($yourActions as $yourAction)
<li>
{{ $yourAction->name }}
</li>
#endforeach
You'll need to make the $name argument optional.
First, declare it as optional in the controller:
public function ($name = null)
That will get rid of the error your getting about missing argument. Next, make the name parameter optional in your route with:
Route::get('/action/{name?}', ...);

Laravel5.2 delete doesn't work

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,
What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

Laravel explain me how working query

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

Add prefix to all routes, links and urls

Is it possible to add some prefix to all routes, urls and links in the whole application at one place including blades?
For example I have route
Route::get('/', 'HomeController#showWelcome');
but instead of it I want to have
Route::get($prefix . '/', 'HomeController#showWelcome');
and in blade
{{ HTML::style('css/bootstrap.min.css') }}
to
{{ HTML::style($prefix . 'css/bootstrap.min.css') }}
I tried
Route::group(array('prefix' => $prefix), function() {});
but it did not apply prefix to links in blades.
I found simple solution. I created .env.php folder inside project which looks like
return array(
'ROUTES_PREFIX' => 'prefix',
);
then added this code to all routes
Route::group(array('prefix' => $_ENV['ROUTES_PREFIX']), function() {
// routes here
});
and created custom macro for style, script and image. For example my macro for style looks like this
HTML::macro('extendedStyle', function($url, $attributes = array(), $secure = null) {
$prefix = $_ENV['ROUTES_PREFIX'] == '' ? '' : $_ENV['ROUTES_PREFIX'] . '/';
return HTML::style($prefix . $url, $attributes, $secure);
});
I hope, someone will find this useful.

How to get value of Input object in a Route Filter?

I've been following the Laravel guides in their website, but there's one thing that it's not clear to me. In the tutorial they describe this filter:
Route::filter('old', function() {
if (Input::get('age') < 200) {
return Redirect::to('home');
}
});
What is the value of Input how does it get assigned?
The Route using that filter is not very descriptive:
Route::get('user', array('before' => 'old', function() {
return 'You are over 200 years old!';
}));
My guess is that Input represents the user, but how does it get assigned? He's not fetching the user from the DB, nor anything.
The ´Input´ (pretty much) always come from a form the user posted on your page. In this case you should have an input field named 'age' on it.
Take a look at requests and inputs: Laravel 4 Requests & Input
A form example for it, written in Laravel Blade:
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name') }}
{{ Form::text('age') }}
{{ Form::submit('Submit this form') }}
{{ Form::close() }}
As soon as you submit this form you will be able to:
echo Input::get('name');
echo Input::get('age');
or just everything you submited:
var_dump(Input::all());

Resources