Laravel Destroy() is not removing any record from DB - php

Database Image
This is my controller's index method. When i run the code it does nothing.
public function index()
{
Customer::destroy(1);
return view('customer');
}
Model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model {
protected $guarded = [];
public $timestamps=false;
}

Try using delete instead of destroy like this:
Customer::findOrFail(1)->delete();
or you can try directly from database using eloquent query builder:
Customer::where('id', 1)->delete();

You can also write your code like this
public function getDeleteTag($id)
{
DB::table('tags')
->where('id','=',$id)
->delete();
return Response::json(['message' => 'Tag has been deleted successfully!']);
}

Related

How can I use laravel Auditor in controller

I used laravel Auditor in a model and it works very well as following:
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
class Contracts extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
protected $fillable=['condatereceived'];
public function user()
{
return $this->belongsTo(User::class);
}
}
But I want to used it in the controller as :
public function updatecomplated(Request $request, $id,Contracts $contract ,Auditor $auditor)
{
Contracts::where('id', $id)
->update(['complated' => 50, 'conuploadby' => Auth::id(),'constatus' =>'Need To Active' ]);
if ($audit = $auditor->execute($contract)) {
$auditor->prune($contract);
}
return redirect()->back();
}
The code in controller give me error:
Call to undefined method OwenIt\Auditing\Facades\Auditor::execute()
any ideas to use auditor in the controller, please.
try this package its easy with good documentation
simply add this to your table
$table->auditable();
and this to your model
namespace App;
use Yajra\Auditable\AuditableTrait;
class User extends Model
{
use AuditableTrait;
}
thats it now simply get your auditor by calling
$user->creator // for who create
and
$user->updater //for who update data
for more information click here for check trait
Hope this helps

Model binding in Laravel

I am trying to fetch data from database in Laravel using Model binding in Controller but it returns empty $task array instead
here is my routes.php :
<?php
Route::get('/tasks','TasksController#index');
Route::get('/tasks/{task}','TasksController#show');
and this the TasksController :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
class TasksController extends Controller
{
public function show(Task $task)
{
return $task;
}
}
Route::get('/tasks/{id}','TasksController#show');
in route model binding you can pass the id in the route and handle it in the show function
public function show($id)
{
$task = Task::find($id);
return $task;
}
This will directly return the task
Without seeing other code it's hard to answer however your function index() should look similar to this:
public function index(Task $task)
{
$tasks = $task->all();
return view('page', compact('tasks'));
}
Now, assuming you're gathering the ID's via a foreach loop your controller code should look like this:
public function show($id, Task $task)
{
$task = $task->find($id);
return view('somePage', compact('task'));
}
This will then pass $task back to a page of your choosing to display the specific task you are getting.
Your Routing is fine providing you're passing the:
Something
back within the blade.

How to get first row in Laravel relationship

How can i create a method in my model below that consist of first value of pages?
I want my cover method to get he first instance of pages.
Here's my model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $guarded = [];
public function pages()
{
return $this->hasMany('\App\Page');
}
public function cover()
{
//first page of pages here
}
}
Use the first() method on the relationship
public function cover()
{
return $this->pages()->first();
}

Laravel BadMethodCallExeption Query Builder post

I get this error message if I try to run my post on postman:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::posts()
My routes Looks like this:
Route::middleware('auth:api')->group(function ()
{
Route::get('posts', ['as' => 'posts', 'uses' => 'Api\PostController#index']);
}
And the Controller like this:
class PostController extends Controller
{
public function index()
{
$posts = Auth::client()->posts()->get();
dd($posts);
return response()->json(['data' => $posts], 200, [], JSON_NUMERIC_CHECK);
}
}
My Client model:
class Client extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, HasApiTokens, Notifiable;
protected $table = 'clients';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function posts()
{
return $this->hasMany(Post::class);
}
}
And my Post model:
namespace App;
use App\Client;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function client()
{
return $this->belongsTo(Client::class);
}
}
If I dump this: $posts = Auth::user()->get();
I get an Output, but I want to get the post Output.
You should define the relationship first.
<?php
...
class User extends Model
{
public function posts()
{
return $this->hasMany('App\Post');
}
}
Once it has been defined, you can access the collection of posts by accessing the posts property. Most important, you should retrieve a single row firstly, then access the collection of posts like this:
$client = Client::find(1);
$posts = $client->posts;
Or you can serve the relationship as query builders like this:
$posts = App\Client::find(1)->posts()->where('name', 'tests')->get();
Iff it still occur the error, You should check if Auth::client()return client model instance.
dd(Auth::Client());
I'm not sure how you've defined Auth::client(), but judging by some of the code there is a misconception of how models are instantiated, and what get() does.
get() returns a collection of results.
Auth::user() already returns a user instance, when you call Auth::user()->get(), you are sending a second query to the database and returning a list of users.
$user->posts()->get() has a more conventional/memoized way of accessing the dataset, `$user->posts.
I'd recommend checking out your Auth::client() method and make sure you're not accidentally returning a new query.

Laravel 5.3 - Blank Screen - Insert Query

I Newly Installed Laravel 5.3. I Create Model and Controller for Role table. When i send Insert Data to Role Controller. It will be display Blank Page. but print a posted will be display. Select Query will be working but insert query not working.
Controller :
<?php namespace App\Http\Controllers;
use Auth;
use DB;
use App\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('admin.userlist');
}
public function roleindex()
{
return view('admin.role');
}
public function saveRole(request $request)
{
$id=Auth::user()->id;
$role=new Role;
$role->role_title=$request->input('role');
$role->created_by=$id;
$role->created_date=date('Y-m-d H:i:s');
$role->save();
$request->session()->flash('alert-success','Role details Saved Successfully!');
return redirect('dddddd');
}
}
Change your saveRole function as follows.
public function saveRole(Request $request)
{ // Rest of your code
}
Use like this :
if this will not work then there will be other issue somewhere else.
public function saveRole(\Request $request)
{
$role=new Role;
$role->role_title=$request->get('role');
$role->created_by=$id;
$role->created_date=date('Y-m-d H:i:s');
$role->save();
$request->session()->flash('alert-success','Role details Saved Successfully!');
return redirect('to_some_url');
}

Categories