How can i access this multi URL in laravel 4? - laravel

How can i access this multi URL in laravel 4.2.
http://localhost/new/public/library/course/1/First%20Video
My code is
Route::get('library/course/{id}', function($id){
return View::make('course')->with('id',$id);
});
Route::get('library/course/{id}/{video}', function($id, $video){
$array = array('id' => '$id', 'video' => '$video');
return View::make('course')->withvideos($array);
});

You are accessing the URL correctly, and it should reach your second route.
But you have a bug in your route:
// Your code
$array = array('id' => '$id', 'video' => '$video');
You shouldn't put single quotes around your variables -- it should be:
$array = array('id' => $id, 'video' => $video);
The single quotes force PHP not to resolve the two variables, so they are simply literal strings -- not what you want here.
Also, to pass the parameters to the view, you should adjust your code to use:
return view('course', $array);

Related

Missing required parameters for [Route: reviews.show] (from controller)

i want to route a new page from controller, i am getting data successfully when passing in route but getting error.
here is my particular part of controller -
$use = User::find($user->id);
$pos = posts::find($post->id);
//dd($user1, $post1);
return redirect()->route('reviews.show', $pos, $use);
in web.php part -
Route::get('p/{posts}/review/{user}','ReviewController#show')->name('reviews.show');
in show method -
public function show($posts, $user)
{
return view('posts.reviews.reviewshow', ['posts' => $posts], ['user' => $user]);
}
You have to use like this
return redirect()->route('reviews.show', ['posts' => $pos, 'user' => $use]);
route() calls in general take URL parameters as associative array. You are using helper function for it, but it still applies.
redirect()->route('reviews.show', ['posts' => $pos, 'user' => $use]);
When you pass 2 paramters to a route you send them as Array
return redirect()->route('reviews.show', [ $pos, $use ]);
This is your issue

Pass Data With Array in Laravel 6?

In CodeIgniter 3.x, we can pass data with array by using this code:
$data=array(
'apartment' => $this->apartmentmodel->get_all(),
'title' => "List Apartement"
);
$this->load->view('apartment/index',$data);
But, this code is not working when implement on my Laravel 6 project
$data=array(
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
);
return view(apartment.index,$data);
What is the problem and how to fix it? Anyone can help me?
Just remove ; from line number 2 before , your it will be work
$data=array(
'apartment' => DB::table('apartment')->get(),
'title' => "List Apartement"
);
return view('apartment.index', $data);
or you can use compact too, see below docs
https://laravel.com/docs/6.x/views
In Laravel we can use eloquent for fetch data from database.
$data=[
'apartment' => Apartment::get();,
'title' => "List Apartment"
];
return view('apartment.index', ['data' => $data]);
It will help full for you.
Thanks
PHpanchal
return view('apartment.index',compact('data'));
You can use compact() method, and you have an error (if this is project code).
Try:
$apartment = DB::table('apartment')->get();
$title = 'List Apartement';
return view('apartment.index', compact('apartment', 'title'));
Or
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', compact('data'));
Or:
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', ['data' => $data]);
Update:
In template file you can use it by calling $data, because in return statement you pass the variable name that will be shared with current template. So, to get some data from your array $data you just need to do:
#foreach($data['apartment'] as $apartment)
{{ $apartment }}
#endforeach
And to get your title use $data['title'] as well

laravel 5.2 retrieve multiple records

i need help here in laravel i'm trying to get two records from the database and pass in these two objects so far the record is retreived but only one variable in passed to the views why is that?
$slides = \App\slides::all();
$followup = Text_Pages::where('machine_name', 'Follow up')->firstOrFail();
$branches = Text_Pages::where('machine_name', 'branches')->firstOrFail();
return view('index',
['slides' => $slides],
['branches' => $branches],
['followup' => $followup]
);
use it like this
return view('index', ['slides' => $slides, 'branches' => $branches, 'followup' => $followup]);
The view method expects all of the view's variables as its second parameter.
return view('index', [
'slides' => $slides,
'branches' => $branches,
'followup' => $followup,
]);
Alternatively, you can use compact to create the array with a bit less typing:
return view('index', compact('slides', 'branches', 'followup'));

Codeigniter redirect add dynamic 3rd URI segment

I'd like to get this to redirect to welcome/stats/3 asuming site_id is 3
function add_keyword()
{
$data = array(
'keyword' => $this->input->post('keywords'),
'depth' => $this->input->post('depth'),
'site_id' => $this->input->post('site_id')
);
$site_id = $this->input->post('site_id');
$this->site_model->add_keyword($data);
redirect('welcome/stats/', $site_id);
}
You have to concatenate $site_id, not pass it as a second parameter:
redirect('welcome/stats/' . $site_id);

Is it possible to return multiple variables/arrays in Codeigniter?

Let's say in my model, I have a function that queries two separate tables. I need to pass the results back to my controller to display in my view.
I'm using MongoDB but it should be the same for any other DB. Normally this would work.
$files = $grid->find(array("username" => $profile_id,
"thumbnail" => array('$ne' => true)) );
$return files;
But, I need to go a step further and check to see if the user has a default photo selected.
$getCount = $grid->count(array("username" => $profile_id,
"default" => array('$ne' => true)) );
If I recall correctly, I would normally do ...
$return array($files, $getCount);
Doesn't work though.
Figured it out ... in my function, I did the following.
$files['data'] = $grid->find(array("username" => $profile_id,
"thumbnail" => array('$ne' => true)) );
$files['count'] = $grid->count(array("username" => $profile_id,
"default" => array('$ne' => true)) );
$return files;
In my view, I would manipulate my data accordingly.
echo $files['count'];
foreach( $files['data'] as $obj) {
...
}
To do it the way you wanted to in your original post it'd be something like:
$return array('files' => $files, 'getCount' => $getCount);
I'd split it into two separate model functions. I think its cleaner than fetching and returning the two separate pieces of data from the one function.

Resources