public function postMoviesEdit( MoviesEditRequest $request, $id){
$episode= episode::where('movies_id', $id)->get();
$episode->ep_quality = $request->txtQuality;
$episode->ep_url = $request->txtFull;
$episode->updated_at = new DateTime();
$episode->save();
I get that message: Method save does not exist.
I used the get () method instead of using first (). I want to get more than one result, so I used the get () method. Is there a way to fix it? Kindly help me so i can get more of a result
Related
I have a question that I cant solve at this moment.
I have and URL like this: https://www.example.com/schedule/2020-02-26
i put the date on the URL with the next function:
public function newdate($datevar)
{
$Dates = Dates::all();
return view('schedule', compact('Dates'), ['Dates' =>
$Dates]);
} //
In this example $datevar is equal to "2020-02-26"
My question is, How can I show the $datevar in my blade? Im trying with {{$datevar}} but I get an error.
To put the datevar on my function I use and other function as Request:
public function addnewdate(Request $request){
$datevar = $request->input('newdate');
return redirect()->to('schedule/'.$datevar);
}
Thanks for your help
The solution is to register the URL format in the web.php file. In that file, add this line:
Route::get('/schedule/{date}', [ControllerName::class, 'addnewdate']);
Now, whenever the the controller method is called, it'll pass the date as an argument to your addnewdate method, which you need to restructure like this:
public function addnewdate($date){
//Do sth with the date
}
guys i trying to filter my data from mysql with where clause but after put secound value laravel give me a blank result? If i try to filtered with first value example like this : http://localhost/transport/1 everything is good but if i try to set from destionation give me a blank result. example with fail : http://localhost/transport/1/Германия
Here is my Controller
class TransportController extends Controller
{
public function filtermethod($method){
$data['ads'] = db::table('ads')->where('method', $method)->get();
return view('transport', $data );
}
public function regionfrom($from){
$data['ads'] = db::table('ads')->where('from', $from)->get();
return view('transport', $data );
}
Here is my routes :
Route::get('transport/{method}', 'TransportController#filtermethod');
Route::get('transport/{method}/{from}', 'TransportController#regionfrom');
Your second route should be giving your controller 2 variables.
public function regionfrom($method, $from)
Is what your route your having problems with is calling, do the logic you like in there.
If you would like to filter twice, try this:
$data = DB::table('ads')-where('method', $method)->where('region', $region)->get();
I have variable sets all my views in AppServiceProvider when I use that variable in the controller it says:
Undefined variable
Code
AppServiceProvider.php
View::composer('*', function ($view) {
$ProductSettings = ProductSetting::all();
foreach($ProductSettings as $psetting){
$show_unavailable = $psetting->show_unavailable;
$show_quantities = $psetting->show_quantities;
$product_pagination = $psetting->pagination;
$max_short_desc = $psetting->max_short_desc;
$allow_delivery_time = $psetting->allow_delivery_time;
$addtocart_on_attribute = $psetting->addtocart_on_attribute;
$outofstock_lable = $psetting->outofstock_lable;
$product_orderby = $psetting->orderby;
}
$view->with('show_unavailable', $show_unavailable);
$view->with('show_quantities', $show_quantities);
$view->with('product_pagination', $product_pagination);
$view->with('max_short_desc', $max_short_desc);
$view->with('allow_delivery_time', $allow_delivery_time);
$view->with('addtocart_on_attribute', $addtocart_on_attribute);
$view->with('outofstock_lable', $outofstock_lable);
$view->with('product_orderby', $product_orderby);
});
Controller
//codes...
$products = Product::whereHas('categories', function ($query) use ($slug) {
$query->where('slug', $slug);
})->paginate($product_pagination);
//codes...
Based on my database values product_pagination is set to 12 which means my code is like: ->paginate(12);
Question
Any idea why I get undefined error?
Update
table
Based on the comments, I am going to assume this will work:
$products = Product::whereHas('categories', function ($query) use ($slug) {
$query->where('slug', $slug);
})->paginate(ProductSetting::first()->pagination);
Since you are only ever having one row in your product_settings table you can simply access the first row of that table, from there you can access any columns you need.
Edit 1
Based on the response, apparently the above is "too long".
The only other alternative I have for this question is the following:
App::singleton('product_settings', function () {
return ProductSetting::first();
});
Write this method within the AppServiceProvider (or any provider of your choice), within the boot method.
Then, this can then be used anywhere with the following code:
app('product_settings')->pagination;
// or, more simply:
app('product_settings')-><some column name>;
If you think that there is still too much code then you can register your original variables using the same format with the singleton method and just add the -><some column name> after the first method call.
Edit 2
The above edit doesn't do anything different from the original solution and will still query with each call to the app method.
If you want to keep the database calls to one per controller, then you can store the ProductSetting::first() onto the BaseController:
class BaseController extends \Controller
{
protected $product_settings;
public function __construct()
{
// Fetch the Site Settings object
$this->product_settings = ProductSetting::first();
}
}
Then within every controller you can call $this->product_settings->pagination.
in my controller in my show function in laravel i want the get the id that shows in browser show when i browse it it shows like this
http://localhost:8000/admin/invoices/1
i want to get that "1" and use it in show controller like below
public function show(Invoice $invoice)
{
$clients = Invoice::with('user','products')->get();
$invoice_id = 1;
$invoices = Invoice::with('products')->where('id', '=', $invoice_id)->firstOrFail();
return view('admin.invoices.show', compact('invoice','invoices'),compact('clients'));
}
and put it instead of $invoice_id so when every my client visit this page only sees the related invoice products . thanks you for help
If you're actually getting an instance of Invoice passed to your show method then it likely means you have Route-Model Binding set up for your project. Laravel is looking at the defined route and working out that the ID part (1) should map to an instance of Invoice and is doing the work to grab the record from the database for you.
The Invoice object passed through should refer to an item in your database with the ID of 1, so to get the ID that was mapped in the route you can simply just do:
public function show(Invoice $invoice)
{
echo $invoice->id; // This should be 1
Laravel supports route model binding out of the box these days, but in earlier versions you had to set it up in app/Providers/RouteServiceProvider.php. If you don't want it, try replacing your show method signature with this:
public function show($id)
{
echo $id; // Should be 1
By removing the type-hint you're simply expecting the value that was given in the route parameter and Laravel won't try to resolve it out of the database for you.
Simple way you may try this.
//Define query string in route
Route::get('admin/invoice/{id}','ControllerName#show')
//Get `id` in show function
public function show(Invoice $invoice,$id)
{
$invoice_id = $id;
}
Try using $invoiceId
public function show(Invoice $invoice, $invoiceId)
{
$clients = Invoice::with('user','products')->get();
$invoices = Invoice::with('products')->findOrFail($invoiceId);
return view('admin.invoices.show', compact('invoice','invoices'),compact('clients'));
}
do this if you want to get the url segment in controller.
$invoice_id = request()->segment(3);
if you want this in view
{{ Request::segment(3) }}
Goodluck!
Usually happens when giving a route name different from the controller name
Example:
Route::resource('xyzs', 'AbcController');
Expected:
Route::resource('abcs', 'AbcController');
I need a little help and I can’t find an answer. I would like to replicate a row from one data table to another. My code is:
public function getClone($id) {
$item = Post::find($id);
$clone = $item->replicate();
unset($clone['name'],$clone['price']);
$data = json_decode($clone, true);
Order::create($data);
$orders = Order::orderBy('price', 'asc')->paginate(5);
return redirect ('/orders')->with('success', 'Success');
}
and i got an error :
"Missing argument 1 for
App\Http\Controllers\OrdersController::getClone()"
.
I have two models: Post and Order. After trying to walk around and write something like this:
public function getClone(Post $id) {
...
}
I got another error
Method replicate does not exist.
Where‘s my mistake? What wrong have i done? Maybe i should use another function? Do i need any additional file or code snippet used for json_decode ?
First of all, make sure your controller gets the $id parameter - you can read more about how routing works in Laravel here: https://laravel.com/docs/5.4/routing
Route::get('getClone/{id}','YourController#getClone');
Then, call the URL that contains the ID, e.g.:
localhost:8000/getClone/5
If you want to create an Order object based on a Post object, the following code will do the trick:
public function getClone($id) {
// find post with given ID
$post = Post::findOrFail($id);
// get all Post attributes
$data = $post->attributesToArray();
// remove name and price attributes
$data = array_except($data, ['name', 'price']);
// create new Order based on Post's data
$order = Order::create($data);
return redirect ('/orders')->with('success', 'Success');
}
By writing
public function getClone(Post $id)
you are telling the script that this function needs a variable $id from class Post, so you can rewrite this code like this :
public function getClone(){
$id = new Post;
}
However, in your case this does not make any sence, because you need and integer, from which you can find the required model.
To make things correct, you should look at your routes, because the url that executes this function is not correct, for example, if you have defined a route like this :
Route::get('getClone/{id}','YourController#getClone');
then the Url you are looking for is something like this :
localhost:8000/getClone/5
So that "5" is the actual ID of the post, and if its correct, then Post::find($id) will return the post and you will be able to replicate it, if not, it will return null and you will not be able to do so.
$item = Post::find($id);
if(!$item){
abort(404)
}
Using this will make a 404 page not found error, meaning that the ID is incorrect.